将除单个文件之外的所有文件添加到提交中?
我的变更集中有一堆文件,但我想专门忽略一个修改过的文件。在 git status
之后看起来像这样:
# modified: main/dontcheckmein.txt
# deleted: main/plzcheckmein.c
# deleted: main/plzcheckmein2.c
...
有没有一种方法可以执行 git add
但忽略我不想触及的一个文本文件?像这样的东西:
git add -u -except main/dontcheckmein.txt
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
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(20)
您可以使用推荐的 git
添加所有文件更改并恢复不需要推送的文件
you can use recommend of git
add all file change and restore file not want push
试试这个:
Try this:
不是直接满足您的要求,因为它无法通过一个命令实现它,但结果应该是您想要的。
通过输入
git status
将所有文件添加到暂存区域后,将建议您
Not directly what you asked as it does not achieve it in one command but the result should be what you desire.
After adding all files to the staging area via
typing
git status
will suggest you to我经常使用 git add --patch ,并且想要这样的东西以避免一直通过相同的文件点击 d 。我创建了几个非常hacky的git别名来完成工作:
就我而言,我只想一直忽略某些缩小的文件,但你可以让它使用像$GIT_EXCLUDE_PATTERN这样的环境变量对于更一般的用例。
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: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.要提交的更改:
(使用“git reset HEAD ...”取消暂存)
Changes to be committed:
(use "git reset HEAD ..." to unstage)
如果您想每次都忽略特定文件,可以使用三种不涉及
.gitignore
的有效解决方案。将要忽略的文件添加到
.git/info/exclude
。它的工作方式与您的.gitignore
类似,只不过配置特定于您的计算机。< /p>使用预提交重置您想要的文件git 钩子。例如:
预提交挂钩在提交之前运行。因此您的文件可以在每次提交之前自动重置。
第三种方法是更新特定文件的索引。
If you want to ignore a particular file every time there are three effective solutions that don't involve
.gitignore
.Add the file you want to ignore to
.git/info/exclude
. It works just like your.gitignore
, except that the configuration is specific to your machine.Reset the file you want to using a pre-commit git hook. For example:
A pre-commit hook runs just before a commit. So your files can be automatically reset before each commit.
The third way is to update the index for a particular file.
根据 Pro Git 书。
作者:斯科特·查康、本·斯特劳布。
有两种方法可以实现:
git reset
Git 2.23.0 版本引入了一个新命令:git Restore。它基本上是我们刚刚介绍的 git Reset 的替代方案。从 Git 版本 2.23.0 开始,Git 将使用 git Restore 而不是 git Reset 来进行许多撤消操作。
git 恢复
According to Pro Git book.
By Scott Chacon, Ben Straub.
There're 2 approaches to achieve:
git reset
Git version 2.23.0 introduced a new command: git restore. It’s basically an alternative to git reset which we just covered. From Git version 2.23.0 onwards, Git will use git restore instead of git reset for many undo operations.
git restore
你可以试试这个:
git add * && git重置main/dontcheckmein.txt
You can try this:
git add * && git reset main/dontcheckmein.txt
注意:Git 随后为此添加了特殊语法,这在其他答案中进行了解释。
Note: Git has subsequently added special syntax for this, which is explained in other answers.
现在,git 支持通过 pathspec magic
:(exclude)
及其缩写形式:!
来排除某些路径和文件
。所以你可以通过以下命令轻松实现它。实际上您可以指定更多:
对于 Mac 和 Linux,用引号将每个文件/文件夹路径括起来
Now
git
supportsexclude certain paths and files
by pathspec magic:(exclude)
and its short form:!
. So you can easily achieve it as the following command.Actually you can specify more:
For Mac and Linux, surround each file/folder path with quotes
1) 开始忽略对单个已版本化文件的更改
并撤消该
git update-index --no-assume-unchanged "main/dontcheckmein.txt"
忽略文件的 github 文档
2) 完全忽略特定的单个文件,防止其被在存储库中创建
首先,查看此 stackoverflow 帖子:Git 全局忽略不起作用
在
.gitignore
中,添加文件的相对路径,不带前导./
。因此,如果您的文件位于
MyProject/MyFolder/myfile.txt
(其中.git
也在MyProject
文件夹中),请添加 < code>MyFolder/myfile.txt 到您的.gitignore
文件。您可以通过
git check-ignore "MyFolder/myfile.txt"
确认哪些规则与忽略相关联关于全局忽略
该链接讨论
~/.gitignore_global< /code>,但该文件与您的项目相关。因此,如果您将排除模式
MyFolder/myfile.txt
放入~/.gitignore_global
中,它会起作用,但没有多大意义......另一方面,如果您使用
git config core.excludesfile .gitignore
设置项目,其中.gitignore
位于MyProject
中,则本地文件将覆盖~ /.gitignore_global
,其中可以有非常有用的规则...所以,现在,我认为最好制作一些脚本将
.gitignore
与~/.gitignore_global
在.gitignore
混合。最后一个警告
如果您要忽略的文件已在存储库中,则此方法将不起作用,除非您执行以下操作:
git rm "MyFolder/myfile.txt"
,但首先将其备份,因为它将是本地也删除了!稍后可以复制回来...1) To start ignoring changes to a single already versioned file
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 theMyProject
folder), addMyFolder/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 patternMyFolder/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 inMyProject
, 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...对于文件
对于文件夹
For a File
For a folder
Git 为要排除的路径提供了
:(exclude)
pathspecs 前缀。它的简短魔法签名是
:^
。文档: https://git-scm.com/docs/gitglossary#Documentation /gitglossary.txt-exclude
此线程的一些答案提到了
:!
,这也可以,但只能带引号。一般来说,!
对于 shell 扩展来说被认为是一个可怕的字符。如果您像我一样 - 始终在
git add
命令中使用-p
标志查看将要提交的内容 -:^
魔术签名有效也像一个魅力:Git provides
:(exclude)
pathspecs prefix for paths to be excluded.Its short magic signature is
:^
.Documentation: https://git-scm.com/docs/gitglossary#Documentation/gitglossary.txt-exclude
Some answers to this thread mention
:!
, which would also work, but only with quotes. In general,!
is considered a horrible character for shell expansion.If you are like me — always review what is going to be committed using
-p
flag within thegit add
command —:^
magic signature works like a charm, too:虽然本·杰克逊是正确的,但我想我也应该补充一下我如何使用该解决方案。下面是我使用的一个非常简单的脚本(我称之为 gitadd)来添加所有更改,除了我在名为
.gittrackignore
的文件中列出的选定的几个更改(与 .gitignore 的工作方式非常相似)。这就是我当前的 .gittrackignore 的样子。
我正在开发一个 Android 项目,在部署时从命令行编译该项目。这个项目依赖于 SherlockActionBar,因此需要在 project.properties 中引用它,但这会扰乱编译,所以现在我只需输入 gitadd 并将所有更改添加到 git 中,而无需取消 -每次都添加project.properties。
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).And this is what my current
.gittrackignore
looks like.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.为了将更改保留在文件中但不提交,我做了这个
git add .
git reset -- main/dontcheckmein.txt
git commit -m "commit message"
验证文件是否已排除,执行
git status
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 -A 一次性添加所有修改过的和新添加的文件。
例子
Use
git add -A
to add all modified and newly added files at once.Example
我们可以添加所有文件并排除必须通过 git Reset 删除的文件。
We can add all the files and exclude the file which has to be removed by git reset.
添加所有文件
git add .
获取要粘贴到下面的文件路径
git status
从提交中删除
git reset -- file/to/path/file-to-ignore.txt
add all files
git add .
get file path to paste below
git status
remove from the commit
git reset -- file/to/path/file-to-ignore.txt
对于问题中的具体情况,最简单的方法是添加具有 .c 扩展名的所有文件并忽略其他所有内容:
来自 git-scm (或/和
man git add
):请注意,这意味着您还可以执行以下操作:
添加
main
文件夹中的所有文件(未被忽略)。您甚至可以使用更复杂的模式:上面的代码将添加
s(any char)c
文件夹中的所有文件,并在其文件名中的某个位置包含Service
。显然,每个命令并不限于一种模式。也就是说,您可以要求 git 添加扩展名为 .c 和 .h 的所有文件:
此 链接 可能会给您一些更多的 glob 模式想法。
当我进行许多更改时,我发现它特别有用,但仍然希望我的提交保持原子性并反映渐进的过程,而不是我当时可能正在进行的更改的大杂烩。当然,在某些时候,提出复杂模式的成本超过了使用更简单的方法添加文件的成本,甚至一次添加一个文件的成本。然而,大多数时候,我可以轻松地通过简单的模式来确定我需要的文件,并排除其他所有文件。
顺便说一句,您可能需要引用您的全局模式才能使它们工作,但对我来说从来不是这样。
For the specific case in the question, easiest way would be to add all files with .c extension and leave out everything else:
From git-scm (or/and
man git add
):Note that this means that you could also do something like:
to add all files (that are not ignored) that are in the
main
folder. You can even go wild with more elaborate patterns:The above will add all files that are in
s(any char)c
folder and haveService
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:
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.