Sourcetree - undo unpushed commits
Version ControlAtlassian SourcetreeVersion Control Problem Overview
I am using Sourcetree for Windows for a git-repository and would like to undo an unpushed commit.
Is that possible? If I do "revert commit", it creates a second commit which reverts the first commit, but I don't want the first commit to appear at all in my source control.
I could also delete my local repository and pull it again without my local commit, but maybe there's another way?
Version Control Solutions
Solution 1 - Version Control
- Right click on the commit you like to reset to (not the one you like to delete!)
- Select "Reset master to this commit"
- Select "Soft" reset.
A soft reset will keep your local changes.
Source: https://answers.atlassian.com/questions/153791/how-should-i-remove-push-commit-from-sourcetree
Edit
About git revert
: This command creates a new commit which will undo other commits. E.g. if you have a commit which adds a new file, git revert
could be used to make a commit which will delete the new file.
About applying a soft reset: Assume you have the commits A
to E
(A---B---C---D---E
) and you like to delete the last commit (E
). Then you can do a soft reset to commit D
. With a soft reset commit E
will be deleted from git but the local changes will be kept. There are more examples in the git reset documentation.
Solution 2 - Version Control
If you select the log entry to which you want to revert to then you can click on "Reset
Solution 3 - Version Control
If you want to delete a commit you can do it as part of an interactive rebase. But do it with caution, so you don't end up messing up your repo.
In Sourcetree:
- Right click a commit that's older than the one you want to delete, and choose "Rebase children of xxxx interactively...". The one you click will be your "base" and you can make changes to every commit made after that one.
- In the new window, select the commit you want gone, and press the "Delete"-button at the bottom, or right click the commit and click "Delete commit".
- List item
- Click "OK" (or "Cancel" if you want to abort).
Check out this Atlassian blog post for more on interactive rebasing in Sourcetree.
Solution 4 - Version Control
If You are on another branch, You need first "check to this commit" for commit you want to delete, and only then "reset current branch to this commit" choosing previous wright commit, will work.