0

I've always known that if I've already pushed a commit I should never change its hash (rebase it, --amend it) and then push it up again cause you'll end up with (probably) two identical commits but with different hashs.

If I pull from that remote how can I possibly know that something like that happened? What's the matter with having two identical commits with different hashs?

3 Answers3

1

To re-push a changed commit- you must specify the -f option. This means changing history (you are saying 'this commit that I already pushed - I want it removed in the past').

This breaks things for anyone who has pulled in the meantime. Their next pull or push will result in conflicts and a need for a merge - so git will tell them.

In other words, git push -f is allowed:

  • if you are the only developer
  • or if you do it very quickly after realising the mistake.
Vorac
  • 8,726
  • 11
  • 58
  • 101
  • Yes, that is correct and it's what I usually do since I'm not working on huge projects. But is there something immediate that should trigger my mind and thing "Ok this commit has definitely been pushed twice by mistake" –  May 30 '18 at 14:55
  • @matt.logic I am confused by your query. If *you* are pushing a changed commit, git will refuse to push without `-f`. I someone else has pushed a changed commit, I think(but am not sure) git will prompt you for a commit message for ensuing merge. – Vorac May 30 '18 at 15:40
0

If I pull from that remote how can I possibly know that something like that happened? As you've already noted, the hashes will differ if you've already pulled said commit. You may not realize it's the same commit if the commit message changed too.

What's the matter with having two identical commits with different hashs? You will most likely cause conflicts for yourself and anyone else working on the repo.

See https://git-scm.com/book/en/v2/Git-Tools-Rewriting-History for more info.

jmargolisvt
  • 5,722
  • 4
  • 29
  • 46
0

You're asking "what happens if I pull". Nothing, because you'll be in sync, given you've force pushed the rewritten history. The problem will be when multiple people use the same remote repository.

Say, you amend, rebase or otherwise rewrite the latest commit. In essence, this deletes the most recent commit and writes a new one on top of the previous one. Now you push this to the remote repository, and it'll be rejected, because your newest commit has no relation to the latest commit the remote knows about.

So you'll have to force push, overwriting the remote branch.

Now if another developer pulls from that remote, they'll end up in trouble: they still have that older commit that you removed, and their local repo can't match your repo to any tree in their repo.

See Git pull after forced update how to resolve that.

So if you really want to rewrite already pushed history, you need to carefully coordinate that with other users of that remote repository.

So to answer your question, how you'll discover a force pushed commit: you won't be able to pull.

CodeCaster
  • 147,647
  • 23
  • 218
  • 272
  • This does make sense, I've never tried to push an amended commit cause I knew that was not a good idea and that I had to use the push --force to remove the latest one. So basically there is no way you can push an amended commit without intentionally use `push --force` ? –  May 30 '18 at 15:01
  • And also in amended or rebased commits, the commit date and the author date are different. – Philippe May 30 '18 at 19:50