标签云

微信群

扫码加入我们

WeChat QR Code


For those somewhat new to git: Laurie's point about having not yet pushed is important. Like rebasing, this is changing the history. If someone has cloned/pulled from your repo between the original and rewritten history then they won't be able to pull after the rewrite (for that branch).

2019年02月21日08分28秒

However git commit --amend isnt as powerful as git rebase -i.

2019年02月22日08分28秒

jeffjose, It definitely doesn't need to be. Also, git commit --amend can fix up the (a?) master commit.

2019年02月22日08分28秒

If you've already pushed, just force push again: git push -f origin branchname

2019年02月22日08分28秒

hughes isn't git push -f a bit dangerous if other people are using the same repository?

2019年02月21日08分28秒

If you don't want to rewrite the entire commit message, go for git commit --amend -c HEAD. This will open the editor pre-populated with your old commit message, so you can change it.

2019年02月22日08分28秒

I did git commit --amend -m "New message", but pushing to Github generated the "Merge the remote changes before pushing again". After pull, commit --amend, and push again, the new message doesn't appear. Instead I have "Merge branch 'master' of github.com:[myrepo]"

2019年02月21日08分28秒

DaveEveritt you most likely pushed your commit upstream before trying to fix it.

2019年02月22日08分28秒

Kyralessa not true. In bash you can easily compose multiline commit messages by just not closing the quote until you're done (hitting return at the end of each line within the quotes).

2019年02月22日08分28秒

I don't get how an answer that looks a lot like just the main idea of an answer that was written two years ago and also the accepted answer gets so many votes. Strange. (nothing wrong with the answer though)

2019年02月22日08分28秒

AmalMurali, well. My point wasn't so much about the popularity of the question, nor the utility of the answer. But this particular answer is not the oldest answer, nor does it offer any further insight into the accepted answer. It appears to be a copy of a section of the accepted answer. That was my point. CHEERS!

2019年02月21日08分28秒

Can one change the message of the first commit (which doesn't have a parent)?

2019年02月21日08分28秒

This is mentioned in one of the other answers but I will put a note of it here. Since git 1.6.6 you can use reword in place of pick to edit the log message.

2019年02月22日08分28秒

Incidentally, $parent_of_flawed_commit is equivalent to $flawed_commit^.

2019年02月21日08分28秒

Never EVER do this (or rebase in general) if you have already pushed upstream!

2019年02月22日08分28秒

Use -p (--preserve-merges) if there was a merge after the flawed commit.

2019年02月21日08分28秒

git rebase -i HEAD~commit_count will also allow you to change the commit messages of however many commits you choose. Just mark the chosen commits as "reword" instead of "pick".

2019年02月21日08分28秒

What if you don't want to rebase? You just want to change an older message?

2019年02月22日08分28秒

git reset --hard annihilates uncommitted changes. Please replace --hard with --soft.

2019年02月21日08分28秒

And if you don't want to add everything, you can first do git add file.ext then just git commit --amend

2019年02月21日08分28秒

Is there a version of this that does not change the commit if the regex doesn't find anything?

2019年02月22日08分28秒

AFAIK filter-branch --msg-filter will generate new commits in any case. However, you could check within the msg-filter, if the sed succeeded and use this information when the filter-branch operation ends to reset your tree to refs/original.

2019年02月21日08分28秒

DavidHogue This is only true when using the filter-branch method. The commit IDs following a modified commit do not change if you use the interactive rebase.

2019年02月21日08分28秒

Mark Yes they do, they are required to. Commit ids are dependent on previous commits. If they didn't change, git would be useless.

2019年02月21日08分28秒

You need $flawed_commit^..HEAD, not $flawed_commit..HEAD. as stated by the man page: «The command will only rewrite the positive refs mentioned in the command line (e.g. if you pass a..b, only b will be rewritten).»

2019年02月21日08分28秒

For me, using your command above actually creates a new commit with a new commit ID plus an extra commit saying "merge branch" as a default commit message.

2019年02月21日08分28秒

Amending always creates a new commit with a new commit ID. The commit ID is the SHA hash of the contents of the commit, including the commit message and authored/committed timestamps. This is a feature of Git that, barring hash collisions, ensures that two commits with the same ID are exactly the same commit, with exactly the same content, history and so on.

2019年02月22日08分28秒

Agree with Emil. Additionally, reading the docs - it seems that all "-c" does is tell git which commit's message to use as the default/template for your new commit..Really its already going to do "-c <commit ID>" by default, so no need to specify it.

2019年02月22日08分28秒

The -c does a few things. It uses the old message by default, but it also copies authorship information (person and time). -C does the same thing except that it does not ask you to edit the message.

2019年02月21日08分28秒

Like SantanuDey , it didn't work for me. I got fatal: Option -m cannot be combined with -c/-C/-F/--fixup.

2019年02月22日08分28秒

If you want to make sure your change from git commit --amend took affect you can use git show and it will show the new message.

2019年02月22日08分28秒

This earlier answer already says that you can use git commit --amend, and it also says that you can use git rebase -i HEAD~commit_count, all you did was plug in 3 for commit_count.

2019年02月22日08分28秒

This answer doesn't address the OP's question, as they're purely interested in fixing a commit they've only just done. I regularly use git commit --amend to fix up comments or add files I forgot to git add, but only ever before I've git pushed. I also use git filter-branch when I want to totally mess with the version history, but the OP doesn't want this, so this answer needs a big health warning - don't try this at home, peeps!!

2019年02月22日08分28秒

Is there a good way to fix commit messages already pushed to a public repository? So far I have come to the conclusion that, once pushed, my commit message typos and thinkos have to live forever.

2019年02月22日08分28秒

In a word, NOPE! There is no GOOD way to retract something you have pushed. All retractions are BAD to a greater or lesser degree. You need to adopt the discipline of working in a branch in your own private repository, doing multiple commits as you add a bit, test a bit, tweak a bit. Then merge your entire branch into a single commit, write a new commit message describing the overall change, PROOFREAD it, and push.

2019年02月22日08分28秒

Just to point out the obvious that one doesn't have to make a single commit when going back from a feature branch. What many people do is rebase on the target branch (to make things look clean) then merge with the option to suppress fast-forwarding. Agree with the main point of being careful before you push up though.

2019年02月22日08分28秒

How do I get to that screen that you have displayed in your example?

2019年02月21日08分28秒

It's the lower right portion of the Windows Git Gui. Just select the 'Amend Last Commit' toggle, and it will populate with the most recent commit info.

2019年02月21日08分28秒

This does the exact same thing as git commit --amend except that it is a 2-step process.

2019年02月22日08分28秒

JosephK.Strauss I believe ammending the commit also keeps original commit author and date information, having the new commiter and date info separately. I'm not sure this approach does that.

2019年02月22日08分28秒

EvertonAgner You are correct. --amend will keep the author information, but the question only asks to change the message.

2019年02月22日08分28秒

git rebase -i <hash of one commit before the wrong commit> works for me. thanks.

2019年02月21日08分28秒

The "top" answer doesn't answer the question. It just gives a general introduction to git commit --amend. The question was very specific, therefore longer != better. The decisive mentioning of the -o flag would probably be buried in the rest of the information. I'm also not comfortable editing an answer which has so many votes already.

2019年02月21日08分28秒

That being said you're free to edit the top answer, since there is a real danger that people are using that as the "correct" answer. It can easily happen to amend your commit with staged stuff -- it happened to me, and it's really annoying when you happen to push that. But still, quantity is no guarantee for correctness. Neither in number of answers nor in number of votes.

2019年02月22日08分28秒

I wouldn't go so far to say that the top answer is "incorrect" and that it "doesn't answer the question". It definitely works and answers the question, you just need to make sure that you don't have staged changes when you try to amend. But I see your point about having to warn people about that. I'll edit it in later if I have time.

2019年02月22日08分28秒

To be fair: even though the --only option with --amend is available since git 1.3.0 it didn't work correctly till it was fixed in 1.7.11.3 (ea2d4ed35902ce15959965ab86d80527731a177c). So the right answer back in 2008 would probably have been something like: git stash; git commit --amend; git stash pop.

2019年02月22日08分28秒

If all you want to do is to edit the message of your last commit, using a soft reset for that purpose is over-kill. Just use git commit --amend, exactly like how it says in the top voted answer. Additionally, git reset --soft HEAD^ works identically to the soft reset in this earlier answer, because they both reset back to the first parent commit.

2019年02月22日08分28秒

I only bother to add git reset in the solution just to give an idea to split one commit message into multiple commit messages. Because, I have faced that problem when, I was starting to use git. Sometimes, this can be really helpfull. :)

2019年02月22日08分28秒

<nitpick>There are no "threads" on Stack Overflow, because it's not a discussion forum, there are only "questions", "answers", and "posts".</nitpick>. Also, not all versions of Vim are the same, not all of them will let you delete characters in insertion mode (makes sense in a way, right?). If you want to always be able to delete characters in Vim, X and x will do that (little x deletes characters in front of the cursor, X will delete behind). If you make mistakes, you can use u repeatedly to undo. Finally, r is shorthand for reword in the interactive rebase editor.

2019年02月21日08分28秒

To change a word in vim is cw typed at its beginning (though the question is not about vim, I agree).

2019年02月22日08分28秒

This requires installing an external program. In my opinion, it would be better to learn to use the built-in tools and aliases more effectively. I would type: g c; g rb -i ~9 (commit and rebase), move the new commit to where I want it, change commit to f (fixup), and save. If you wanted something faster than that, you could alias git commit --fixup=<commit>; git rebase -i --autosquash <commit>^

2019年02月22日08分28秒

github.com/qxo/git-rebase-reword both for linux and windows

2019年02月21日08分28秒

Nothing gets ever "overwritten" in git. In this case the branch pointer will be set to your new commit and the old commit will get stale if no references are left to it and it might get cleaned up after a few weeks. (Until then others still can find and reference it, e.g. by looking into the reflog.)

2019年02月21日08分28秒