70

Recently in a project with multiple people, a commit was made as seen in the image below. Marked in red you can see a commit with the description/comment of 'Merge?'.

This commit added numerous files and altered numerous others and was never intended to take place.

Using what do I need to do to roll everything back to the commit highlighted in blue? (I am 8 commits behind as seen in the screenshot.)

sourcetree troubs

captainrad
  • 3,760
  • 16
  • 41
  • 74
  • 7
    I don't know about that program, but in git bash you can do `git reset --hard [the hash of the commit]`. – PlasmaPower Mar 12 '14 at 22:40
  • does 'hard' remove all other commits made after the 'bad commit'? – captainrad Mar 12 '14 at 22:41
  • Yes, it will change everything back to the way it was just after the commit you pass it was made. – PlasmaPower Mar 12 '14 at 22:43
  • 2
    Nothing but garbage collection actually *removes* commits in git, but `git reset` moves a branch pointer, and once the commits are "beyond the tip of any branch", they become ripe for garbage collection. (The branch's reflog keeps them around for a default expiration time of 30 days before they really get reaped.) So they're effectively gone, but in emergencies you can "un-remove" them for a month or so. – torek Mar 13 '14 at 00:07
  • Right click -> Reverse commit -> Push. http://flummox-engineering.blogspot.com/2014/10/how-to-undo-git-commit-in-sourcetree.html – Kanagavelu Sugumar Jan 08 '18 at 08:24

3 Answers3

104

If you have pushed the commits upstream...

Select the commit you would like to roll back to and reverse the changes by clicking Reverse File, Reverse Hunk or Reverse Selected Lines. Do this for all the commits after the commit you would like to roll back to also.

reverse stuff reverse commit

If you have not pushed the commits upstream...

Right click on the commit and click on Reset current branch to this commit.

reset branch to commit

0xcaff
  • 13,085
  • 5
  • 47
  • 55
  • This answer is outdated. – Yeats Jun 26 '16 at 11:35
  • 5
    The idea is the same. Feel free to fix it @Yeats – 0xcaff Jun 26 '16 at 15:39
  • 7
    what next? after I resetted branch to the previous commit, it is asking me to pull everything back to the current version. I am not able to push anything – Neville Nazerane Oct 07 '16 at 23:56
  • @NevilleNazerane Force push. – 0xcaff Oct 07 '16 at 23:57
  • 2
    i tried that.. the gui had it grayed out. The terminal din't work either – Neville Nazerane Oct 09 '16 at 01:34
  • 1
    For anyone who has items in the SourceTree right-click menu greyed out: I restarted SourceTree and it went back to normal. – Lesley Jan 15 '18 at 11:28
  • @Yeats This worked perfectly for me. I am using `Version 2.3.2`. The images are not exact yet the wording in the dropdown is the same. – davidhartman00 Oct 23 '19 at 19:25
  • In Aug 2020 still found this a useful 'quick' method for a small change in 1 file. Reverse commit or resetting the branch probably a better method overall though. Didn't see the option to 'reverse file' though (v.3.3.9). – codah Aug 18 '20 at 00:23
  • One day you are in a hurry, you make a mistake, you look for a solution and someone from internet post exactly what you need perfectly explained. Thanks :-) – Oscar Foley Nov 17 '20 at 13:01
44

I searched for multiple options to get my git reset to specific commit, but most of them aren't so satisfactory.

I generally use this to reset the git to the specific commit in source tree.

  1. select commit to reset on sourcetree.

  2. In dropdowns select the active branch , first Parent Only

  3. And right click on "Reset branch to this commit" and select hard reset option (soft, mixed and hard)

  4. and then go to terminal git push -f

You should be all set!

FluffyKitten
  • 13,824
  • 10
  • 39
  • 52
Sidd Thota
  • 2,040
  • 1
  • 20
  • 24
6

There are two options to revert one commit our merged branch.

A. Using sourcetree

  • Right click in commit
  • Select reverse commit
  • Sometimes, it can be failed. In that case, please try option B.

B. Using terminal

  • Select your commit and look info on bottom side of screen. Example commit id: 1a6cd9879d8c7d98cdcd9da9cac8979dac7a89c

  • Click top right corner "Terminal" option of Sourcetree.

  • Write in terminal: git revert your_commit_id -m 1

  • When the commit message editor opens up:

    • For just accept and exit, write :q then click enter.
    • If you want to change the commit message, press "i", write your message and then write :wq click enter.
canerkaseler
  • 6,204
  • 45
  • 38
Rui Meneses
  • 61
  • 1
  • 3