11

I'm trying to get started using Git and TortoiseGit.

Is there a way to hide files that should never be tracked completely? Currently, all temporary build files are in the same "Not Versioned" list as new files when I commit a change.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
peterchen
  • 40,917
  • 20
  • 104
  • 186
  • This can be done directly in ***TortoiseGit***! See [linquize's answer](https://stackoverflow.com/questions/1248827/git-hide-remove-files-never-to-be-versioned/12292110#12292110). – Peter Mortensen Aug 05 '19 at 16:37
  • Consider changing the accepted answer to [linquize's answer](https://stackoverflow.com/questions/1248827/git-hide-remove-files-never-to-be-versioned/12292110#12292110), as this question is somewhat specific to TortoiseGit and less about the general Git method. – Peter Mortensen Sep 09 '19 at 00:32

5 Answers5

25

Create a text file called .gitignore in your root folder and add lines like the following to exclude files:

*.obj
test.c

Then add .gitignore to your Git repository and commit:

$ git add .gitignore
$ git commit .gitignore
Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
lemonad
  • 4,148
  • 26
  • 27
  • 2
    Thanks! As a side note, the paths can be selected in tortoise-git's commit window, and be copied to lcipboard, which makes it easy t obase them into the .gitignore – peterchen Aug 10 '09 at 12:42
  • Yes, but it may be easier to use [the built-in functionality in TortoiseGit](https://stackoverflow.com/questions/1248827/git-hide-remove-files-never-to-be-versioned/12292110#12292110) (in another answer here). – Peter Mortensen Aug 05 '19 at 16:29
10

You need to investigate .gitignore files.

git help gitignore
CB Bailey
  • 755,051
  • 104
  • 632
  • 656
9

TortoiseGit can add files to the ignore list with many options.

Right click on a file in a repository → TortoiseGitAdd to ignore list → by filename / by extension

A dialog shows to choose options:

Ignore type

This file only (match absolute name),

Recursive (match same name)

Ignore file

.gitignore in root directory (need to commit)

.gitignore in included directory (need to commit)

.git/info/exclude (like a configuration file, stored locally)

See also: Manual entry, Chapter 2. TortoiseGit Daily Use Guide, Ignoring Files And Directories.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
linquize
  • 19,828
  • 10
  • 59
  • 83
5

In case you have files already added to the repository, but need to remove/ignore them, follow these steps:

  1. Move those files somewhere out of the repository.
  2. Make a commit, which would delete those files from the repository
  3. Move those files back to the repository to the place where they have been
  4. Make a commit, but now instead of adding those files to the repository, right click on those files. Select Add to ignore listBy file name. The option to which .gitignore you add those is irrelevant, but you can look up the difference on the Internet.
Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Alexey Vassiliev
  • 2,349
  • 2
  • 17
  • 8
5

Since one of the benefits of TortoiseGit is to use the UI, especially if you have used other products like TortoiseSVN and TortoiseHg, here are some UI ways to achieve this:

File not already added

An easy way to add files to the .gitignore exclusions is when you are selecting files to be staged when you do a git commit through Tortoise - Tortoise will list all non-ignored files which aren't currently in your repository under the Not Versioned Files box at the bottom of the commit screen:

Adding file exclusions to .gitignore during commit

Unwanted file already added by mistake

If you've already committed or pushed the undesirable file, another way in newer (circa 1.8.14) versions of TortoiseGit to remove an existing file from your repository branch and add it to one of the .gitignores is hidden under the right mouse function Delete and add to ignore list:

Removing versioned file from repository and adding to GitIgnore

You then have several .gitignore options to elect, either on whether to add just this file or wildcard matches, and to which .gitignore to add it to - local folder, repository root, or your `.git/info/exclude'.

However, right at the last minute, you are given the opportunity to keep the local copy of the file:

Deleted from the repository, but kept locally

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
StuartLC
  • 104,537
  • 17
  • 209
  • 285