教我 git commit
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/5625934/
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
Teach me git commit
提问by Quintin Par
I don'tunderstand git commit at all.
我根本不了解 git commit。
It's been a week since I've been using git and all these git with –a, -m, new files, untracked etcis making me totally confused. Tortoisegit is saving me for the moment, but I would like to understand the essence and use it in a way it's totally easy.
自从我使用 git 以来已经一周了,所有这些带有–a、-m、新文件、未跟踪等的git使我完全困惑。Tortoisegit 目前正在拯救我,但我想了解本质并以一种非常简单的方式使用它。
When I am in the git command line, it's rather difficult to selectively commit some files and keep the rest for another commit. How do I make this easy?
当我在 git 命令行中时,很难有选择地提交一些文件并将其余文件保留用于另一个提交。我如何使这变得容易?
回答by Adam Dymitruk
It is quite simple. You need to add the changes that you want to the indexfirst:
这很简单。您需要先将您想要的更改添加到索引中:
git add file1 file2
then
然后
git commit
if you removed a file, the add does that. Add means "add the change" even though it is a removal.
如果您删除了文件,则添加会这样做。添加意味着“添加更改”,即使它是删除。
If you want to add all changes:
如果要添加所有更改:
git add -A
The -a
parameteron commit says to add all changes of tracked filesand commit them. So
在-a
参数上承诺说要添加的所有更改跟踪文件并提交它们。所以
git commit -a
will not commit a new fileyou created. You must explicitly add this.
不会提交您创建的新文件。您必须明确添加此项。
The -m
parameterallows you to avoid opening the editor to edit your commit message and use what you put in following the -m
option:
该-m
参数允许您避免打开编辑器来编辑您的提交消息并使用您在-m
选项后面输入的内容:
git commit -m "Use this message and don't open the editor"
Sometimes this is not a good idea. If you just tried a merge and had conflicts, git caches a very nice message for you once you resolve the conflicts and commit. So there a git commit
is better.
有时这不是一个好主意。如果您刚刚尝试合并并遇到冲突,一旦您解决了冲突并提交,git 就会为您缓存一条非常好的消息。所以有一个git commit
更好的。
To selectively add files, use the patch modifier on git add:
要选择性地添加文件,请在 git add 上使用补丁修饰符:
git add -p
This will now prompt you about the files. This is quite powerful as you can also specify parts of files, or alternatively edit what you want to add to the index. A git commit will only add those.
这将提示您有关文件的信息。这非常强大,因为您还可以指定文件的一部分,或者编辑要添加到索引中的内容。git commit 只会添加那些。
If you want some gui help that is not tortoisegit (avoid windows shell integration), use git gui
.
如果您需要一些不是 tortoisegit 的 gui 帮助(避免 Windows shell 集成),请使用git gui
.
Here is a diagram explaining the index (or staged files):
这是一个解释索引(或暂存文件)的图表:
(from http://progit.org/book/ch2-2.html)
(来自http://progit.org/book/ch2-2.html)
hope this helps.
希望这可以帮助。
回答by Mark Longair
When I am in the git command line, it's rather difficult to selectively commit some files and keep the rest for another commit. How do I make this easy?
当我在 git 命令行中时,很难有选择地提交一些文件并将其余文件保留用于另一个提交。我如何使这变得容易?
Ironically, this is one of the things that git makes really easy, but you have to learn a concept that other version control systems just don't have. That idea is the "staging area" (technically known as the "index") which essentially keeps track of the content that will be in your next commit. When you say git add somefile
that's not saying "start tracking a file of this name", it means "stage the content of the file, as it is right now". You can do this for just the files you pick in your working tree - then, a plain git commit
just turns the contents of the staging area into your next commit.
具有讽刺意味的是,这是 git 使之变得非常简单的事情之一,但是您必须学习其他版本控制系统所没有的概念。这个想法是“暂存区”(技术上称为“索引”),它基本上跟踪将在您下一次提交中的内容。当您说git add somefile
这不是说“开始跟踪此名称的文件”时,它的意思是“暂存文件的内容,就像现在一样”。您可以只为您在工作树中选择的文件执行此操作 - 然后,一个普通的git commit
只是将暂存区的内容转换为您的下一次提交。
Using the -a
parameter to git commit
basically says "I don't care about this staging area / index business, just stage everything that's being tracked before committing". So, until you're happy with the idea of staging particular files, I'd just do a plain "git commit".
使用-a
参数git commit
基本上是说“我不关心这个暂存区/索引业务,只是在提交之前暂存正在跟踪的所有内容”。因此,在您对暂存特定文件的想法感到满意之前,我只会做一个简单的“git commit”。
It's worth running git status
frequently when you're adding files to get used to this idea - it shows files in up to three sections, although not all will always be present if there's nothing to report in that section:
git status
当您添加文件以习惯这个想法时,值得经常运行- 它最多显示三个部分的文件,但如果该部分没有任何报告,则并非所有文件都将始终存在:
Changes to be committed:
要提交的更改:
This lists changes that have been staged, so will be in the next commit. This includes new files, files whose deletion has been staged, and any files with staged changes.
这列出了已暂存的更改,因此将在下一次提交中。这包括新文件、已暂存删除的文件以及具有暂存更改的任何文件。
Changed but not updated:
更改但未更新:
This lists files that have changes that haven't been staged yet.
这会列出具有尚未暂存的更改的文件。
Untracked files:
未跟踪的文件:
These are the files that git doesn't know anything about - typically you'll want to either ignore them, by adding patterns to your .gitignore
file, or add them with git add
.
这些是 git 一无所知的文件 - 通常你会想要忽略它们,通过在.gitignore
文件中添加模式,或者将它们添加到git add
.
Note that files can appear in both of the first two sections. For example, you can change your Makefile
, then add it to the index with git add Makefile
. Then if you go on to make more changes to Makefile
, save it, and then run git status
, you'll see that it's listed both in "Changes to be committed" and "Changed but not updated" - that's because (as mentioned above) git add Makefile
just stages the exact content of the file when you ran the command. If you run git add Makefile
again, the stage version will be overwritten by the version in your working tree, so that all the changes you've made to that are staged.
请注意,文件可以出现在前两个部分中。例如,您可以更改您的Makefile
,然后使用 将其添加到索引中git add Makefile
。然后,如果您继续对 进行更多更改Makefile
,保存它,然后运行git status
,您会看到它同时列在“要提交的更改”和“已更改但未更新”中 - 那是因为(如上所述)git add Makefile
只是阶段运行命令时文件的确切内容。如果您git add Makefile
再次运行,阶段版本将被工作树中的版本覆盖,因此您对其所做的所有更改都会被暂存。
Another couple of tips might be worth adding, about the useful pair of commands git diff
and git diff --cached
- essentially, their meanings are:
提示的另外一对夫妇可能是值得加入,对有用的对命令git diff
和git diff --cached
-本质上,它们的含义是:
git diff
: "Which changes have I not yet staged?"git diff --cached
: "Which changes have I staged already?"
git diff
:“我还没有上演哪些变化?”git diff --cached
:“我已经上演了哪些变化?”
As you can probably work out from the above, that's because git diff
shows the difference between the staging area and your working tree, while git diff --cached
shows the difference between your last commit and the staging area.
正如您可能从上面得出的那样,那是因为git diff
显示了暂存区和工作树git diff --cached
之间的差异,同时显示了上次提交和暂存区之间的差异。
回答by adam_0
You can view the manual page for git commit
by typing git commit --help
(also here).
您可以git commit
通过键入git commit --help
(也在这里)来查看手册页。
In order to add files to commit, use git add
or, if the files are already in the repository, the -a
flag will help you.
为了添加要提交、使用git add
的文件,或者,如果文件已经在存储库中,该-a
标志将帮助您。
-m
tags a message to your commit.
-m
为您的提交标记一条消息。
If you want to selectively choose files, use git add
and then git commit
without the -a
tag (which would add all the changes to files that exist in the repository).
如果您想有选择地选择文件,请使用git add
然后git commit
不使用-a
标签(这会将所有更改添加到存储库中存在的文件)。
I personally use the -s
option as well, which adds signed off by <[email protected]>
at the end, if you have that in your git config. If not, just use git config --global user.email [email protected]
.
我个人也使用该-s
选项signed off by <[email protected]>
,如果您的 git 配置中有该选项,它会在最后添加。如果没有,只需使用git config --global user.email [email protected]
.
回答by amrita
It is simple enough to commit files in git. For adding files, we have following commands
在 git 中提交文件很简单。对于添加文件,我们有以下命令
git add file1 file2
Or if you only want to add all files at a time you can do by
或者,如果您只想一次添加所有文件,则可以通过
git add *
Then, it comes to commit. According to professionals, you are required to add message with commit so that it is easier to track what you have committed. For that you can use
然后,它来到提交。根据专业人士的说法,您需要添加带有提交的消息,以便更容易地跟踪您提交的内容。为此,您可以使用
git commit -m "your message"
If you want to commit any files you have added with git add
, and also commit any files you have changed since then. You can use
如果您想提交您添加的任何文件git add
,并提交您从那时起更改的任何文件。您可以使用
git commit -a
if you want the list of files you have changed and those you still need to add or commit. You can use
如果您想要已更改的文件列表以及仍需要添加或提交的文件列表。您可以使用
git status
And to send your changes to the master branch of your remote repository. The command is
并将您的更改发送到远程存储库的主分支。命令是
git push origin master
Hope this would help you in understanding the concept of git add, commit and push. And solve your problem also.
希望这能帮助您理解 git add、commit 和 push 的概念。并解决您的问题。