git 将除单个文件之外的所有文件添加到提交?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/4475457/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-10 09:39:46  来源:igfitidea点击:

Add all files to a commit except a single file?

gitgit-add

提问by user291701

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:

我在变更集中有一堆文件,但我想专门忽略一个修改过的文件。看起来像这样git status

# modified:   main/dontcheckmein.txt
# deleted:    main/plzcheckmein.c
# deleted:    main/plzcheckmein2.c
...

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

有什么方法可以做git add但只是忽略我不想触摸的一个文本文件?就像是:

git add -u -except main/dontcheckmein.txt

回答by Ben Hymanson

git add -u
git reset -- main/dontcheckmein.txt

回答by Aquarius Power

1) To start ignoring changes to a single already versioned file

1) 开始忽略对单个已版本化文件的更改

git update-index --assume-unchanged "main/dontcheckmein.txt"

and to undo that git update-index --no-assume-unchanged "main/dontcheckmein.txt"

并撤消 git update-index --no-assume-unchanged "main/dontcheckmein.txt"

github docs to ignore files

github 文档忽略文件

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

2)完全忽略特定的单个文件,阻止它在存储库中创建

First, look at this stackoverflow post: Git global ignore not working

首先,看看这个stackoverflow帖子:Git global ignore not working

In .gitignore, add the relative path to the file without leading ./.

在 中.gitignore,添加文件的相对路径,不带前导./

So, if your file is at MyProject/MyFolder/myfile.txt, (where .gitis also in the MyProjectfolder), add MyFolder/myfile.txtto your at .gitignorefile.

因此,如果您的文件位于MyProject/MyFolder/myfile.txt, (.git也在MyProject文件夹中的位置),请添加MyFolder/myfile.txt到您的 at.gitignore文件中。

You can confirm what rules are associated with ignore via git check-ignore "MyFolder/myfile.txt"

您可以通过以下方式确认哪些规则与忽略相关联 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.txtin ~/.gitignore_global, it will work but will not make much sense...

该链接讨论~/.gitignore_global,但该文件与您的项目相关。因此,如果您将排除模式MyFolder/myfile.txt放入 中~/.gitignore_global,它将起作用但没有多大意义......

On the other hand, if you setup your project with git config core.excludesfile .gitignorewhere .gitignoreis in MyProject, the local file will override ~/.gitignore_global, which can have very useful rules...

另一方面,如果您使用git config core.excludesfile .gitignorewhere .gitignoreis in设置您的项目MyProject,本地文件将覆盖~/.gitignore_global,这可能具有非常有用的规则......

So, for now, I think it's best to make some script to mix your .gitignorewith ~/.gitignore_globalat .gitignore.

所以,就目前而言,我认为最好制作一些脚本将您的.gitignore~/.gitignore_globalat混合.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...

最后一个警告
如果您要忽略的文件已经在存储库中,除非您执行以下操作git rm "MyFolder/myfile.txt",否则此方法将不起作用:但请先备份它,因为它也会在本地删除!你可以稍后复制它...

回答by Rituraj ratan

For a File

对于文件

git add -u
git reset -- main/dontcheckmein.txt

For a folder

对于文件夹

git add -u
git reset -- main/*

回答by cateyes

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

现在git支持exclude certain paths and filespathspec magic:(exclude)及其缩写形式:!。因此,您可以通过以下命令轻松实现它。

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/*

回答by Anthony Naddeo

While Ben Hymanson 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).

虽然 Ben Hymanson 是正确的,但我想我也会补充一下我是如何使用该解决方案的。下面是我使用的一个非常简单的脚本(我称之为 gitadd)来添加所有更改,除了我在一个名为.gittrackignore(非常类似于 .gitignore 的工作方式)的文件中列出的少数几个更改。

#!/bin/bash
set -e

git add -A
git reset `cat .gittrackignore`

And this is what my current .gittrackignorelooks like.

这就是我现在的.gittrackignore样子。

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 gitaddand add all of the changes to git without having to un-add project.properties every single time.

我正在开发一个在部署时从命令行编译的 Android 项目。这个项目依赖于 SherlockActionBar,所以它需要在 project.properties 中被引用,但这会影响编译,所以现在我只需输入gitadd并将所有更改添加到 git 中,而不必每次都取消添加 project.properties。

回答by gsumk

To keep the change in file but not to commit I did this

为了保持文件中的更改但不提交我这样做了

git add .

git add .

git reset -- main/dontcheckmein.txt

git reset -- main/dontcheckmein.txt

git commit -m "commit message"

git commit -m "commit message"

to verify the file is excluded do

验证文件被排除做

git status

git status

回答by Daniel Danielecki

git add .
git reset main/dontcheckmein.txt

回答by Dhanuka Nuwan

Use git add -Ato add all modified and newly added files at once.

用于git add -A一次性添加所有已修改和新添加的文件。

Example

例子

git add -A
git reset -- main/dontcheckmein.txt

回答by Abz

Try this:

尝试这个:

git checkout -- main/dontcheckmein.txt

回答by cegas

For the specific case in the question, easiest way would be to add all files with .cextension and leave out everything else:

对于问题中的特定情况,最简单的方法是添加所有带有.c扩展名的文件并忽略其他所有文件:

git add *.c

From git-scm(or/and man git add):

git-scm(或/和man git add):

git add <pathspec>…?

Files to add content from. Fileglobs (e.g. *.c) can be given to add all matching files. <...>

git add <pathspec>...?

要从中添加内容的文件。可以给出 Fileglobs(例如 *.c)来添加所有匹配的文件。<...>

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 mainfolder. You can even go wild with more elaborate patterns:

添加文件main夹中的所有文件(不会被忽略)。你甚至可以用更精致的图案来疯狂:

git add **/s?c/*Service*

The above will add all files that are in s(any char)cfolder and have Servicesomewhere in their filename.

以上将添加文件s(any char)c夹中的所有文件,并Service在其文件名中的某处。

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 .cand .h:

显然,您不限于每个命令使用一种模式。也就是说,您可以要求 git 添加所有扩展名为.c.h 的文件

git add *.c *.h

This linkmight give you some more glob pattern ideas.

链接可能会为您提供更多 glob 模式的想法。

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 mayneed to quote your glob patterns for them to work, but this was never the case for me.

顺便说一句,您可能需要引用您的 glob 模式才能使它们工作,但对我来说情况并非如此。