sidney
sidney

Reputation: 2714

Cannot change an old commit

I am trying to change a commit that is 39 commits old (there is 39 commits ahead of this one). I only need to change a few lines in only one file.

Here's what I have done thanks to this question:

git rebase --interactive e0ea83ac7c68df747705e3f39864cd4931416ae4^

I wrote edit instead of pick to the line next to this commit, inputed 'Control+X' on nano, typed "Y", then enter, and I got:

⭑ shideneyu project/nuddz/public(master) ✔»git rebase --interactive e0ea83ac7c68df747705e3f39864cd4931416ae4^                                               [19:02:01]
Stopped at e0ea83a... Migrated every Reponse + ProvisionalReponse data to Answer
You can amend the commit now, with

    git commit --amend

Once you are satisfied with your changes, run

    git rebase --continue

Here is the problem: I cannot make the necessary changes since the version of the file is already too recent. The file that I got under my eyes is not the one that I had when I commited that commit. Indeed, that file under my eyes is the same than the most recent commit !

Maybe that there is an hint here, when I do git log, here is what I got:

⭑ shideneyu ~/project/nuddz(02b5f2c) ✔»git log 
commit 02b5f2c9c4987d433329b172c9cda28bf74d9db8
Author: sidney <shiden**@gmail.com>
Date:   Mon May 12 15:20:01 2014 +0200

    Migrated every Reponse + ProvisionalReponse data to Answer

commit 75468d2a2dba8d06540781b6a13783610130eed8
Author: Alexis <*****@yahoo.fr>
Date:   Mon May 26 10:44:40 2014 +0200

    Moved debugger gem in developpment

commit 2ff563c63ce395f72b1f9acb4a0de7c0d64fb204
Author: Alexis <*****@yahoo.fr>
Date:   Fri May 23 19:30:31 2014 +0200

    Assets precompile before Heroku push

[...]

I truncated in purpose (too many records). I don't think that it is normal. The chronology (12 may is the most recent? How come the 26 May exists then if I want to change a commit that existed a dozen days ago if the recent changes exist?) is weird.

I aborted the rebase with rebase --abort, then looked on the differences of the different commits: I am right, I am not crazy, this is the truth, rebase didn't changed the local files to the version they should be 39 commits ago.

How can I make that change then? What should I do?

EDIT: I upload this screenshot which shows the differences while looking at the logs thanks to gitk --all, and visualizing the specific file with cat Gemfile while doing git rebase --interactive e0ea83ac7c68df747705e3f39864cd4931416ae4^

enter image description here

Upvotes: 2

Views: 104

Answers (1)

Martin G
Martin G

Reputation: 18109

If I wanted to change the contents of an old commit i would remove it and merge it back to the top of the branch.

git checkout <your_branch>
git revert <old_commit>
git merge <old_commit>

Do your updates... and then

git commit --amend -m "Re-applying <old_commit> with the addition of <blablabla>"

I think it's nice to be able to see that this has been done in the history of the branch so there is no confusion later on.

If you want to keep cleaner, then use a temporary branch for the cleanup job and integrate it on your branch with ONE new version.

git checkout -b <temp_branch> <your_branch>
git revert <old_commit>
git merge <old_commit>

Do your updates... and then

git commit --amend -m "..."
git checkout <your_branch>
git merge --squash <temp_branch>
git commit --amend -m "Explain what was done"

Upvotes: 1

Related Questions