Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Add all files to a commit except a single file?

Tags:

git

git-add

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:   main/dontcheckmein.txt # deleted:    main/plzcheckmein.c # deleted:    main/plzcheckmein2.c ... 

Is there a way I can do git add but just ignore the one text file I don't want to touch? Something like:

git add -u -except main/dontcheckmein.txt 
like image 214
user291701 Avatar asked Dec 17 '10 22:12

user291701


People also ask

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.

How do I ignore a specific file in git?

If you want to ignore a file that you've committed in the past, you'll need to delete the file from your repository and then add a . gitignore rule for it. Using the --cached option with git rm means that the file will be deleted from your repository, but will remain in your working directory as an ignored file.


2 Answers

git add -u git reset -- main/dontcheckmein.txt 
like image 179
Ben Jackson Avatar answered Oct 13 '22 13:10

Ben Jackson


Now git supports exclude certain paths and files by pathspec magic :(exclude) and its short form :!. So you can easily achieve it as the following command.

git add --all -- :!main/dontcheckmein.txt git add -- . :!main/dontcheckmein.txt 

Actually you can specify more:

git add --all -- :!path/to/file1 :!path/to/file2 :!path/to/folder1/* git add -- . :!path/to/file1 :!path/to/file2 :!path/to/folder1/* 

For Mac and Linux, surround each file/folder path with quotes

git add --all -- ':!path/to/file1' ':!path/to/file2' ':!path/to/folder1/*' 
like image 42
cateyes Avatar answered Oct 13 '22 12:10

cateyes