Home > Software design >  How to reword a commit starting with a hashmark using git rebase?
How to reword a commit starting with a hashmark using git rebase?

Time:12-22

I have made a commit with just WIP and now I would like to reword it to #123456 - my message here, where #123456 is the number of the issue.

Since I've made other commits since then, my only option is to use git rebase and reword from there, but the hashmark is seen as a comment on the text editor.

Is there a way to do this without changing the comment symbol for the entire project?

CodePudding user response:

You have a number of options:

  • core.commentChar defines the character that is to be used for comments in commit messages. You can change it for this clone with git config, do the commit, and then change it back, or override it just for this one git commit command; see below.

  • commit.cleanup determines how a commit message is "cleaned" of commentary and other fluff: you can set this to whitespace or verbatim. Again, you can do this temporarily for just one command.

  • -m and -F options supply messages (as literal text, or via files) that are treated as if you used the verbatim cleanup option.

Of course your case is git rebase -i and edit, so I'd stick with commit.cleanup here: run git -c commit.cleanup=verbatim rebase -i. I have not actually tested this, but it should do the trick.

CodePudding user response:

It might not be the quickest way, but you could (from a clean working tree)

# point a new branch at the faulty commit
git checkout -b repairBranch <yourBadCommit>

# amend the message
git commit --amend -m "#123456 - your message here"

# then finally bring copies of the later commits
git cherry-pick <yourBadCommit>..<yourOriginalBranch>

Then finally if the result is what you wanted, just reset your original branch on the new one with git reset --hard repairBranch (to be executed from your original branch, on a clean tree)

CodePudding user response:

Yes, you can use git rebase -i HEAD~N, where N is the number of commits from the most recent commit that you want to include in the interactive rebase. This will open a text editor showing a list of the commits that you specified. You can then change the pick command at the beginning of the line for the commit that you want to modify to reword.

For example, if you want to modify the commit that you made immediately before the most recent commit, you would run git rebase -i HEAD~2. This would open a text editor showing the two most recent commits, and you could change the pick command for the commit that you want to modify to reword.

When you save and exit the text editor, git will apply the interactive rebase and open a new text editor for you to modify the commit message for the commit that you selected. You can then modify the commit message to include the issue number, using the format #123456 - my message here.

After you save and exit the text editor, git will continue with the interactive rebase, applying any other changes that you specified. When the rebase is complete, your commit message will be updated with the issue number included.

  •  Tags:  
  • git
  • Related