每次提交时都必须'git add'文件吗? [英] Do I have to 'git add' a file each time it gets committed?

查看:1039
本文介绍了每次提交时都必须'git add'文件吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我之前使用过Mercurial,但计划在不久的将来转用Git。



我看过的所有教程都解释了Git的工作方式,文件在每次提交之前都会添加到舞台('git add'),无论它们是否曾被跟踪过。

Mercurial也有一个类似的命令('hg add'),但从我记忆中,你只需要'添加'一次。例如,新存储库的步骤如下所示:

  hg init 
hg add。
hg commit初始提交
hg推送

此工作流程可能与Git,如果不是,重复'git add'的原因是什么?只是看起来没有必要。

解决方案

是的,可以应用相同的常规工作流程,但add命令也可以更有选择性地使用通过仅向每次提交添加某些文件或仅添加特定部分的文件。这使得将逻辑上不同的补丁分成不同的提交变得更加容易,这使得更容易跟踪和与其他人共享。



但是,你可以只用git add。一切都将进入提交区域。正如在另一个答案中提到的,git commit -a是一个部分快捷方式(它不会添加新文件,但会将所有更改/删除提交到已经跟踪的文件中)。尝试使用git status来查看下一次提交中会包含哪些内容,哪些内容不会。



更新我也应该注意到git add的补充是git reset HEAD,它将从临时区域中删除文件以供下次提交。此外,如果您有兴趣将单个文件中的修改分离为多个提交,请使用-p标志来同时添加git和git以便交互式地浏览文件并选择要添加/重置的块。


I've used Mercurial before but plan on switching to Git in the near future.

All of the tutorials I've seen that explain how Git works show that files are added to the stage ('git add') before each commit, regardless of whether they have been tracked before.

Mercurial also has a command that works in a similar way ('hg add'), but from what I remember, you only need to do 'add' once. For example, the steps for a new repository look something like this:

hg init
hg add .
hg commit "Initial commit"
hg push

Is this workflow possible with Git and if it isn't, what's the reason for the repeated 'git add'? Just seems unnecessary.

解决方案

Yes, the same general workflow can apply, but the "add" command can also be used more selectively, by adding only certain files or only certain portions of files to each commit. This makes it easier to separate logically different patches into different commits, which makes it easier to track and share with others.

But yes, you can just "git add ." and everything will go into the staging area for commit. As mentioned in another answer, "git commit -a" is a partial shortcut (it won't add new files, but will commit all changes/deletes to already tracked files). Try "git status" to see what will be included in your next commit and what won't.

UPDATE I should also note that the complement to git add is "git reset HEAD ", which removes the file from the staging area for your next commit. Also, if you are interested in separating edits within a single file into multiple commits, use the "-p" flag to both git add and git reset in order to step through the file interactively and choose which chunks to add/reset.

这篇关于每次提交时都必须'git add'文件吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆