I have a bunch of files in a changeset, but I want to specifically ignore a single modified file. Looks like this after git status
:
# modified:
You can try this: git add * && git reset main/dontcheckmein.txt
To keep the change in file but not to commit I did this
git add .
git reset -- main/dontcheckmein.txt
git commit -m "commit message"
to verify the file is excluded do
git status
git add .
git reset main/dontcheckmein.txt
1) To start ignoring changes to a single already versioned file
git update-index --assume-unchanged "main/dontcheckmein.txt"
and to undo that git update-index --no-assume-unchanged "main/dontcheckmein.txt"
github docs to ignore files
2) To completely ignore a specific single file preventing it from being created at repository
First, look at this stackoverflow post: Git global ignore not working
In .gitignore
, add the relative path to the file without leading ./
.
So, if your file is at MyProject/MyFolder/myfile.txt
, (where .git
is also in the MyProject
folder), add MyFolder/myfile.txt
to your at .gitignore
file.
You can confirm what rules are associated with ignore via git check-ignore "MyFolder/myfile.txt"
About global ignore
That link talks about ~/.gitignore_global
, but the file is related to your project. So, if you put the exclude pattern MyFolder/myfile.txt
in ~/.gitignore_global
, it will work but will not make much sense...
On the other hand, if you setup your project with git config core.excludesfile .gitignore
where .gitignore
is in MyProject
, the local file will override ~/.gitignore_global
, which can have very useful rules...
So, for now, I think it's best to make some script to mix your .gitignore
with ~/.gitignore_global
at .gitignore
.
One last warning
If the file you want to ignore is already in the repository, this method will not work unless you do this: git rm "MyFolder/myfile.txt"
, but back it up first, as it will be removed locally also! You can copy it back later...
Changes to be committed: (use "git reset HEAD ..." to unstage)
For the specific case in the question, easiest way would be to add all files with .c extension and leave out everything else:
git add *.c
From git-scm (or/and man git add
):
git add <pathspec>…
Files to add content from. Fileglobs (e.g. *.c) can be given to add all matching files. <...>
Note that this means that you could also do something like:
git add **/main/*
to add all files (that are not ignored) that are in the main
folder. You can even go wild with more elaborate patterns:
git add **/s?c/*Service*
The above will add all files that are in s(any char)c
folder and have Service
somewhere in their filename.
Obviously, you are not limited to one pattern per command. That is, you could ask git to add all files that have an extension of .c and .h:
git add *.c *.h
This link might give you some more glob pattern ideas.
I find it particularly useful when I'm making many changes, but still want my commits to stay atomic and reflect gradual process rather than a hodgepodge of changes I may be working at the time. Of course, at some point the cost of coming up with elaborate patterns outweighs the cost of adding files with simpler methods, or even one file at a time. However, most of the time I'm easily able to pinpoint just the files I need with a simple pattern, and exclude everything else.
By the way, you may need to quote your glob patterns for them to work, but this was never the case for me.