简体   繁体   中英

Why is a git merge required if I want to amend last remote commit?

Sometime I commit or push some code and then I realize that there's a tiny error somewhere. So I 'amend last commit' with the new changes, and this happens when I try to push:

If I haven't pushed the last commit, everything works fine

If I already pushed the last commit, i get

$ git push origin
To https://github.com/Kyklos-Italy/GitFashion.git
 ! [rejected]        develop -> develop (non-fast-forward)
error: failed to push some refs to 'https://github.com/Kyklos-Italy/GitFashion.git'
hint: Updates were rejected because the tip of your current branch is behind
hint: its remote counterpart. Integrate the remote changes (e.g.
hint: 'git pull ...') before pushing again.
hint: See the 'Note about fast-forwards' in 'git push --help' for details.

So I merge getting

$ git pull origin
Merge made by the 'recursive' strategy.

and then I'm allowed to push.

Why is there such a behaviour?

Amending creates a completely new commit. That means, you have to merge your local, thus generated, branch with the remote changes.

For example you have some commits:

A---B---C 

When you amend these changes it will look like this afterwards, where C is your original and D your amended commit:

A---B---D
     \
      C

As long as there is no other branch pointing to C , you won't see it anymore and it looks like nothing has changed but your editions (When already pushed, the remote branch still should point to C , eg origin/master ). Because there is a new branch now, you have to merge these.

Maybe, you could avoid merging by pushing with option --force . Given you have the required privileges:

git push origin master --force

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2025 STACKOOM.COM