Jordi
Jordi

Reputation: 23237

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?

Upvotes: 0

Views: 138

Answers (3)

Rahul Garg
Rahul Garg

Reputation: 4339

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"

Upvotes: 0

torek
torek

Reputation: 488183

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 may1 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.


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

Upvotes: 4

Makoto
Makoto

Reputation: 106430

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.

Upvotes: 1

Related Questions