简体   繁体   中英

Git: ignore specific file(s) both on Commit and Pull

I am "ignoring" our ConnectionString.config using update-index --assume-unchanged, as I change it often during development and is generated automatically by our build server anyway; however, when performing a Pull/Rebase the operation fails because it would overwrite it locally, forcing me to stash it and then restore it.

Is there a way to ignore a file both on commit and on pull? I just want Git to leave it be.

If you want this to only happen on your local system, the local .git/info/exclude file should solve the problem.


To requote the answer from the above stack post:

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

The .git/info/exclude file has the same format as any .gitignore file. Another option is to set core.excludesFile to the name of a file containing global patterns.

Note, if you already have unstaged changes you must run the following after editing your ignore-patterns:

git update-index --assume-unchanged [<file>...]

Note on $GIT_DIR : This is a notation used all over the git manual simply to indicate the path to the git repository. If the environment variable is set, then it will override the location of whichever repo you're in, which probably isn't what you want.

You can try and use the assume-unchanged flag
https://git-scm.com/docs/git-update-index

--[no-]assume-unchanged

When this flag is specified, the object names recorded for the paths are not updated.

Instead, this option sets/unsets the "assume unchanged" bit for the paths.

When the "assume unchanged" bit is on, the user promises not to change the file and allows Git to assume that the working tree file matches what is recorded in the index. If you want to change the working tree file, you need to unset the bit to tell Git. This is sometimes helpful when working with a big project on a filesystem that has very slow lstat(2) system call (eg cifs).

Git will fail (gracefully) in case it needs to modify this file in the index eg when merging in a commit; thus, in case the assumed-untracked file is changed upstream, you will need to handle the situation manually.

在此输入图像描述

Is there any reason that file needs to be checked in to your git repository? If not, remove it from the repository and add it to .gitignore :

git rm filename.foo
echo "filename.foo" >> .gitignore
git add .gitignore
git commit

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM