100

I'm new to the Git environment, and I'm using BitBucket with SourceTree on Mac. All I want to do now is to discard the changes since last commit. How should I do this? I haven't found anything like "discard changes", and directly pulling from the last commit doesn't seem to work. Solutions done with either the GUI or command line will be good.

Tomer Shetah
  • 8,413
  • 7
  • 27
  • 35
goldfrapp04
  • 2,326
  • 7
  • 34
  • 46
  • you mean you want to undo the last commit? or leave it and checkout the previous commit for some testing reasons for example ? – Mohammad AbuShady Nov 27 '13 at 05:38
  • 1
    @MohammadAbuShady I guess the answer is neither. Since the last commit, I have made some changes that I want to discard. I want my last commit back. – goldfrapp04 Nov 27 '13 at 05:41
  • so you have uncommited changes that you want to revert ? – Mohammad AbuShady Nov 27 '13 at 05:42
  • None of the answers here helped me remove the annoying 'Uncommitted changes' entry at the top of my Sourcetree. And there were too many unstaged files to Discard one by one. The answer here worked: http://stackoverflow.com/questions/14075581/git-undo-all-uncommited-changes – PVS Aug 25 '15 at 13:45
  • I was confused between context menu options when right-clicking item in Unstaged Files list; `Discard` (Shift+Ctrl+R) and `Remove` (Ctrl+Del) I guess `Discard` will revert the changes like `git reset --hard` and `Remove` will delete the file and stage that deletion. – Nate Anderson Apr 27 '17 at 16:06

9 Answers9

152

On SourceTree for Mac, right click the files you want to discard (in the Files in the working tree list), and choose Reset.

On SourceTree for Windows, right click the files you want to discard (in the Working Copy Changes list), and choose Discard.

On git, you'd simply do:

git reset --hard to discard changes made to versioned files;

git clean -xdf to erase new (untracked) files, including ignored ones (the x option). d is to also remove untracked directories and f to force.

Roberto
  • 11,557
  • 16
  • 54
  • 68
  • 10
    You can also right click on the entire "Working Copy" in the left panel (or press Cmd+Shift+R) ;-) – Geo Apr 30 '14 at 15:46
  • 15
    Amazing that the same command has a different name for Mac and Windows. As if Git GUI (compared to command line) wasn't confusing enough already. – NSTJ May 20 '15 at 06:07
  • Thanks! I Think this should be Voted as the Right Answer ;-) – Hernan Arber Oct 13 '15 at 12:58
  • @NSTJ: Agreed ... it pretty well shakes one's confidence in the possibility of assigning a consistent definition to terms like "reset" and "discard" in regard to git tools. I.e. a definition that you can expect to be widely agreed upon. – LarsH May 19 '16 at 13:59
  • Be very careful about `git clean -xdf` command. It can do a lot of damage. – SMBiggs Mar 04 '21 at 22:12
66

I like to use

git stash

This stores all uncommitted changes in the stash. If you want to discard these changes later just git stash drop (or git stash pop to restore them).

Though this is technically not the "proper" way to discard changes (as other answers and comments have pointed out).

SourceTree: On the top bar click on icon 'Stash', type its name and create. Then in left vertical menu you can "show" all Stash and delete in right-click menu. There is probably no other way in ST to discard all files at once.

Kamil Kiełczewski
  • 85,173
  • 29
  • 368
  • 345
Max
  • 21,123
  • 5
  • 49
  • 71
  • 7
    well if you want to just discard every thing it's even easier to just do `git reset --hard HEAD` – Mohammad AbuShady Nov 28 '13 at 09:17
  • 4
    @cocoanut I think my answer "wrong" because this is an abuse of the `stash` function. Mohammad's answer is the proper way to discard all changes. – Max Sep 15 '14 at 16:12
  • Yes, and also because I couldn't see where's the shortcut. – Roberto Sep 16 '14 at 06:01
  • 3
    I do like this method as it gives you one last chance to restore your changes if you change your mind. I rarely `reset --hard`, but I often stash my changes to start fresh and later drop the stash if I decide I don't need it. – Max Sep 16 '14 at 14:12
  • This method did not delete new unversioned files. Roberto's method of using reset and clean worked better in that case. – Gabriel Jensen Oct 20 '14 at 20:17
  • @GabrielJensen git is not _supposed_ to care about unversioned files in most cases. `clean` is one of the few exceptions. – Max Oct 21 '14 at 19:11
  • you can use `git stash --include-untracked` to include new, untracked files in the stash – Luke Jan 27 '20 at 20:44
  • If you by some reason want to abandon all changes made to a file in your working directory or it has been accidently deleted. If the file has changes has been renamed or deleted it will show up in the 'Unstaged Files' list in SourceTree. If it has been deleted or renamed it will show up with a minus sign. To instantly restore the file to the last synced state, right click the file in the SourceTree unstaged list and select 'Discard'. Any changes should now be gone and if file was accidently deleted it should be back. – flodis Oct 13 '21 at 12:21
14

Follow steps to discard multiple uncommited changes in Sourcetree.

New version of Sourcetree does not have -Reset Button- as mentioned previous answer. Thus, please follow these 5 steps for solution.

  1. Right click "File status" and click "Reset...".

enter image description here

  1. Select files. If you want, you can select all of them like the below image.

enter image description here

  1. Click "Reset All".

enter image description here

  1. Again click "Reset All".

enter image description here

  1. Click "Reset".

enter image description here

Welldone..! No more 302 files to discard.

enter image description here

canerkaseler
  • 6,204
  • 45
  • 38
9

On the unstaged file, click on the three dots on the right side. Once you click it, a popover menu will appear where you can then Discard file.

NYC Tech Engineer
  • 1,845
  • 2
  • 22
  • 23
4

Ok I just noticed that my question was already answered in the question title.

To unstage files use

git reset HEAD /file/name

And to undo the changes to a file

git checkout -- /file/name

If you have a batch of files inside a folder you can undo the whole folder

git checkout -- /folder/name

Note that all these commands are already displayed when you git status

Here I created a dummy repo and listed all 3 possibilities

# On branch master
# Changes to be committed:
#   (use "git reset HEAD <file>..." to unstage)
#
#       modified:   test
#
# Changes not staged for commit:
#   (use "git add <file>..." to update what will be committed)
#   (use "git checkout -- <file>..." to discard changes in working directory)
#
#       modified:   test2
#
# Untracked files:
#   (use "git add <file>..." to include in what will be committed)
#
#       test3
Mohammad AbuShady
  • 40,884
  • 11
  • 78
  • 89
4

Ok so in Windows sourcetree that is simple, on macOS I looked as well for a while..

Click Command + Shift + R while in source tree a hidden popup will be shown that will let you discard individual files OR ALL! Why is this hidden? We will never know.. but it works!

enter image description here

Tomer Shetah
  • 8,413
  • 7
  • 27
  • 35
ItaiRoded
  • 142
  • 7
3

From sourcetree gui click on working directoy, right-click the file(s) that you want to discard, then click on Discard

giuse
  • 337
  • 1
  • 2
  • 12
1

Do as follow,

  • Click on commit
  • Select all by pressing CMD+A that you want to delete or discard
  • Right click on the selected uncommitted files that you want to delete
  • Select Remove from the drop-down list
Frostmourne
  • 156
  • 1
  • 19
0

It's Ctrl + Shift + r

For me, there was only one option to discard all.

enter image description here

Pang
  • 9,564
  • 146
  • 81
  • 122
Sharutkarsh
  • 17
  • 2
  • 8