Ignore file changes with Git

VSTS | TFS 2018 | TFS 2017 | TFS 2015 | VS 2017 | VS 2015

Not every file created or updated in your code should be committed to Git. Temporary files from your development environment, test outputs and logs are all examples of files that you create but are not part of your codebase. Customize which files Git tracks through the gitignore feature.

In this tutorial you learn how to:

  • Use gitignore to prevent tracking or files
  • Ignore files only on your system
  • Ignore files across all repos on your system
  • Ignore changes to committed files

Use gitignore to prevent tracking of files

Create a .gitignore file in your Git repo to prevent Git from staging unwanted files. Share the .gitignore in the default branch in your repo, so you and your team can update it to change which files to ignore.

Create a .gitignore

Visual Studio automatically creates a .gitignore file in your repo when you create new repo for your project.

Git applies a .gitignore to the folder and any child folders where it's located. We strongly recommended to place your .gitignore in the root folder of your repo to prevent confusion.

Customize your .gitignore

Modify your .gitignore to include files types, paths, and file patterns in your repo. Git starts ignoring these files as soon as the .gitignore is updated, but be sure to commit the changes if others on your team need the same set of ignored files.

You can edit your .gitignore file for your repo by going to the Settings view in Team Explorer, then selecting Repository Settings. Select the Edit link under next to your .gitignore.

Find and open your .gitignore file for your repo in Visual Studio

Each line in the .gitignore excludes a file or set of files matching a pattern. The full gitignore syntax is very flexible. Here are some examples of the most common entries:

# ignore a single file
mycode.class

# ignore an entire directory
/mydebugdir/

# ignore a file type
*.json

# add an exception (using !) to the preceding rule to track a specific file
!package.json

Note

Windows users: All file paths in the .gitignore file use a forward slash separator and not a backslash.

Ignore files only on your system

Your .gitignore is shared across team members as a file committed and pushed to the Git repo. To exclude files only on your system without pushing the changes to the rest of your team, edit the .git/info/exclude file in your local repo. Changes to this file will not be shared with others and only apply to the files in that repo. The syntax for this file is the same as the one used in .gitignore.

Ignore files across all repos on your system

Set up a global .gitignore for use across all repos on your system using the command line git config tool:

> git config core.excludesfile C:\Users\frank\.gitignore_global

This is particularly useful for ignoring entire file types you don't want to ever commit, such as compiled binaries.

Ignore changes to committed files

Temporarily ignore changes

During development it's convenient to stop tracking file changes to a file committed into your git repo. This is very convenient when customizing settings or configuration files that are part of your project source for your own work environment.

> git update-index --assume-unchanged <file>

Resume tracking files with:

> git update-index --no-assume-unchanged <file>

Permanently ignore changes to a file

If a file is already tracked by Git, adding that file to your .gitignore is not enough to ignore changes to the file. You also need to remove the information about the file from Git's index:

These steps will not delete the file from your system. They just tell Git to ignore future updates to the file.

  1. Add the file in your .gitignore.

  2. Run the following:

    > git rm --cached <file>
    
  3. Commit the removal of the file and the updated .gitignore to your repo.

Next steps