1

I would like to know how to undo a git commit or rename a commit message in sourcetree. I have committed my changes with a wrong commit message. I would like to undo that commit and also remove the same from the graph. I have tried reverse commit option but it shows another commit in the graph.

Sumit
  • 93
  • 3
  • 13
  • 2
    Possible duplicate of [How to undo the most recent commits in Git?](https://stackoverflow.com/questions/927358/how-to-undo-the-most-recent-commits-in-git) – Siddharth Das Jun 03 '18 at 15:04

3 Answers3

4

I'm imaging now that you tried to use git revert to undo the commit but it ends you up with additional commit, Here is a solution for you. use git log --oneline' to show you your logs, You gonna find the commit with wrong message, Besides it you gonna find its hash (like 53fnd3w) copy it then type

git reset --hard <paste-the-hash>

This will lead you to go back to that commit, after that if you wanna just change the name of the commit use

git commit --amend -m "New commit message"

This gonna make you change the name of the latest commit which is the one you are standing on, But if you want to undo these changes because you want to divide them on two commits or something, Use

git reset HEAD^

This gonna return you back for the commit that is previous of the current one with changes un-committed, Once you make a new commit it will ignore the commits after HEAD^ and you gonna see only the new commit when you make

git log --oneline

Hope this helps

Amr Adel
  • 574
  • 1
  • 7
  • 18
1

Basically the functionality you are looking for is called amend, which allows you to make changes to your last commit. This is a functionality offered by Sourcetree, but since you already created a reverse commit which you want to get rid of, you still have to "get your hands dirty" with the command line.

To get rid of the last reverse commit do the following:

git reset --hard HEAD~1

After that, you can return to the comfort of Sourcetree and do the following to change your orignial commit's message:

  1. Go to your working copy
  2. On the bottom right choose from the Commit options -> Amend latest commit
  3. Edit your commit message and click on Commit
Christos Batzilis
  • 352
  • 1
  • 3
  • 12
0

A very useful command to "rewrite history" : rebase

You need to specify a reference point (last Ok commit in history) it goes like this :

git rebase -i <reference point sha/tag/branch>

this will lead you to a list of the commits and allow you to modify them with the following choices

#Commands:
#  p, pick = use commit
#  r, reword = use commit, but edit the commit message
#  e, edit = use commit, but stop for amending
#  s, squash = use commit, but meld into previous commit
#  f, fixup = like "squash", but discard this commit's log message
#  x, exec = run command (the rest of the line) using shell

I think 'r' is what your are looking for, but you can see it is much more powerful. git doc on rewriting history.

After this you will need to do a forced push to update your remote server

git push -f <remote> <branch>
marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
jo_
  • 8,324
  • 1
  • 18
  • 14