每次我想提交时是否都必须向 git 添加文件?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/3160976/
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
Should I have to add files to git every time I want to commit?
提问by Hamy
I'm new to git, coming from SVN world. So far, it seems a lot more useful, but I am still working out the kinks.
我是 git 新手,来自 SVN 世界。到目前为止,它似乎更有用,但我仍在解决问题。
Currently, my workflow is like this:
目前,我的工作流程是这样的:
make changes > git add . > git commit > enter log message
进行更改> git add 。> git commit > 输入日志信息
What I don't understand is why I seem to have to add all of my files before I commit. They are under version control already? Why does git commit tell me there are no changes added to the commit, but also point out that I have modified files? It says "Changed but not updated:". What does this mean??
我不明白的是为什么我似乎必须在提交之前添加所有文件。他们已经在版本控制之下了吗?为什么 git commit 告诉我提交中没有添加任何更改,但还指出我修改了文件?它说“已更改但未更新:”。这是什么意思??
Sorry if this is easy, I feel like I am missing some overarching point
对不起,如果这很容易,我觉得我错过了一些重要的点
采纳答案by Justin L.
This allows you to separate commits by edits. If you only want to commit these files now, under one commit, and then these next files now, under a second commit, you could do:
这允许您按编辑分隔提交。如果您现在只想在一次提交下提交这些文件,然后在第二次提交下现在提交这些下一个文件,您可以执行以下操作:
git add files_under_one_topic
git commit -m "this is about one thing"
git add files_left_over_to_commit_about_a_completely_different_topic
git commit -m "this is about another thing."
回答by Derek Greer
Git works by using a "staging" area where you prepare what you are going to bundle together as a commit. So, you decided what set of changes you want to commit (e.g. all or a subset), you add them to the staging area, and then you commit what's in the staging area.
Git 通过使用“暂存”区域来工作,您可以在其中准备将要捆绑在一起作为提交的内容。因此,您决定要提交的更改集(例如全部或子集),将它们添加到暂存区,然后提交暂存区中的内容。
When you invoke git status
, it shows you what has been added to the staging area (i.e. "Changes to be committed"), what has been modified among the files git is tracking (i.e. Changed but not updated"), and any new files that you've never added before (i.e. Untracked files).
当您调用 时git status
,它会向您显示已添加到暂存区的内容(即“要提交的更改”)、git 正在跟踪的文件中已修改的内容(即已更改但未更新),以及您添加的任何新文件以前从未添加过(即未跟踪的文件)。
If you want to just commit stuff that you've changed, but not include newly created files you can use git commit -a -m "Comment for modified files already under source control."
如果您只想提交已更改的内容,但不包括您可以使用的新创建的文件 git commit -a -m "Comment for modified files already under source control."
回答by Ether
You're not adding files in the sense that you're putting them under git control, but you're adding them to a change list. Some other SCMs such as perforce do this as well. It's handy for building up distinct sets of changes that you aren't ready to commit yet, but you want to commit in separate blocks.
您不是在将文件置于 git 控制之下的意义上添加文件,而是将它们添加到更改列表中。其他一些 SCM(例如 perforce)也执行此操作。这对于构建您尚未准备好提交但希望在单独的块中提交的不同更改集非常方便。
You can commit in a more subversionish way by simply doing git commit -a
-- which will commit everything that git knows about that has been changed, just like what svn commit
does.
您可以通过简单地以更具颠覆性的方式git commit -a
提交 - 这将提交 git 知道的所有已更改的内容,就像所做的svn commit
一样。
(PS. Since you're coming from the svn world, I'll mention another gotcha that threw me for a loop for a bit -- when you git diff
, it will only show you the diff between your current state and what is in the changelist, NOT the difference between your current state and the last commit. If you run git diff
immediately after adding all changed files (e.g. git add -u
), you'll see an empty diff, even though there are differences to be committed!)
(PS。既然你来自 svn 世界,我会提到另一个让我陷入循环的问题——当你时git diff
,它只会显示你当前状态和更改列表中的内容之间的差异, 不是您当前状态和上次提交之间的差异。如果您git diff
在添加所有更改的文件(例如git add -u
)后立即运行,您将看到一个空的差异,即使存在差异要提交!)