This method is based on history deletion:
- Check out the branch you made the mistake on
- Right click on the commit you want to reset the branch to
- Click "Reset current branch to this commit"
- Select "Hard" mode and click "OK"
- Unfortunately you need terminal to do this bit. Type
git push origin name_of_branch --force
into terminal (you may need to enter your git repo username and password for it to accept the command)
Update: The current version of source tree supports forced pushes. That means you don't need to use the command mentioned in step 5! There is a check box visible when pushing that is labled "force". It is usually disabled.
How to enabled the force push checkbox:
- "Tools" (in the blue bar at the top of the screen)
- "Options"
- "Git" tab
- "Enable Force Push" (2nd section)
- "OK"
Check that checkbox when pushing to also fix the mistake on the origin server.
This is probably the easiest way to do it but since it is based on history deletion, If others are working on the project, make sure to let them all know what you are doing so you don't break anyone's git repository.
Update 2: If you use Visual Studio Code as your code editor, I highly recommend installing the "Git Graph" extension. It allows you to do practically everything that Source Tree allows you to do, but you can do it from directly inside your code editor! The steps for doing this using Git Graph are roughly the same as the steps for doing this in Source Tree.
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…