How to rollback everything to previous commit
Asked Answered
M

4

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

Map answered 12/3, 2014 at 22:36 Comment(5)
I don't know about that program, but in git bash you can do git reset --hard [the hash of the commit].Raycher
does 'hard' remove all other commits made after the 'bad commit'?Map
Yes, it will change everything back to the way it was just after the commit you pass it was made.Raycher
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.Synchromesh
Right click -> Reverse commit -> Push. flummox-engineering.blogspot.com/2014/10/…Gaikwar
G
106

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

Galvin answered 12/3, 2014 at 22:49 Comment(9)
This answer is outdated.Unlash
The idea is the same. Feel free to fix it @UnlashGalvin
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 anythingJehial
@NevilleNazerane Force push.Galvin
i tried that.. the gui had it grayed out. The terminal din't work eitherJehial
For anyone who has items in the SourceTree right-click menu greyed out: I restarted SourceTree and it went back to normal.Throb
@Unlash 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.Coulomb
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).Preoccupation
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 :-)Opisthognathous
L
45

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!

Leighleigha answered 11/9, 2017 at 18:35 Comment(4)
The answer is safe only If you have not pushed the commits upstreamMatherne
The answer is works only if you have not pushed the commits upstream.Poleax
this answer also works after pushed commits upstreamSewel
This response provides a total rollback for commits done after a commit selected.Irmine
R
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.
Redhot answered 16/2, 2022 at 21:15 Comment(0)
R
0

If the master branch needs a Pull Request to accept changes (e.g. git flow and you can't git push force your changes), you can:

  • Git clone a copy of your project
  • Go to that new copy and checkout the commit you want to rollback to
  • Go to the root folder of that commit in File Explorer
  • Copy all files and folders except the .git folder
  • Go to the original project and create a new branch from master
  • Go to the root folder of that original project
  • Remove all files and folders except the .git folder
  • Paste all the files and folders from before
  • Git add all changes and push to the new branch
  • Do a PR for this branch to master
Rab answered 22/12, 2023 at 14:56 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.