Git:添加 vs 推送 vs 提交
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/6143285/
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
Git: add vs push vs commit
提问by CQM
What is the difference between git add
, push
and commit
?
是什么混帐之间的差异add
,push
以及commit
?
Just a little confused coming from SVN, where "update" will 'add' stuff, and commit does a "push" and will 'add' as well
来自 SVN 有点困惑,其中“更新”将“添加”东西,而提交会“推送”并“添加”
There are all different functions within git. Hoping for some explanation from your experience.
git中有所有不同的功能。希望从你的经验中得到一些解释。
回答by abcd
git add
adds your modified files to the queue to be committed later. Files are not committedgit commit
commits the files that have been added and creates a new revision with a log... If you do not add any files, git will not commit anything. You can combine both actions withgit commit -a
git push
pushes your changes to the remote repository.
git add
将修改后的文件添加到队列以供稍后提交。文件未提交git commit
提交已添加的文件并创建一个带有日志的新修订版...如果您不添加任何文件,git 将不会提交任何内容。您可以将这两个操作与git commit -a
git push
将您的更改推送到远程存储库。
This figure from this git cheat sheetgives a good idea of the work flow
这个 git 备忘单中的这个数字很好地说明了工作流程
git add
isn't on the figure because the suggested way to commit is the combined git commit -a
, but you can mentally add a git add
to the change block to understand the flow.
git add
不在图中,因为建议的提交方式是组合git commit -a
,但您可以在心里添加一个git add
到更改块以了解流程。
Lastly, the reason why push
is a separate command is because of git
's philosophy. git
is a distributed versioning system, and your local working directory isyour repository! All changes you commit are instantly reflected and recorded. push
is only used to update the remote repo (which you might share with others) when you're done with whatever it is that you're working on. This is a neat way to work and save changes locally (without network overhead) and update it only when you want to, instead of at every commit. This indirectly results in easier commits/branching etc (why not, right? what does it cost you?) which leads to more save points, without messing with the repository.
最后,为什么push
是一个单独的命令是因为git
的哲学。git
是一个分布式版本控制系统,您的本地工作目录就是您的存储库!您提交的所有更改都会立即反映出来并记录下来。push
仅用于在完成您正在处理的任何内容后更新远程存储库(您可能会与他人共享)。这是一种在本地工作和保存更改(没有网络开销)并仅在需要时更新它的巧妙方法,而不是在每次提交时更新。这间接导致更容易提交/分支等(为什么不,对吧?它会花费你多少?)这会导致更多的保存点,而不会弄乱存储库。
回答by Dustin
git add
selects changes
git add
选择更改
git commit
records changes LOCALLY
git commit
本地记录更改
git push
shares changes
git push
股份变动
回答by Adam Byrtek
git add
adds files to the Git index, which is a staging area for objects prepared to be commited.git commit
commits the files in the index to the repository,git commit -a
is a shortcut to add all the modified tracked files to the index first.git push
sends all the pending changes to the remote repository to which your branch is mapped (eg. on GitHub).
git add
将文件添加到 Git 索引,这是准备提交的对象的暂存区。git commit
将索引中的文件提交到存储库,git commit -a
是将所有修改过的跟踪文件首先添加到索引的快捷方式。git push
将所有挂起的更改发送到您的分支映射到的远程存储库(例如,在 GitHub 上)。
In order to understand Git you would need to invest more effort than just glancing over the documentation, but it's definitely worth it. Just don't try to map Git commands directly to Subversion, as most of them don't have a direct counterpart.
为了理解 Git,您需要投入更多的精力,而不仅仅是浏览文档,但这绝对是值得的。只是不要尝试将 Git 命令直接映射到 Subversion,因为它们中的大多数没有直接对应物。
回答by Adam Byrtek
I was confused about what 'add' really does. I just read a very enlightening paragraph from the book Git Pro that I'd like to add here, because it clarifies things
我对“添加”的真正作用感到困惑。我刚刚从 Git Pro 一书中读到了一段非常有启发性的段落,我想在这里添加它,因为它澄清了一些事情
“It turns out that Git stages a file exactly as it is when you run the git add command. If you commit now, the version of benchmarks.rb as it was when you last ran the git add command is how it will go into the commit, not the version of the file as it looks in your working directory when you run git commit. If you modify a file after you run git add, you have to run git add again to stage the latest version of the file:”
“事实证明,当您运行 git add 命令时,Git 会完全按照原样暂存文件。如果你现在提交,当你上次运行 git add 命令时的 benchmarks.rb 版本是它将如何进入提交,而不是当你运行 git commit 时它在你的工作目录中看起来的文件版本。如果在运行 git add 后修改文件,则必须再次运行 git add 以暂存文件的最新版本:”
Excerpt From: Chacon, Scott. “Pro Git.” Springer, 2009-08-19T00:00:00+00:00. iBooks. This material may be protected by copyright.
摘自:Chacon,Scott。“Pro Git。” 斯普林格,2009-08-19T00:00:00+00:00。电子书。该材料可能受版权保护。
回答by Malick
回答by hvgotcodes
add tells git to start tracking a file.
add 告诉 git 开始跟踪文件。
commit commits your current changes on your local repository
commit 在本地存储库上提交您当前的更改
push pushes you local repo upstream.
push 将您本地回购推向上游。
回答by lord_t
回答by Herbert
add -in git is used to tell git which files we want to commit, it puts files to the staging area
add -in git 用于告诉 git 我们要提交哪些文件,它将文件放入暂存区
commit- in git is used to save files on to local machine so that if we make any changes or even delete the files we can still recover our committed files
commit-in git 用于将文件保存到本地机器上,这样如果我们进行任何更改甚至删除文件,我们仍然可以恢复我们提交的文件
push - if we commit our files on the local machine they are still prone to be lost if our local machine gets lost, gets damaged, etc, to keep our files safe or to share our files usually we want to keep our files on a remote repository like Github. To save on remote repositories we use push
push - 如果我们在本地机器上提交我们的文件,如果我们的本地机器丢失、损坏等,它们仍然容易丢失,以确保我们的文件安全或共享我们的文件通常我们希望将我们的文件保存在远程像 Github 这样的存储库。为了节省远程存储库,我们使用推送
example Staging a file named index.html git add index.html
示例暂存名为 index.html 的文件 git add index.html
Committing a file that is staged git commit -m 'name of your commit'
提交已暂存的文件 git commit -m '您提交的名称'
Pushing a file to Github git push origin master
将文件推送到 Github git push origin master