Modify the message of an unpushed commit with git (and not only the last)

I am still learning my way around git, which I mostly use from within Visual Studio. Today I had to change a commit message because I noticed later that I had mentioned an incorrect ticket number. Fortunately I had not yet pushed the commits to origin.

If you need to change the latest commit’s message, that is easy. Just perform an “amend” to the latest commit. In the Visual Studio Team Explorer pane, go to Sync, lookup the latest Outgoing Commit edit the message and choose “amend” from the options. Or type git commit –amend -m “My new message” on the command line.

BTW You can also add more changes into the latest commit, which comes in handy if you spot a spelling error just after you commit. 😉

Changing any commit other than the last is a bit more difficult. We’ll need the command line here, there’s no interactive Visual Studio guidance available. But I’ll walk you through it. I’ve based my steps on this guide in combination with help with vim from here. (Of course you could change vim to your favorite editor for git, which could make parts of this procedure a lot easier, depending on your experience with vim.)

  • Open the command prompt in your repo directory: in Team Explorer, go to Connections, right-click your repo and choose Open Command Prompt.
  • Put the command window on the side and go back to Team Explorer, to Sync, and lookup the commit you would like to edit. Click its parent and copy the commit hash to the clipboard.
  • Now go to the command prompt and type
    git rebase -i (paste-your-hash-here)
  • This opens op vim (unless you registered another editor). This is where it can get scary for long-time Windows users like me, although I do have a bit of *nix experience.
  • You’ll see a list of your commits with “pick” in front of them. You need to change “pick” into “edit” for those commits you want to change the message for. To do that in vim,
  • Type ‘i’ to enter insert mode. You can now edit “edit” over “pick” by typing it and using the delete or backspace key.
  • Press ‘Esc’ to go back to command mode.
  • Type :wq (semi-colon, W, Q, Enter)
  • You’ll get back to the command prompt now.
  • Now you can type
    git commit --amend -m "Your new message"

    to change the message

  • Followed by
    git rebase --continue

    to reapply the rest

Back in Visual Studio, the Team Explorer pane will have displayed “a rebase is in progress” while you were busy, which should now change back to a regular view once VS notices git rebase is done. Go to the Sync view to view your unpushed commits. The commit message you wanted to edit should have changed. Ready to push now, if you want to.

Note: I later learned that you can also use ‘reword’ instead of ‘edit’ to replace pick in the editor. They are similar, but reword allows only a change of the commit message while edit allows for more changes like editing files. So if a change of message is your goal, reword is a safer choice than edit (but I haven’t tried that yet).

Advertenties

Over Shiftkey
I am Maarten, owner of and chief software developer for Shiftkey software development from The Netherlands. I will be writing mainly about things I run into when programming in C# or Delphi.

Geef een reactie

Vul je gegevens in of klik op een icoon om in te loggen.

WordPress.com logo

Je reageert onder je WordPress.com account. Log uit / Bijwerken )

Twitter-afbeelding

Je reageert onder je Twitter account. Log uit / Bijwerken )

Facebook foto

Je reageert onder je Facebook account. Log uit / Bijwerken )

Google+ photo

Je reageert onder je Google+ account. Log uit / Bijwerken )

Verbinden met %s

%d bloggers liken dit: