The scenario here is that you've got a lot of commits on the master
branch that should have been committed to a feature branch.
You want to reset master
back to the last commit, and you don't want to lose your work.
The process below is fairly specific to SourceTree.
- Create a new branch named
placeholder
at the point that you want to restore to ... This will be deleted later, after everything is confirmed ok.- In SourceTree: right click the commit, choose
Branch...
, name itplaceholder
, and clickCreate Branch
- Push this to origin
- In SourceTree: right click the commit, choose
- Create a new branch at your latest commit ... This is the branch that will contain further work, so you should stick to your typical feature branch naming conventions.
- In SourceTree: switch back to the branch for that latest commit and create a new branch from there
- Push this to origin
- Reset master back to the
placeholder
branch- Switch back to the master branch
- copy the SHA-1 for the commit that you want to reset to
- In SourceTree: right click on the commit and choose
Copy SHA-1 to Clipboard
- In Bitbucket: go to https://bitbucket.org/USER-NAME/PROJECT-NAME/commits/all ... find the commit ... click on its commit link ... take the last endpoint from the URL
- In GitHub: go to https://github.com/USER-NAME/PROJECT-NAME/commits/master ... find the commit ... click on its commit link ... take the last endpoint from the URL
- In SourceTree: right click on the commit and choose
git reset --hard b6d4ea1ac537f83b4fa7cc2c6e738217073bf815
git push -f origin master
- Delete the
placeholder
branch - Continue working in your new feature branch
You don't necessarily need the placeholder
branch. For smaller/simpler projects, it might be excessive - you can use the SHA-1 for the commit directly rather then marking your spot with the branch. For larger projects whose git history looks more like some kind of insane Git Hero game, it can be very useful.