SourceTree - how to revert changes made since last commit
Asked by Matt Perdeck:

I want to do a "checkout --" on a number of files in SourceTree (that is, discard all changes since the last commit).

So I selected those files in SourceTree under Working Copy Changes and right clicked on the selection, hoping to see a menu item that would let me discard the changes.

However, it gives me two scary looking options: Remove and Discard. Don't want to delete those files by mistake.

1)

Which of these options does a "checkout --" ?

If neither does this, how do I do a "checkout --" in SourceTree on a number of files?

2)

Is there any documentation where I could have found the answer to this question?


Answers:
author: stevestreeting
body: You're right not to use Remove/Discard:

* Remove = remove the file from disk and stop tracking it in the repo from next commit
* Discard = discard your current uncommitted changes

If you want to return the file to a previous state, there are a number of ways to do this.

Option 1:

1. Right-click the file (in any view) and pick 'Log Selected'. This gives you a history just of that file.
2. Pick the commit in the list which represents the state at which you want the file to be returned to. So this would be the commit \*before\* the changes you wanted to reverse
3. Right-click this commit and select "Reset To Commit"

Note, you can also do this in the main log view (right-click the file in a commit and pick "Reset To Commit", but it's easier to find the commit you want when you use the file-specific log.

Option 2:

You can Reverse file changes made to a file in a commit, undoing them in your working copy. This is more selective than resetting the entire file back to a previous point.

1. Pick a commit in the log view
2. Select the file in that commit whose changes you want to reverse
3. Use the 'Reverse File' button at the top to reverse all the file changes, or use 'Reverse Hunk' or 'Reverse Selected Lines' to undo just portions of that change.
4. These changes will appear in your working copy ready to be tested/committed

The 'Reverse' buttons are available in the Log Selected single-file window too. Please note that this 'Reverse' feature actually applies an 'inverse patch' to your working copy, so although in theory you can reverse any previous change at any level of detail, the more the file has changed content since the commit you're trying to reverse, the more chance the patch won't apply any more and you'll have to back the changes out manually instead, or use the more severe Option 1).

comments: author: Matt Perdeck
body: I'm a bit confused between "Discard" (discard uncommittted changes) and "reset to commit". Sounds to me they do the same if you reset to the last commit.

Take these events:

1) make change A to a file

2) make change B to the same file

3) locally commit the file

4) make change C to the file

If I now right click on the file and choose Discard, will that essentially get rid of change C (so it is back to where it was when I committed it locally in step 3)?

Reason I'm asking:

Sometimes I have a number of files that I've messed with, and where I want to get rid of my uncommitted changes before committing. Would be very handy if I could select them all and get rid of the uncommitted changes by right clicking the entire selection and choosing "Discard", instead of having to do this one by one.


author: stevestreeting
body: Yes, if you select a file in the previous commit and choose Reset to this Commit it's the same as Discard. But discard is quicker and more accessible (you can even do it per hunk/line).

You can already discard many files at once. Multi-select them in the working copy (selecting either the File Status View with Ctrl-1 or the Uncommitted Changes line in the log view), then right-click and select Discard.

Alternatively, click Discard on the toolbar and then check the files you want to discard. Or, if you want to quick-discard absolutely everything, use Discard from the toolbar then select the Reset All tab and click OK.


author: Charles Robertson
body: From my understanding, they are not the same at all.Discard merely removes the file from being tracked for the latest changes. I use it to tidy up my main window. Resetting a commit presumably actually changes the content of a file back to a previous version.


author: John Rusk
body: I found this comment thread confusing because the above two comments seem to contradict each other. As far as I can tell Steve is right. I.e. "Reset to \[most recent\] Commit" *is* the same as Discard. But I'm new to SourceTree. Can someone who knows please confirm?

(This thread seems to be the best resource on this topic, but it will be much *better* with a definitive resolution to the contradiction.)


author: JBNZ123
body: I, too, was looking to find out how to 'Undo' a change in SourceTree and came across this thread. I tried the 'Reset to Commit' command and whilst this did reverse the change, it left the file listed in the 'Unstaged files' area. Yet when I 'Discarded' the file, it disappeared from the 'Unstaged' area which is the behaviour I would expect when 'Undoing' a change.

Perhaps the difference is that with 'Reset to Commit' you can choose a Commit - it doesn't necessarily have to be the previous Commit and thus the file may still be changed/different from the latest Commit.

Sooo, if you simply want to 'Undo' current changes and go back to the latest 'Committed' version use 'Discard'

But if you want to go back further use 'Reset to Commit'?

Dunno, just my thoughts on my observations on SourceTree.


author: John Rusk
body: I checked with Atlassian. Steve's comments are indeed correct. Don't just read his main answer, but also his comment that follows his answer.


author: Matt Perdeck
body: This is for Windows.


author: GameBX
body: Thank you  
I get something quite interesting

**GamesBX**


author: itzrahulsoni
body: Check this article for more options: <https://www.attosol.com/undo-in-git-using-sourcetree/>


author: KieranA
body: Hi Matt, could you just clarify whether this is for Windows or Mac?