git add 只添加修改过的更改并忽略未跟踪的文件

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/7124726/
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 11:38:05  来源:igfitidea点击:

git add only modified changes and ignore untracked files

gitcommitgitignorestaging

提问by Steve

I ran "git status" and listed below are some files that were modified/or under the heading "changes not staged for commit". It also listed some untracked files that I want to ignore (I have a ".gitignore" file in these directories).

我运行了“git status”,下面列出了一些被修改/或在“未暂存以进行提交的更改”标题下的文件。它还列出了一些我想忽略的未跟踪文件(我在这些目录中有一个“.gitignore”文件)。

I want to put the modified files in staging so I can commit them. When I ran "git add .", it added the modified files AND the files I want to ignore to staging.

我想将修改后的文件放在暂存中,以便我可以提交它们。当我运行“git add .”时,它将修改后的文件和我想忽略的文件添加到暂存中。

How do I add only the modified files and ignore the untracked files if presented with the git status below.

如果出现下面的 git 状态,如何仅添加修改过的文件并忽略未跟踪的文件。

Also, are my ".gitignore" files working properly?

另外,我的“.gitignore”文件是否正常工作?

$ git status
# On branch addLocation
# Changes not staged for commit:
#   (use "git add <file>..." to update what will be committed)
#   (use "git checkout -- <file>..." to discard changes in working directory)
#
#       modified:   someProject/path/domain/viewer/LocationDO.java
#       modified:   someProject/path/service/ld/LdService.java
#       modified:   someProject/path/service/ld/LdServiceImpl.java
#       modified:   someProject/path/web/jsf/viewer/LocationFormAction.java
#       modified:   someProject/war/WEB-INF/classes/message/viewer/viewer.properties
#       modified:   someProject/war/page/viewer/searchForm.xhtml
#
# Untracked files:
#   (use "git add <file>..." to include in what will be committed)
#
#       .metadata/
#       someProject/build/
no changes added to commit (use "git add" and/or "git commit -a")

回答by manojlds

Ideally your .gitignoreshould prevent the untracked ( and ignored )files from being shown in status, added using git addetc. So I would ask you to correct your .gitignore

理想情况下,您.gitignore应该防止未跟踪(和忽略)文件显示在状态中,使用git add等添加。所以我会要求您更正您的.gitignore

You can do git add -uso that it will stage the modified and deleted files.

您可以这样git add -u做,它将暂存修改和删除的文件。

You can also do git commit -ato commit only the modified and deleted files.

您也可以git commit -a只提交修改和删除的文件。

Note that if you have Git of version before 2.0 and used git add ., then you would need to use git add -u .(See "Difference of “git add -A” and “git add .").

请注意,如果您有 2.0 之前版本的 Git 并使用了git add .,那么您将需要使用git add -u .(参见“ git add -A”和“ git add .”的区别“)。

回答by user877329

This worked for me:

这对我有用:

#!/bin/bash

git add `git status | grep modified | sed 's/\(.*modified:\s*\)//'`

Or even better:

或者甚至更好:

$ git ls-files --modified | xargs git add

回答by Saurabh

git commit -a -m "message"

-a : Includes all currently changed/deleted files in this commit. Keep in mind, however, that untracked (new) files are not included.

-m : Sets the commit's message

-a :包括此提交中所有当前更改/删除的文件。但是请记住,不包括未跟踪的(新)文件。

-m :设置提交的消息

回答by Karl Bielefeldt

You didn't say what's currently your .gitignore, but a .gitignorewith the following contents in your root directory should do the trick.

您没有说当前是什么.gitignore,但是.gitignore根目录中包含以下内容的 应该可以解决问题。

.metadata
build

回答by Mike Q

I happened to try this so I could see the list of files first:

我碰巧尝试了这个,所以我可以先看到文件列表:

git status | grep "modified:" | awk '{print "git add  " }' > file.sh

cat ./file.sh

execute:

执行:

chmod a+x file.sh
./file.sh 

Edit:(see comments) This could be achieved in one step:

编辑:(见评论)这可以一步实现:

git status | grep "modified:" | awk '{print }' | xargs git add && git status

回答by Erik Englund

Not sure if this is a feature or a bug but this worked for us:

不确定这是功能还是错误,但这对我们有用:

git commit '' -m "Message"

git commit '' -m "Message"

Note the empty file list ''. Git interprets this to commit all modified tracked files, even if they are not staged, and ignore untracked files.

请注意空文件列表 ''。Git 将此解释为提交所有修改过的跟踪文件,即使它们没有暂存,并忽略未跟踪的文件。

回答by Dayamoy

To stage modified and deleted files

暂存修改和删除的文件

git add -u

git add -u