git accidentally tracked config file

Take for example the following scenario:

  • You accidentally track and commit a config file
  • Each development environment should have their own specific config file, so you should have .gitignore before git add
  • You didn't realise for quite a while
  • The commits

    A - B - C - D - E
        |   |   |   |
        |      |  /
        | commits that accidentally track application config
        |
        commit to untrack & .gitignore config
        [finally you did the right thing... but too late?]
    

    If you ever reset or cherry-pick back to C, D, or E, it'll overwrite the config file.

    Is there anyway to rewrite C - E by applying the B commit on them?


    If the config file should just be untracked, better make a commit over E untracking the config file, adding it to gitignore etc. If the config has sensitive information, in which case it must be removed from any commit of the repo, you have no other go but modifying history ( This help page from GitHub talks about how you can do this - http://help.github.com/remove-sensitive-data/ )

    Due to the very nature and requirements of Git, you cannot make a change to a commit and make it seem to be the same commit.


    If the authors are all you and you don't need to preserve the dates on the affected commits (all of them, AE) it'll be almost trivially easy, the other way will take more

    Working from your picture I'll add revision F as the commit before the mistake. Assuming you're on branch 'master',

    git log --oneline master~6..master
    

    should show you those revisions.

    git branch corrected master~5   # this is F, master~0 is A i.e. the branch tip
    
    git config advice.detachedhead false  # just to get it to stop blabbing at you
    
    # make corrected commit E
    git checkout master~4
    git rm --cached yourconfigfile
    echo ref: refs/heads/corrected >.git/HEAD
    git cat-file -p master~4 | sed 1,/^$/d | git commit -m-
    
    # make corrected commit D
    git checkout master~3
    git rm --cached yourconfigfile
    echo ref: refs/heads/corrected >.git/HEAD
    git cat-file -p master~3 | sed 1,/^$/d | git commit -m-
    
    # ... repeat for C, B, and A
    

    At the end,

    echo ref: refs/heads/master > .git/config
    

    and you're done. Preserving the author/date info is a matter of setting GIT_{AUTHOR,COMMITTER}_{NAME,EMAIL,DATE} from the headers git cat-file -p shows you, if you need it I'll cook up a sed for you.

    链接地址: http://www.djcxy.com/p/10688.html

    上一篇: 记事本++类型的FTP插件,用于崇高的文字,netbeans或eclipse

    下一篇: git意外地跟踪配置文件