272

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?

StayOnTarget
  • 11,743
  • 10
  • 52
  • 81
deimos1988
  • 5,896
  • 7
  • 41
  • 57
  • 2
    I have written a detailed article with screenshots. Sharing in hope that it would help: https://www.attosol.com/undo-in-git-using-sourcetree/ – Rahul Soni Jan 22 '18 at 05:41

4 Answers4

432
  1. Right click on the commit you like to reset to (not the one you like to delete!)
  2. Select "Reset master to this commit"
  3. 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.

nightlyop
  • 7,675
  • 5
  • 27
  • 36
  • 44
    Or if you're working on a branch - "Reset current branch to this commit" – Tom Auger Aug 12 '15 at 13:26
  • 5
    I so much don't understand these UI explanations... Reset current branch to this commit so much doesn't sound like it will delete the commit. And it ended up doing what you would expect more. It reset my working copy to the commit, instead of deleting it. – MrFox Nov 24 '15 at 13:32
  • 1
    I agree with @MrFox. After I did this the commit I don't want is still shown in the tree. – Timmmm Jan 11 '16 at 11:09
  • @Timmmm: Does the new explanation help? – nightlyop Jan 11 '16 at 12:29
  • Actually I realised that I had already pushed my commit to github. I found [an article that explains how to "rewrite history"](http://christoph.ruegg.name/blog/git-howto-revert-a-commit-already-pushed-to-a-remote-reposit.html) but it seems like it is more trouble than it is worth! – Timmmm Jan 11 '16 at 15:40
  • Do we also need to "unstage" files after that? – Winand Aug 12 '18 at 10:03
  • @Winand: The staged files are the files which are included when comitting (https://githowto.com/staging_changes). Hence, you can have modified files which are not included when commiting. If you unstage the files, you have to stage the file with `git add file.xyz` to include it in the next commit. – nightlyop Aug 15 '18 at 09:15
58

If you select the log entry to which you want to revert to then you can click on "Reset to this commit". Only use this option if you didn't push the reverse commit changes. If you're worried about losing the changes then you can use the soft mode which will leave a set of uncommitted changes (what you just changed). Using the mixed resets the working copy but keeps those changes, and a hard will just get rid of the changes entirely. Here's some screenshots:

enter image description here

Muhammad Awais
  • 4,238
  • 1
  • 42
  • 37
  • 5
    If you still see a icon showing a Push is available from your previous commit (Which you just wiped out), pull and Sourcetree seems to update the UI. – ChristoKiwi Nov 11 '16 at 04:56
11

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:

  1. 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.

Screenshot-1

  1. 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".
  2. List item
  3. Click "OK" (or "Cancel" if you want to abort).

Check out this Atlassian blog post for more on interactive rebasing in Sourcetree.

Jayprakash Dubey
  • 35,723
  • 18
  • 170
  • 177
jna
  • 926
  • 10
  • 18
0

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.

pomaranga
  • 1
  • 2