Gary Green
Gary Green

Reputation: 22395

git accidentally tracked config file

Take for example the following scenario:

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?

Upvotes: 7

Views: 520

Answers (2)

jthill
jthill

Reputation: 60585

If the authors are all you and you don't need to preserve the dates on the affected commits (all of them, A-E) 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.

Upvotes: 0

manojlds
manojlds

Reputation: 301567

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.

Upvotes: 3

Related Questions