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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-10 11:00:50  来源:igfitidea点击:

Git: add vs push vs commit

gitrepositorypushcommitadd

提问by CQM

What is the difference between git add, pushand commit?

是什么混帐之间的差异addpush以及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

  1. git addadds your modified files to the queue to be committed later. Files are not committed
  2. git commitcommits 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 with git commit -a

  3. git pushpushes your changes to the remote repository.

  1. git add将修改后的文件添加到队列以供稍后提交。文件未提交
  2. git commit提交已添加的文件并创建一个带有日志的新修订版...如果您不添加任何文件,git 将不会提交任何内容。您可以将这两个操作与git commit -a

  3. git push将您的更改推送到远程存储库。

This figure from this git cheat sheetgives a good idea of the work flow

这个 git 备忘单中的这个数字很好地说明了工作流程

enter image description here

在此处输入图片说明

git addisn't on the figure because the suggested way to commit is the combined git commit -a, but you can mentally add a git addto the change block to understand the flow.

git add不在图中,因为建议的提交方式是组合git commit -a,但您可以在心里添加一个git add到更改块以了解流程。

Lastly, the reason why pushis a separate command is because of git's philosophy. gitis a distributed versioning system, and your local working directory isyour repository! All changes you commit are instantly reflected and recorded. pushis 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 addselects changes

git add选择更改

git commitrecords changes LOCALLY

git commit本地记录更改

git pushshares changes

git push股份变动

回答by Adam Byrtek

  • git addadds files to the Git index, which is a staging area for objects prepared to be commited.
  • git commitcommits the files in the index to the repository, git commit -ais a shortcut to add all the modified tracked files to the index first.
  • git pushsends 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

I find this image very meaningful :

我觉得这张图很有意义:

enter image description here

在此处输入图片说明

(from : Oliver Steele -My Git Workflow (2008))

(来自:奥利弗斯蒂尔 - 我的 Git 工作流程(2008)

回答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

Very nice pdfabout many GIT secrets.

关于许多 GIT 秘密的非常好的pdf

Addis same as svn's add (how ever sometimes it is used to mark file resolved).

添加与 svn 的添加相同(有时它用于标记文件已解决)。

Commitalso is same as svn's , but it commit change into your local repository.

提交也与 svn 相同,但它将更改提交到您的本地存储库。

回答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