7

Actually by an accident I found out something interesting. I amended a change to an existing local commit. I know that will change the hash of the original commit - at least that's what I thought. But it seems git creates a complete new commit. No problem so far.

$ vim foo
$ git add foo
$ git commit -m "Edited Foo"
$ git log --oneline -n 1
5b122c7 Edited Foo

$ vim foo
$ git add foo
$ git commit --amend
$ git log --oneline -n 1
98f1e64 Edited Foo
$ git show 5b122c7   # wait what?

git show 5b122c7 will show me the original commit - so the amended commit is effictively a new commit.

But why is the old commit still in the repository? Okay it could be neat to have the original commit to go back.

But the original commit 5b122c7 does not even appear in git log --all
Also a git revert 5b122c7 does not fall back to 5b122c7 but instead to the previous commit of my original one.

I'm just curious about this behavior and want to know: is there a way to find the original commit 5b122c7 with git log or something? In case I don't know the hash of the original commit: how can I find the hash?

boop
  • 7,413
  • 13
  • 50
  • 94
  • 2
    it is lost in the limbos. it will be garbage collected if git ever needs space. you can't find it unless you know its sha1. consider this other case: checkout a given sha1 (not a branch). create a commit on top of it. checkout anywhere else. your new commit is in limbo (and git warns you that this happens when you enter the detached state) – njzk2 Aug 25 '15 at 01:48
  • See http://stackoverflow.com/questions/26050327/how-does-git-commit-amend-work-exactly/26050416#26050416 to understand what happened to the old commit, and for a way to get it back. – jub0bs Aug 25 '15 at 05:14

1 Answers1

10

The old commit is still in the repository, but is no longer included in the history of any branch. So, 5b122c7 will eventually get cleaned up by Git's garbage collection.

There is no direct way to discover the original commit given only the hash of a new amended commit. Amending a commit is essentially throwing away the old one and making a new one. There is no link between them.

Within the repository where you amended the commit, the git reflog command will show the hash of the old commit. However, this is not part of what somebody else would see if they were to clone your repository.

Greg Hewgill
  • 951,095
  • 183
  • 1,149
  • 1,285