2

I know they exist in git reflog, but if I push a commit to a remote repository, amend, then git push --force (warning: pushing --force to remote repositories should usually be avoided), will the commit that I overwrote exist somewhere?

mustafa.0x
  • 1,476
  • 2
  • 17
  • 30

2 Answers2

5

will the commit that I overwrote exist somewhere?

Only in:


Note that ORIG_HEAD (a memento from a time where reflog didn't existed yet) is not created/updated by a git commit (like a git commit --amend or a git commit --amend -m "new comment").
So if the reflog isn't activated on a repo, ORIG_HEAD cannot be used to reference the amended commit.

Community
  • 1
  • 1
VonC
  • 1,262,500
  • 529
  • 4,410
  • 5,250
  • I should be noted that usually pushes go to bare repositories, and bare repositories have the reflog disabled by default. So the second point should have a big "if" attached to it. – kostix Jun 21 '13 at 15:08
  • @kostix good point, I have included the reflog activation on bare repo in the answer. – VonC Jun 21 '13 at 15:18
  • I also beleive (though I'm lazy to check) that the `ORIG_HEAD` ref might point to the commit `HEAD` referenced just before the amendmend had been carried out. In theory, this might be able to ease saving one's neck if the reflog is for some reason disabled in a normal local repository (I mean, no `git fsck` run with manual exploration would be needed). – kostix Jun 21 '13 at 15:26
  • 1
    @kostix Just did a `C:\Users\VonC\Prog\git\git>c:\prog\Gow\bin\grep.exe -nrHI "ORIG_HEAD" *`: the following commands do indeed an `update_ref("updating ORIG_HEAD", "ORIG_HEAD", head_commit->object.sha1`: `git merge`, `git reset`, `git resolve` (now deprecated), `git am` and `git rebase`. **Not `git commit`**. I just tested it. – VonC Jun 21 '13 at 15:58
  • Thank you. So basically it's only accessible via local and remote's reflog (which eventually gets GCed)? – mustafa.0x Jun 22 '13 at 10:47
  • @mustafa.0x yes, or it is in any other local clones done by other developers. – VonC Jun 22 '13 at 12:22
1

No. Your amended commit will stay in your local repo for some time, but it won't be pushed.

When pushing, Git computes the missing commits to have the remote branch at the same point as your local branch. Since your amended commit is not on the branch anymore, it won't be pushed.

An amended commit stays until a garbage collection occur. But GC check several conditions before removing a commit, so don't be afraid of losing data.

Guillaume Darmont
  • 5,002
  • 1
  • 23
  • 35