Skip to content

Instantly share code, notes, and snippets.

@JamesChevalier
Last active September 14, 2021 01:39
Show Gist options
  • Save JamesChevalier/0aa0133c7e41cb7581d0 to your computer and use it in GitHub Desktop.
Save JamesChevalier/0aa0133c7e41cb7581d0 to your computer and use it in GitHub Desktop.
How to recover from commits pushed to the wrong branch

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.

  1. 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 it placeholder, and click Create Branch
    • Push this to origin
  2. 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
  3. Reset master back to the placeholder branch
  4. Delete the placeholder branch
  5. 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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment