git 仅添加修改的更改并忽略未跟踪的文件
我运行了“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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(10)
理想情况下,您的 .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 usinggit 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 usegit add -u .
(See "Difference of “git add -A
” and “git add .
”").这对我有用:
或者更好:
This worked for me:
Or even better:
要暂存修改和删除的文件:
git add -u
其中
-u
是--update
的缩写。To stage modified and deleted files:
git add -u
Where
-u
is short for--update
.-a :包含此提交中所有当前更改/删除的文件。但请记住,未跟踪的(新)文件不包括在内。
-m :设置提交的消息
-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
我碰巧尝试了这个,所以我可以先看到文件列表:
执行:
编辑:(参见评论)
这可以一步实现:
I happened to try this so I could see the list of files first:
execute:
Edit: (see comments)
This could be achieved in one step:
TL;DR : START
使用交互模式 (
git add -i
),选择更新(键入2
或What now> 提示
中的u
),选择全部(在Update>>
中键入*
)代码>提示),退出交互模式(类型What now> 提示中的
7
或q
)。TL;DR : END
对于更有耐心的人:---
STEP 1
您可以使用交互模式。
或者
简写形式,
这将给出所有已修改文件的列表及其状态,以及您可以使用的一堆命令选项,例如:
STEP 2
对于您的情况,请输入 u 或2针对
现在>
,您将在下面看到
STEP 3
,在
更新>>
中> 提示符下,输入*
添加全部将上面列表中的文件添加到暂存区(您也可以通过提供逗号分隔的数字来添加单个文件),此时,您修改的所有文件都已添加到暂存区,您可以验证状态通过在
What now>
提示中键入1
或s
(或者只需键入q
或退出交互模式7
并运行通常的git status
命令),这将向您显示状态,
我们可以在这里看到 unstaged 列显示列表中所有文件的文本 “nothing” 。
对于有关忽略未跟踪文件的问题的第二部分,我相信这个SO答案会有很大帮助。
TL;DR : START
Use interactive mode (
git add -i
), select update (type2
oru
inWhat now> prompt
), select all(type*
inUpdate>>
prompt), exit interactive mode(type7
orq
inWhat now> prompt
) .TL;DR : END
For the more patient:---
STEP 1
You could use the interactive mode.
or
the short-hand form,
This will give a list of all modified files along with their status, plus a bunch of command options you could use, like:
STEP 2
For your case, type u or 2 against
What now>
,You get below,
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),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
ors
in theWhat now>
prompt(or just exit the interactive mode by typingq
or7
and run the usualgit status
command),which will show you the status,
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.
您没有说明当前的
.gitignore
是什么,但根目录中包含以下内容的.gitignore
应该可以解决问题。You didn't say what's currently your
.gitignore
, but a.gitignore
with the following contents in your root directory should do the trick.不确定这是一个功能还是一个错误,但这对我们有用:
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.
.
是正则表达式中的任意一个字符。简短形式由 X 和 Y 字段以及文件名组成。例如:
XY filename.py
检查
git-status
联机帮助页中的“短格式”部分以精确选择文件。M example.py
:清空X字段中的暂存更改和Y字段中的未暂存更改。MM example.py
:X字段中的暂存更改和Y字段中的未暂存更改。.
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.git commit -am“你的消息在这里”这对我有用
git commit -am "your message here" this worked for me