Git: Ignoring Version-Controlled Files
Asked Answered
I

6

59

The .gitignore file is very useful in ignoring some of the files that we don't want to control. Unfortunately, it cannot be used when the file is already under version control. For example, my .gitignore (which is already added to git) file might be different than what my coworker wants it to be (e.g. I want to ignore Vim files). Whenever I make changes to this file, git shows it as a modified file. So my questions:

  1. Is there any way to ignore changes for a certain file, which is already controlled by Git?!
  2. Is there any way to commit these changes, but keep it for myself only? Obviously, I don't want to use a branch, because I am working on a certain branch.
Investigate answered 8/1, 2011 at 11:41 Comment(2)
None of the answers answer the main question - number 1, including the accepted one. The question was about ignoring files that are already controlled, not .gitignore not exlude will do the jobDeem
The git-update-index answer is the correct answer.Preferable
S
17

If you want to exclude files that are specific to your process (such as Vim temporary files), edit the (local) file .git/info/exclude and add your exclusion patterns there. This file is designed for developer-specific exclusions rather than .gitignore, which is designed for project-wide exclusions.

The short summary is, everybody should agree on what is added to .gitignore. For files where you don't agree, use .git/info/exclude.

Salmon answered 8/1, 2011 at 12:30 Comment(2)
I added version controlled files to .get/info/exclude but they continued to appear as modified files.Cotonou
The original question is looking for a way for different coworkers to ignore different sets of files. My answer introduces the .git/info/exclude file which is designed for this purpose. However, if you generalise the question into "how do I ignore changes to any file that is already added to Git", then yes, you need a different solution because that's a different problem. I wouldn't recommend the general solution for this specific problem, however, because coworkers can then no longer share changes to .gitignore.Salmon
T
135

Use git-update-index to temporarily ignore changes to files that are already under version control:

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

To undo that use:

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

Also have a look at the skip-worktree and no-skip-worktree options for update-index if you need this to persist past a git-reset

Twibill answered 8/1, 2011 at 12:11 Comment(0)
S
17

If you want to exclude files that are specific to your process (such as Vim temporary files), edit the (local) file .git/info/exclude and add your exclusion patterns there. This file is designed for developer-specific exclusions rather than .gitignore, which is designed for project-wide exclusions.

The short summary is, everybody should agree on what is added to .gitignore. For files where you don't agree, use .git/info/exclude.

Salmon answered 8/1, 2011 at 12:30 Comment(2)
I added version controlled files to .get/info/exclude but they continued to appear as modified files.Cotonou
The original question is looking for a way for different coworkers to ignore different sets of files. My answer introduces the .git/info/exclude file which is designed for this purpose. However, if you generalise the question into "how do I ignore changes to any file that is already added to Git", then yes, you need a different solution because that's a different problem. I wouldn't recommend the general solution for this specific problem, however, because coworkers can then no longer share changes to .gitignore.Salmon
N
12

you can use this command to get you want.

git rm --cached path/to/file

or

git rm -r --cached path/ignore/dir

This will only remove the track from git, will not delete the real files.

Then you can edit the ignore file to untrack these files or dirs.

Negotiate answered 3/8, 2015 at 14:52 Comment(1)
OP is not asking how to remove files that are wrongly versioned, but how to keep them versioned and ignore them when checking git status.Fallow
T
2

I cannot really answer the general question (having Git ignore tracked files) - it strikes me as a feature that would be much more detrimental than useful.

However, the gitignore manual page specifies a few ways to configure patterns for excluded files.

In particular, it gives explicit instructions on how to use these various ways:

  • Patterns which should be version-controlled and distributed to other repositories via clone (i.e., files that all developers will want to ignore) should go into a .gitignore file.

Meaning that your .gitignore file should not be different from your coworkers - it is working as intended.

  • Patterns which are specific to a particular repository but which do not need to be shared with other related repositories (e.g., auxiliary files that live inside the repository but are specific to one user’s workflow) should go into the $GIT_DIR/info/exclude file.

  • Patterns which a user wants git to ignore in all situations (e.g., backup or temporary files generated by the user’s editor of choice) generally go into a file specified by core.excludesfile in the user’s ~/.gitconfig.

There you have it. Specify a core.excludesfile file path into your ~/.gitconfig file, and then put into it the patterns that you want to exclude.

Transpacific answered 8/1, 2011 at 11:52 Comment(0)
K
1

I've written about three ways of excluding files elsewhere.

In summary:

  1. A global git ignore file applies to all repositories on that system
  2. The .gitignore file in the repository applies the the repository and all clones of that repository.
  3. The .git/info/exclude file applies only to that repository.

The lower items in the list have priority over the higher items, and a ! in front of an item in any of the patterns in the file reverses a previous exclusion.

This paradigm is seen elsewhere in Git. For example, if you were using submodules, the url to the submodule to use is in the the .gitmodules file in the repository, but you can over-ride the url to use in the .git/config file.

Kantianism answered 8/1, 2011 at 12:14 Comment(0)
A
0

Here is a generalized, ubiquitos (not individual) solution for those using Git under SmartGitHG viusal interface:

  • choose a folder you want to ignore in "Repositories" window;
  • select all the files from that folder in "Files" window (unhide unchanged files by clicking the respective filter icon in the Right Top region of the main window);
  • click "Remove" on control panel (check the "Delete local files" check box if you need it);
  • commit local changes;
  • rightclick on the folder in "Repositories" window;
  • click "Ignore".
Anastasiaanastasie answered 9/10, 2014 at 19:28 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.