Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to ignore any changes on already committed file to Git [duplicate]

Tags:

git

gitignore

I need to ignore any changes done to a particular files that are already tracked by Git. Those files should not be removed from either local or remote repository.

I tried to ignore them in .gitignore, but it does not work.

.gitignore:

$MAVEN_PATH_TO_CHILD_MODULE/src/main/java/$PACKAGE/$CLASS.java

Any idea why?

like image 281
Crazyjavahacking Avatar asked Feb 13 '15 10:02

Crazyjavahacking


People also ask

How do I ignore previous changes in Git?

Use Git rm to ignore changes Then, use a . gitignore or exclude file entry to prevent Git from reporting changes to the file.

How do I Untrack an already tracked file in Git?

Simply move the files to a folder outside of git, then do "git add .", "git commit". (This removed the files) then add the gitignore, referencing the files/folders, commit again to add the gitignore file to git, then copy/move back in the folders, and they should be ignored.

How do you exclude a file from a commit?

Set “–assume-unchanged” to a path to exclude to check on git commit and it will exclude your file from git commit. You will need to use the git update-index and –assume-unchanged to exclude files from git commit.


1 Answers

Only untracked files (not added to Git) can be ignored. Or in other words ignoring is about files which are not known to Git, i.e. are not in the index (have no entry in the staging area).

To make a file untracked (removing it from the index, and thus staging it from deletion in the next commit), but keep it in the working area i.e. in the filesystem, you can use

$ git rm --cached <file>

The file will be removed from remote repository (from the tree of top commit - it will be still present in history) after local commit, and after push to remote.


If you want for Git to ignore changes so that git diff and git status would not report them, and git commit -a would not commit them (but git add <file> + git commit and git commit <file> would still add them), you can lie to Git with this trick (see git ready » temporarily ignoring files article):

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

The git update-index command is low-level (plumbing) command. You can find which files are "assumed unchanged" with:

$ git ls-files -v
h <file>

git ls-files -v uses lowercase letters for denoting status files that are marked as assume unchanged.

You can turn it off with

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

Note that this option original purpose is speeding up status on platforms where stat is slow.


Following @Dave comment, and referenced Git - Difference Between 'assume-unchanged' and 'skip-worktree' question, another option in modern enough Git is making Git do not use working area version with skip-worktree bit.

You can turn it on for a tracked file with

$ git update-index --skip-worktree <file>

This makes Git use the index version (the last committed version) in place of the version from the filesystem. You can check which files have this bit set with

$ git ls-files -v
S <file>

The git ls-files -v command will use S to denote files with "skip-worktree" set.

Note that this option original purpose is support for sparse checkout.

like image 178
Jakub Narębski Avatar answered Oct 05 '22 16:10

Jakub Narębski