Git Pull vs Git Rebase

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/36148602/
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-09 04:03:27  来源:igfitidea点击:

Git Pull vs Git Rebase

git

提问by user4943236

I'm a noob in Git, and trying to learn the difference between git pullvs git rebase. Can someone provide an example when to use which option since I feel that both serve the same purpose.

我是 Git 的菜鸟,并试图了解git pullgit rebase. 有人可以提供一个何时使用哪个选项的示例,因为我觉得两者都有相同的目的。

回答by Peter

git pulland git rebaseare not interchangeable, but they are closely connected.

git pullgit rebase不能互换,但它们紧密相连。

git pullfetches the latest changes of the current branch from a remote and applies those changes to your local copy of the branch. Generally this is done by merging, i.e. the local changes are merged into the remote changes. So git pullis similar to git fetch & git merge.

git pull从远程获取当前分支的最新更改并将这些更改应用到您的本地分支副本。通常这是通过合并完成的,即将本地更改合并到远程更改中。所以git pull类似于git fetch & git merge.

Rebasing is an alternative to merging. Instead of creating a new commit that combines the two branches, it moves the commits of one of the branches on top of the other.

变基是合并的替代方法。它不是创建组合两个分支的新提交,而是将其中一个分支的提交移动到另一个分支的顶部。

You can pull using rebase instead of merge (git pull --rebase). The local changes you made will be rebased on top of the remote changes, instead of being merged with the remote changes.

您可以使用 rebase 而不是 merge ( git pull --rebase)来拉取。您所做的本地更改将重新基于远程更改,而不是与远程更改合并。

Atlassian has some excellent documentation on merging vs. rebasing.

Atlassian 有一些关于 merging vs. rebase 的优秀文档

回答by Tomasz

git-pull - Fetch from and integrate with another repository or a local branch GIT PULL

git-pull - 从另一个存储库或本地分支获取并集成 GIT PULL

Basically you are pulling remote branch to your local, example:

基本上,您将远程分支拉到本地,例如:

git pull origin master

Will pull master branch into your local repository

将主分支拉入您的本地存储库

git-rebase - Forward-port local commits to the updated upstream head GIT REBASE

git-rebase - 转发端口本地提交到更新的上游头 GIT REBASE

This one is putting your local changes on top of changes done remotely by other users. For example:

这是将您的本地更改置于其他用户远程完成的更改之上。例如:

  • You have committed some changes on your local branch for example called SOME-FEATURE
  • Your friend in the meantime was working on other features and he merged his branch into master
  • 您已经在本地分支上提交了一些更改,例如称为 SOME-FEATURE
  • 同时你的朋友正在研究其他功能,他将他的分支合并到 master

Now you want to see his and your changes on your local branch. So then you checkout masterbranch:

现在您想在本地分支上查看他和您的更改。那么你结帐master分支:

git checkout master

then you can pull:

然后你可以拉:

git pull origin master

and then you go to your branch:

然后你去你的分支:

git checkout SOME-FEATURE

and you can do rebase masterto get lastest changes from it and put your branch commits on top:

并且您可以执行 rebasemaster以从中获取最新更改并将您的分支提交放在首位:

git rebase master

I hope now it's a bit more clear for you.

我希望现在对你来说更清楚一点。

回答by Saurin Vala

In a nutshell :

简而言之 :

-> Git Merge: It will simply merge your and remote changes, and that will create another commit history record

-> Git Merge:它将简单地合并您和远程更改,这将创建另一个提交历史记录

-> Git Rebase: It will put your changes above all new remote changes, and rewrite commit history, so your commit history will be much cleaner than git merge. Rebase is a destructive operation. That means, if you do not apply it correctly, you could lose committed work and/or break the consistency of other developer's repositories.

-> Git Rebase:它将您的更改置于所有新的远程更改之上,并重写提交历史,因此您的提交历史将比 git merge 干净得多。Rebase 是一种破坏性操作。这意味着,如果您没有正确应用它,您可能会丢失已提交的工作和/或破坏其他开发人员存储库的一致性。