Add all files to a commit except a single file?

2019-01-12 13:11发布

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

标签: git git-add
10条回答
Viruses.
2楼-- · 2019-01-12 13:35

Changes to be committed: (use "git reset HEAD ..." to unstage)

查看更多
趁早两清
3楼-- · 2019-01-12 13:40

Try this:

git checkout -- main/dontcheckmein.txt

查看更多
The star\"
4楼-- · 2019-01-12 13:44

I use git add --patch quite a bit and wanted something like this to avoid having to hit d all the time through the same files. I whipped up a very hacky couple of git aliases to get the job done:

[alias]
    HELPER-CHANGED-FILTERED = "!f() { git status --porcelain | cut -c4- | ( [[ \"$1\" ]] && egrep -v \"$1\" || cat ); }; f"
    ap                      = "!git add --patch -- $(git HELPER-CHANGED-FILTERED 'min.(js|css)$' || echo 'THIS_FILE_PROBABLY_DOESNT_EXIST' )"

In my case I just wanted to ignore certain minified files all the time, but you could make it use an environment variable like $GIT_EXCLUDE_PATTERN for a more general use case.

查看更多
一纸荒年 Trace。
5楼-- · 2019-01-12 13:45

While Ben Jackson is correct, I thought I would add how I've been using that solution as well. Below is a very simple script I use (that I call gitadd) to add all changes except a select few that I keep listed in a file called .gittrackignore (very similar to how .gitignore works).

#!/bin/bash
set -e

git add -A
git reset `cat .gittrackignore`

And this is what my current .gittrackignore looks like.

project.properties

I'm working on an Android project that I compile from the command line when deploying. This project depends on SherlockActionBar, so it needs to be referenced in project.properties, but that messes with the compilation, so now I just type gitadd and add all of the changes to git without having to un-add project.properties every single time.

查看更多
祖国的老花朵
6楼-- · 2019-01-12 13:49
git add -u
git reset -- main/dontcheckmein.txt
查看更多
倾城 Initia
7楼-- · 2019-01-12 13:50

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"

check here

2) To completely ignore a specific single file preventing it from being created at repository

First look at this Git global ignore not working

and at .gitignore add the relative path to the file without leading ./

so if your file is at MyProject/MyFolder/myfile.txt (where .git is also at MyProject), at .gitignore you put just this MyFolder/myfile.txt

you can confirm what rule is related to the ignore with git check-ignore "MyFolder/myfile.txt"

About global ignore

That link speaks about ~/.gitignore_global; but the file is related to your project; so, if you put the exclude pattern MyFolder/myfile.txt at ~/.gitignore_global, it will work but will not make much sense...

In the other hand, if you setup your project with git config core.excludesfile .gitignore where .gitignore is at MyProject; that setup will override ~/.gitignore_global that can have very useful rules...

So, for now, I think the best is 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 on the repository, this method will not work unless you do this: git rm "MyFolder/myfile.txt", but backup it first as it will be removed locally also! you can copy it back later...

查看更多
登录 后发表回答