简体   繁体   中英

git amend commit with different message

I've commited a file with message "m1". Later, I've modified it and I've tried to amend the last commit. However, I've changed the last message and I commited the changes with message "m2".

The situation is I've two different commits, one of them pending to push and another one pending to pull. I absolutly don't understand this situation and I'm not quite able to figure out what's up.

Any ideas?

The issue is that git commit --amend does not—and indeed cannot —change a commit.

Instead, it creates a new commit that smells a lot like the old one, except for whatever you have amended. Then it makes the current branch name point to the new commit instead of the old commit.

If anyone else has the old commit already, they still have it . In this case, you either already sent the commit upstream (with git push ), or obtained it from your upstream in the first place (with git fetch followed by something, usually merge or rebase, that set your local branch to include the commit). So they still have the original, and now you have the copy:

              C'  <-- branch
             /
... <- A <- B
             \
              C   <-- upstream/branch

You also still have the original version C , pointed to by upstream/branch (in this diagram) and by your reflogs (where you normally will not see it). You and only you have your "amended" copy C' , at least until you publish it somehow.

If you use git push --force you may 1 convince your upstream to take it, discarding their copy of the original C . Doing so is annoying to everyone else who may also have a copy of the original C and may be using it for something, so be sure that even if you have permission, you can get forgiveness. :-) If no one else (besides the upstream) has C , or if everyone has agreed to accept this kind of replacement, you're OK.


1 Whether the upstream accepts force-push is up to the upstream.

By amending the commit you've altered the history of your repository. Git is attempting to rectify this by claiming that your branch has diverged with your remote branch, and allows you to merge to fix it.

Don't merge. If you amended the commit, you intended to rewrite history.

Instead, you can force push the branch:

git push --force

This will tell Git that you fully intend to overwrite history and that your local branch has the correct history to be reflected on your remote repository.

If you have created a local commit but forgot to add some files. You can run :

git add .
git commit --amend -m "New Commit Message"

This will add these new files in already created commit and will update the commit message. If you just want to update the commit message, but not the content, below command is good enough.

git commit --amend -m "New Commit Message"

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-2024 STACKOOM.COM