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

发布于 2024-10-08 01:30:33 字数 339 浏览 8 评论 0原文

我的变更集中有一堆文件,但我想专门忽略一个修改过的文件。在 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(20

荒路情人 2024-10-15 01:30:34

您可以使用推荐的 git

添加所有文件更改并恢复不需要推送的文件

git add .
git restore --staged <file_not_push>

you can use recommend of git

add all file change and restore file not want push

git add .
git restore --staged <file_not_push>
心房的律动 2024-10-15 01:30:34

试试这个:

git checkout -- main/dontcheckmein.txt

Try this:

git checkout -- main/dontcheckmein.txt
戈亓 2024-10-15 01:30:34

不是直接满足您的要求,因为它无法通过一个命令实现它,但结果应该是您想要的。

通过输入 git status 将所有文件添加到暂存区域后,

git add -u

将建议您

git restore --staged main/dontcheckmein.txt

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

git add -u

typing git status will suggest you to

git restore --staged main/dontcheckmein.txt
情域 2024-10-15 01:30:34

我经常使用 git add --patch ,并且想要这样的东西以避免一直通过相同的文件点击 d 。我创建了几个非常hacky的git别名来完成工作:

[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)

就我而言,我只想一直忽略某些缩小的文件,但你可以让它使用像$GIT_EXCLUDE_PATTERN这样的环境变量对于更一般的用例。

|| echo 'THIS_FILE_PROBABLY_DOESNT_EXIST' )"

就我而言,我只想一直忽略某些缩小的文件,但你可以让它使用像$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:

[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)

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.

|| 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.

顾北清歌寒 2024-10-15 01:30:34

要提交的更改:
(使用“git reset HEAD ...”取消暂存)

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

廻憶裏菂餘溫 2024-10-15 01:30:34

如果您想每次都忽略特定文件,可以使用三种不涉及 .gitignore 的有效解决方案。

  1. 将要忽略的文件添加到.git/info/exclude。它的工作方式与您的 .gitignore 类似,只不过配置特定于您的计算机。< /p>

  2. 使用预提交重置您想要的文件git 钩子。例如:

    $ echo 'git reset -- src/main/resources/log4j.properties' >>> .git/hooks/预提交
    $ chmod +x .git/hooks/预提交
    

    预提交挂钩在提交之前运行。因此您的文件可以在每次提交之前自动重置。

    $ git 状态
    在分支 CHANGE-137-notifications-support 上...
    
    未暂存提交的更改:
     修改:Dockerfile
     修改:src/main/resources/log4j.properties
    
    $ git add .
    
    $ git 状态
    在分支 CHANGE-137-notifications-support 上...
    
    要提交的更改:
     修改:Dockerfile
     修改:src/main/resources/log4j.properties
    
    
    $ git commit -m“微不足道的改变”
    
    重置后未暂存的更改:
    M src/main/resources/log4j.properties
    
    [CHANGE-137-notifications-support 97cfb3f] 简单的更改
    更改了 1 个文件,插入了 3 次(+)
    
  3. 第三种方法是更新特定文件的索引。

    $ git update-index --assume-unchanged src/main/resources/log4j.properties
    

If you want to ignore a particular file every time there are three effective solutions that don't involve .gitignore.

  1. 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.

  2. Reset the file you want to using a pre-commit git hook. For example:

    $ echo 'git reset -- src/main/resources/log4j.properties' >> .git/hooks/pre-commit
    $ chmod +x .git/hooks/pre-commit
    

    A pre-commit hook runs just before a commit. So your files can be automatically reset before each commit.

    $ git status
    On branch CHANGE-137-notifications-support ...
    
    Changes not staged for commit:
     modified:   Dockerfile
     modified:   src/main/resources/log4j.properties
    
    $ git add .
    
    $ git status
    On branch CHANGE-137-notifications-support ...
    
    Changes to be committed:
     modified:   Dockerfile
     modified:   src/main/resources/log4j.properties
    
    
    $ git commit -m "Trivial change"
    
    Unstaged changes after reset:
    M    src/main/resources/log4j.properties
    
    [CHANGE-137-notifications-support 97cfb3f] Trivial change
    1 file changed, 3 insertions(+)
    
  3. The third way is to update the index for a particular file.

    $ git update-index --assume-unchanged src/main/resources/log4j.properties
    
摇划花蜜的午后 2024-10-15 01:30:34

根据 Pro Git 书。
作者:斯科特·查康、本·斯特劳布。
有两种方法可以实现:

git reset

$ git reset HEAD main/dontcheckmein.txt

Git 2.23.0 版本引入了一个新命令:git Restore。它基本上是我们刚刚介绍的 git Reset 的替代方案。从 Git 版本 2.23.0 开始,Git 将使用 git Restore 而不是 git Reset 来进行许多撤消操作。

git 恢复

$ git restore --staged main/dontcheckmein.txt

:警告:重要的是要了解 git Restore是一个危险的命令。您对该文件所做的任何本地更改都消失了 - Git 只是用最后一个暂存或提交的版本替换了该文件。除非您绝对知道您不想要那些未保存的本地更改,否则永远不要使用此命令。

:警告:确实,git重置可能是一个危险的命令,特别是如果您提供--hard标志。但是,在上述场景中,您的工作目录中的文件不会被触及,因此相对安全。

According to Pro Git book.
By Scott Chacon, Ben Straub.
There're 2 approaches to achieve:

git reset

$ git reset HEAD main/dontcheckmein.txt

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 restore --staged main/dontcheckmein.txt

:warning: It’s important to understand that git restore <file> is a dangerous command. Any local changes you made to that file are gone — Git just replaced that file with the last staged or committed version. Don’t ever use this command unless you absolutely know that you don’t want those unsaved local changes.

:warning: It’s true that git reset can be a dangerous command, especially if you provide the --hard flag. However, in the scenario described above, the file in your working directory is not touched, so it’s relatively safe.

落叶缤纷 2024-10-15 01:30:34

你可以试试这个:git add * && git重置main/dontcheckmein.txt

You can try this: git add * && git reset main/dontcheckmein.txt

婴鹅 2024-10-15 01:30:33
git add -u
git reset -- main/dontcheckmein.txt

注意:Git 随后为此添加了特殊语法,这在其他答案中进行了解释。

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

Note: Git has subsequently added special syntax for this, which is explained in other answers.

番薯 2024-10-15 01:30:33

现在,git 支持通过 pathspec magic :(exclude) 及其缩写形式 :!排除某些路径和文件。所以你可以通过以下命令轻松实现它。

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

实际上您可以指定更多:

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

对于 Mac 和 Linux,用引号将每个文件/文件夹路径括起来

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

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/*'
毁虫ゝ 2024-10-15 01:30:33

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

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

并撤消该 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

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...

回心转意 2024-10-15 01:30:33

对于文件

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

对于文件夹

git add -u
git reset -- main/*

For a File

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

For a folder

git add -u
git reset -- main/*
葬花如无物 2024-10-15 01:30:33

Git 为要排除的路径提供了 :(exclude) pathspecs 前缀。

它的简短魔法签名是:^
文档: https://git-scm.com/docs/gitglossary#Documentation /gitglossary.txt-exclude

git add . :^main/dontcheckmein.txt

此线程的一些答案提到了 :!,这也可以,但只能带引号。一般来说,! 对于 shell 扩展来说被认为是一个可怕的字符。


如果您像我一样 - 始终在 git add 命令中使用 -p 标志查看将要提交的内容 - :^ 魔术签名有效也像一个魅力:

git add -p . :^main/dontcheckmein.txt

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

git add . :^main/dontcheckmein.txt

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 the git add command — :^ magic signature works like a charm, too:

git add -p . :^main/dontcheckmein.txt
凌乱心跳 2024-10-15 01:30:33

虽然本·杰克逊是正确的,但我想我也应该补充一下我如何使用该解决方案。下面是我使用的一个非常简单的脚本(我称之为 gitadd)来添加所有更改,除了我在名为 .gittrackignore 的文件中列出的选定的几个更改(与 .gitignore 的工作方式非常相似)。

#!/bin/bash
set -e

git add -A
git reset `cat .gittrackignore`

这就是我当前的 .gittrackignore 的样子。

project.properties

我正在开发一个 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).

#!/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.

却一份温柔 2024-10-15 01:30:33

为了将更改保留在文件中但不提交,我做了这个

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

鲸落 2024-10-15 01:30:33
git add .
git reset main/dontcheckmein.txt
git add .
git reset main/dontcheckmein.txt
私藏温柔 2024-10-15 01:30:33

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

例子

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

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

Example

git add -A
git reset -- main/dontcheckmein.txt
嘴硬脾气大 2024-10-15 01:30:33

我们可以添加所有文件并排除必须通过 git Reset 删除的文件。

git add .
git reset -- <file_name_to_exclude>

We can add all the files and exclude the file which has to be removed by git reset.

git add .
git reset -- <file_name_to_exclude>
输什么也不输骨气 2024-10-15 01:30:33

添加所有文件

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

懵少女 2024-10-15 01:30:33

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

git add *.c

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

git add...

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

请注意,这意味着您还可以执行以下操作:

git add **/main/*

添加 main 文件夹中的所有文件(未被忽略)。您甚至可以使用更复杂的模式:

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

上面的代码将添加 s(any char)c 文件夹中的所有文件,并在其文件名中的某个位置包含 Service

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

git add *.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:

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文