Can I ignore files locally without polluting the global git config for everyone else? I have untracked files that are spam in my git status but I don't want to commit git config changes for every single little random untracked file I have in my local branches.
From the relevant Git documentation:
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.
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-list>
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.
Edit: Another way is to use:
git update-index --skip-worktree <file-list>
Reverse it by:
git update-index --no-skip-worktree <file-list>
Answered 2023-09-21 08:11:16
git update-index --assume-unchanged [<file>...]
after making the addition to the exclude file. The changes won't be picked up until then. - anyone skip-worktree
would likely be preferred to assume-unchanged
. - anyone Update: Consider using git update-index --skip-worktree [<file>...]
instead, thanks @danShumway! See Borealid's explanation on the difference of the two options.
Old answer:
If you need to ignore local changes to tracked files (we have that with local modifications to config files), use git update-index --assume-unchanged [<file>...]
.
Answered 2023-09-21 08:11:16
git update-index --assume-unchanged my-file.php
for it to start being ignored. Thanks for the tip! - anyone git update-index --no-assume-unchanged my-file.php
- anyone .git/info/exclude
is what you want (to avoid polluting the often shared and tracked .gitignore) - anyone --assume-unchanged
before reading your update. I undid with --no-assume-unchanged
and then did the --skip-worktree
... Am I in the clear? - anyone Add the following lines to the [alias] section of your .gitconfig file
ignore = update-index --assume-unchanged
unignore = update-index --no-assume-unchanged
ignored = !git ls-files -v | grep "^[[:lower:]]"
Now you can use git ignore my_file
to ignore changes to the local file, and git unignore my_file
to stop ignoring the changes. git ignored
lists the ignored files.
This answer was gleaned from http://gitready.com/intermediate/2009/02/18/temporarily-ignoring-files.html.
Answered 2023-09-21 08:11:16
You have several options:
.gitignore
file in your working dir (or apply it automatically using topgit or some other such patch tool).$GIT_DIR/info/exclude
file, if this is specific to one tree.git config --global core.excludesfile ~/.gitignore
and add patterns to your ~/.gitignore
. This option applies if you want to ignore certain patterns across all trees. I use this for .pyc
and .pyo
files, for example.Also, make sure you are using patterns and not explicitly enumerating files, when applicable.
Answered 2023-09-21 08:11:16
git config --global
to set the option globally. - anyone I think you are looking for:
git update-index --skip-worktree FILENAME
which ignore changes made local
Here's http://devblog.avdi.org/2011/05/20/keep-local-modifications-in-git-tracked-files/ more explanation about these solution!
to undo use:
git update-index --no-skip-worktree FILENAME
Answered 2023-09-21 08:11:16
--skip-worktree
and --assume-unchanged
at this SO question - anyone --skipworktree
, should I later change my mind and want to start tracking the file again? - anyone git update-index --no-skip-worktree <file>
- anyone git ls-files -v | grep ^S
- anyone You can simply add a .gitignore file to your home directory, i.e. $HOME/.gitignore
or ~/.gitignore
. Then tell git to use that file with the command:
git config --global core.excludesfile ~/.gitignore
This is a normal .gitignore file which git references when deciding what to ignore. Since it's in your home directory, it applies only to you and doesn't pollute any project .gitignore files.
I've been using this approach for years with great results.
Answered 2023-09-21 08:11:16
git check-ignore <file-name>
to verify. LMK if it works for you - anyone =
: git config --global core.excludesfile ~/.gitignore
- anyone .gitignore
file under home directory and told git to use that file instead, then deleted the files I would like to untrack locally. However, after I push the changes, the remote repository also deleted those files. Did I do something wrong? - anyone .gitignore
is about ignoring files that exist in your local directory. What you should have done is git rm --cached
which removes them from the repo but leaves them in your local. You should be able to go back to your previous commit with something like git reset --soft HEAD^
to undo that commit and recover your files. That's the beauty of git: it's all still there in your git history. - anyone You can install some git aliases to make this process simpler. This edits the [alias]
node of your .gitconfig
file.
git config --global alias.ignore 'update-index --skip-worktree'
git config --global alias.unignore 'update-index --no-skip-worktree'
git config --global alias.ignored '!git ls-files -v | grep "^S"'
The shortcuts this installs for you are as follows:
git ignore config.xml
config.xml
— preventing you from accidentally committing those changes.git unignore config.xml
config.xml
— allowing you again to commit those changes.git ignored
I built these by referring to phatmann's answer — which presents an --assume-unchanged
version of the same.
The version I present uses --skip-worktree
for ignoring local changes. See Borealid's answer for a full explanation of the difference, but essentially --skip-worktree
's purpose is for developers to change files without the risk of committing their changes.
The git ignored
command presented here uses git ls-files -v
, and filters the list to show just those entries beginning with the S
tag. The S
tag denotes a file whose status is "skip worktree". For a full list of the file statuses shown by git ls-files
: see the documentation for the -t
option on git ls-files
.
Answered 2023-09-21 08:11:16
'!git ls-files -v | grep "^S"'
? The command doesn't work for me with it on there.. and seems to work fine with it removed. - anyone Expansion of alias 'ignored' failed; 'git' is not a git command
. This makes sense; without the exclamation mark: git aliases your command to git git ls-files …
. - anyone git ignore <filename>
, it only applies to that directory. And when you finally want to make your changes to that file and commit and push to remote, just use the handy git unignore <filename>
to temporarily start tracking it again! Thanks! - anyone This is a brief one-line solution to exclude a local file.
echo YOUR_FILE_OR_DIRECTORY >> .git/info/exclude
based on @Vanduc1102 comment. if it didn't applied run the following commend after that.
git update-index --assume-unchanged YOUR_FILE_OR_DIRECTORY
Answered 2023-09-21 08:11:16
git update-index --assume-unchanged YOUR_FILE_OR_DIRECTORY
after the command - anyone Both --assume-unchanged and --skip-worktree are NOT A CORRECT WAY to ignore files locally... Kindly check this answer and the notes in the documentation of git update-index. Files that for any reason keep changing frequently (and/or change from a clone to another) and their changes should not be committed, then these files SHOULD NOT be tracked in the first place.
However, the are two proper ways to ignore files locally (both work with untracked files). Either to put files names in .git/info/exclude file which is the local alternative of .gitignore but specific to the current clone. Or to use a global .gitignore (which should be properly used only for common auxiliary files e.g. pyz, pycache, etc) and the file will be ignored in any git repo in your machine.
To make the above as kind of automated (adding to exclude or global .gitignore), you can use the following commands (add to your bash-profile):
.git/info/exclude
~/.gitignore
Linux
alias git-ignore='echo $1 >> ##FILE-NAME##'
alias git-show-ignored='cat ##FILE-NAME##'
git-unignore(){
GITFILETOUNIGNORE=${1//\//\\\/}
sed -i "/$GITFILETOUNIGNORE/d" ##FILE-NAME##
unset GITFILETOUNIGNORE
}
MacOS (you need the .bak for sed
inplace modifications (i.e. you are forced to add a file extension to inplace sed. i.e. make a backup before replacing something), therefore to delete the .bak file I added rm filename.bak)
alias git-ignore='echo $1 >> ##FILE-NAME##'
alias git-show-ignored='cat ##FILE-NAME##'
git-unignore(){
GITFILETOUNIGNORE=${1//\//\\\/}
sed -i.bak "/$GITFILETOUNIGNORE/d" ##FILE-NAME##
rm ##FILE-NAME##.bak
unset GITFILETOUNIGNORE
}
Then you can do:
git-ignore example_file.txt
git-unignore example_file.txt
Answered 2023-09-21 08:11:16
In order to ignore untracked files especially if they are located in (a few) folders that are not tracked, a simple solution is to add a .gitignore
file to every untracked folder and enter in a single line containing *
followed by a new line. It's a really simple and straightforward solution if the untracked files are in a few folders. For me, all files were coming from a single untracked folder vendor
and the above just worked.
Answered 2023-09-21 08:11:16
Just Simply add path to the file ypu want to remove from commits on any branch of current repo:
And thats it ! For me answears above are too long. KISS - Keep it stupid simple
Answered 2023-09-21 08:11:16
For anyone who wants to ignore files locally in a submodule:
Edit my-parent-repo/.git/modules/my-submodule/info/exclude
with the same format as a .gitignore
file.
Answered 2023-09-21 08:11:16
TL;DR - filter the output to ignore the files
Don't do it. There are excellent answers on the technical ways to do this. However, I have found this Q/A page several times. As you can see there are multiple solutions. The problem is that each will make git ignore the file; which might be what you want now, but maybe not in the future.
The devil in the details are,
git
will no longer report the files.Either add it to .gitignore or write a filter to git status
that will actively filter out the text. Each will ignore the file, but the answer of why it is being ignored is more obvious. The 2nd case of a wrapper script (or command line recall) makes it abundantly apparent what you are choosing to ignore and that your workflow is non-standard.
Answered 2023-09-21 08:11:16
If your repo doesn't already have a .gitignore file, then a simple solution is to create a .gitignore file, and in it add .gitignore
to the list of files to be ignored.
Answered 2023-09-21 08:11:16
.gitignore
file at the top. But my dirty files are in a deep folder, so I just added my own .gitignore
next to them. +1 - anyone