38

I wanted to know what the difference is between discarding a file and and stop tracking a file in git using source-tree. If I deleted a file in my updated code and I want that file deleted on the repository too should I mark it as stop tracking or should I discard it during the commit process

Clay Nichols
  • 11,848
  • 30
  • 109
  • 170
MistyD
  • 16,373
  • 40
  • 138
  • 240

2 Answers2

66

The one detail not covered by the first answer is that Stop Tracking does not delete the local file. It only removes it from source control.

  1. If you want to keep the file locally (maybe it's a .suo file storing Visual Studio settings that you decide shouldn't have been in source control) but remove it from the repository, you should use Stop Tracking. After this, if you see the file listed in Unstaged files, you can use right-click > Ignore... in SourceTree to update your .gitignore file to ignore the file in future.
  2. If you want to delete your file both locally and in the repository, you should use Remove. (to use the SourceTree term)
  3. Finally, if you only want to revert your local changes without
    changing the repository at all, use Discard.
Ken Lyon
  • 1,230
  • 10
  • 10
  • For files created by the build env (e.g. gradlew.bat), ignore is greyed out. Remove an stop tracking ar options. The file is shown with a yellowish colour. Any ideas? If I do Stop tracking, it comes up in red. – John Little Mar 27 '19 at 13:42
  • What does reset do? – John Little Mar 27 '19 at 13:44
  • @john-little Is it possible that you staged the addition of gradlew.bat? I found that ignore is disabled when you have staged a change, but if you unstage you can then ignore. Alternatively, is the file already under source control? If it appears yellow, that would suggest it's a change, not an addition. – Ken Lyon Apr 08 '19 at 17:19
  • @john-little Regarding your question about "reset", are you referring to an action in SourceTree? Or are you referring to the "git reset" command? If so, it is fully documented [here](https://git-scm.com/docs/git-reset). – Ken Lyon Apr 08 '19 at 17:23
  • I tried "Stop tracking" and noticed that selected files where moved to stage area, as a normal file I'd like to commit. what should I do, then, commit files moved to stage area? If I commit, will such action have effect on remote repository or will it influence only my local version of files? If I commit, should I push too? – Bia Sep 11 '20 at 11:18
  • @KenLyon is there a way to track the file again? I have tried `git add *` in its directory but it still was being tracked. The .gitignore file also doesn't list the file. How do I track the file again? – Justin Dec 15 '21 at 17:19
36

In SourceTree, selecting "discard" on a file just throws away your local changes; stop tracking removes it from the repository. However, as long as you have deleted the file on your local drive, and you can see that deletion in the "Staged Files" section of SourceTree, it will be deleted in the repository as well when you commit.

David Deutsch
  • 17,443
  • 4
  • 47
  • 54
  • 1
    How do you do the equivalent from cmd prompt? SourceTree did the job while Visual Studio and git status both acted like .sln file was modified and it was indeed not - so this "Discard" seems to have been the trick. – Tom Stickel Jan 13 '17 at 06:59
  • In the most recent version of Sourcetree **Discard** option was renamed to **Reset** – Legonaftik Oct 12 '17 at 09:25
  • 1
    @TomStickel - the command line equivalent of SourceTree's "Discard" for a file is `git checkout path/to/some/file.sln`. You've probably already discovered this in the 4+ years since you asked, but I figured I'd answer just in case :) – David Deutsch Mar 16 '21 at 16:20