git 仅添加修改的更改并忽略未跟踪的文件

发布于 2024-11-30 08:47:24 字数 1134 浏览 5 评论 0原文

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

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

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

另外,我的“.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")

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

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.

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

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

$ 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")

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(10

乙白 2024-12-07 08:47:24

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

您可以执行git add -u,以便它会暂存修改和删除的文件。

您还可以执行 git commit -a 来仅提交修改和删除的文件。

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

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

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

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

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

千柳 2024-12-07 08:47:24

这对我有用:

#!/bin/bash

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

或者更好:

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

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
漆黑的白昼 2024-12-07 08:47:24

要暂存修改和删除的文件:

git add -u

其中 -u--update 的缩写。

To stage modified and deleted files:

git add -u

Where -u is short for --update.

阳光①夏 2024-12-07 08:47:24
git commit -a -m "message"

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

-m :设置提交的消息

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

飘过的浮云 2024-12-07 08:47:24

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

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

cat ./file.sh

执行:

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

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

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

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

git status | grep "modified:" | awk '{print "git add  " $2}' > 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 $2}' | xargs git add && git status
白馒头 2024-12-07 08:47:24

我想将修改后的文件放入暂存阶段,以便我可以提交它们

TL;DR : START

使用交互模式 (git add -i),选择更新(键入 2What now> 提示 中的 u),选择全部(在 Update>> 中键入 *)代码>提示),退出交互模式(类型What now> 提示中的 7q)。

TL;DR : END

对于更有耐心的人:---

STEP 1

您可以使用交互模式。

git add --interactive 

或者
简写形式,

git add -i

这将给出所有已修改文件的列表及其状态,以及您可以使用的一堆命令选项,例如:

           staged     unstaged path
  1:    unchanged       +17/-0 package-lock.json
  2:    unchanged        +2/-0 package.json
  3:    unchanged       +2/-28 src/App.js
  4:    unchanged        +7/-6 src/App.test.js

*** Commands ***
  1: status       2: update       3: revert       4: add untracked
  5: patch        6: diff         7: quit         8: help
What now>

STEP 2

对于您的情况,请输入 u 2针对现在>

What now>u

您将在下面看到

           staged     unstaged path
  1:    unchanged       +17/-0 package-lock.json
  2:    unchanged        +2/-0 package.json
  3:    unchanged       +2/-28 src/App.js
  4:    unchanged        +7/-6 src/App.test.js
Update>> *

STEP 3

,在更新>>中> 提示符下,输入 * 添加全部将上面列表中的文件添加到暂存区(您也可以通过提供逗号分隔的数字来添加单个文件),

Update>> *

此时,您修改的所有文件都已添加到暂存区,您可以验证状态通过在 What now> 提示中键入 1s(或者只需键入 q 或退出交互模式7 并运行通常的 git status 命令),

What now> 1

这将向您显示状态,

           staged     unstaged path
  1:       +17/-0      nothing package-lock.json
  2:        +2/-0      nothing package.json
  3:       +2/-28      nothing src/App.js
  4:        +7/-6      nothing src/App.test.js

我们可以在这里看到 unstaged 列显示列表中所有文件的文本 “nothing”

对于有关忽略未跟踪文件的问题的第二部分,我相信这个SO答案会有很大帮助。

I want to put the modified files in staging so I can commit them

TL;DR : START

Use interactive mode (git add -i), select update (type 2 or u in What now> prompt), select all(type * in Update>> prompt), exit interactive mode(type 7 or q in What now> prompt) .

TL;DR : END

For the more patient:---

STEP 1

You could use the interactive mode.

git add --interactive 

or
the short-hand form,

git add -i

This will give a list of all modified files along with their status, plus a bunch of command options you could use, like:

           staged     unstaged path
  1:    unchanged       +17/-0 package-lock.json
  2:    unchanged        +2/-0 package.json
  3:    unchanged       +2/-28 src/App.js
  4:    unchanged        +7/-6 src/App.test.js

*** Commands ***
  1: status       2: update       3: revert       4: add untracked
  5: patch        6: diff         7: quit         8: help
What now>

STEP 2

For your case, type u or 2 against What now>,

What now>u

You get below,

           staged     unstaged path
  1:    unchanged       +17/-0 package-lock.json
  2:    unchanged        +2/-0 package.json
  3:    unchanged       +2/-28 src/App.js
  4:    unchanged        +7/-6 src/App.test.js
Update>> *

STEP 3

In the Update>> prompt, type * to add all the files of the above list to the staging area(you could well add individual files too by providing the comma separated numbers),

Update>> *

At this point of time, all of your modified files have been added to the staging area and you can verify the status by typing 1 or s in the What now> prompt(or just exit the interactive mode by typing q or 7 and run the usual git status command),

What now> 1

which will show you the status,

           staged     unstaged path
  1:       +17/-0      nothing package-lock.json
  2:        +2/-0      nothing package.json
  3:       +2/-28      nothing src/App.js
  4:        +7/-6      nothing src/App.test.js

We can see here that the unstaged column displays the text "nothing" for all the files of the list.

For the second part of question regarding ignoring the untracked files, I believe this SO answer will help a lot.

无法言说的痛 2024-12-07 08:47:24

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

.metadata
build

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

.metadata
build
深居我梦 2024-12-07 08:47:24

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

git commit '' -m "Message"

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

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

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.

︶ ̄淡然 2024-12-07 08:47:24
git add $(git status --short | grep '.M ' | sed 's/.M //')
# Change the regex pattern of grep only.
# So, if you create a custom shell alias or function, 
# the regex pattern of grep should be the parameter.

. 是正则表达式中的任意一个字符。
简短形式由 X 和 Y 字段以及文件名组成。例如:XY filename.py

检查git-status 联机帮助页中的“短格式”部分以精确选择文件。

M example.py:清空X字段中的暂存更改和Y字段中的未暂存更改。

MM example.py:X字段中的暂存更改和Y字段中的未暂存更改。

git add $(git status --short | grep '.M ' | sed 's/.M //')
# Change the regex pattern of grep only.
# So, if you create a custom shell alias or function, 
# the regex pattern of grep should be the parameter.

. is any one character in regular expression.
The short form consists of X and Y fields and a file name. eg: XY filename.py

Check the 'Short Format' section in the git-status manpage to select files precisely.

M example.py: Empty staged changes in the X field and unstaged changes in the Y field.

MM example.py: Staged changes in the X field and unstaged changes in the Y field.

心作怪 2024-12-07 08:47:24

git commit -am“你的消息在这里”这对我有用

git commit -am "your message here" this worked for me

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