Git Pull vs Git Rebase头部版本 [英] Git pull vs Git Rebase head version

查看:66
本文介绍了Git Pull vs Git Rebase头部版本的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

通常,这就是我如何推送我的代码以进行合并

Usually this I how I push my code for merging to master

git pull origin master
git add -A
git commit -m 'message'
git push

后来发现pull不是拥有线性历史记录的正确方法,我应该重新设置基础然后再进行推送

Later found pull is not the right way to have a linear history, I was supposed to rebase and then push

我目前正在做什么

git checkout LocalBranch
git rebase master[resolve conflicts]
git add -A
git commit -m 'message'
git push

问题:

当我使用方法1做事时,它说已经掌握了最新信息如果我使用方法2,它将引发很多冲突,并且在解决之后,我将无法推送代码.

ISSUE:

When I do things using method 1 it says already upto date with master If I go with method 2 it throws lot of conflicts and after resolving I'm unable to push the code.

  1. 为什么 git pull master(5abc) git rebase master(4def)指向master的不同头部版本.
  2. git pull git rebase 不会随时获得相同版本的master吗?
  1. Why does git pull master(5abc) and git rebase master(4def) point to different head versions of master.
  2. git pull and git rebase won't get to the same version of master anytime?

推荐答案

我认为您需要弄清 git pull 的工作.从远程 ie 中提取代码的位置(可以是GitHub,GitLab,BitBucket等),导致git进行 git fetch git merge .那是什么意思?

I think you need to be clear with the working of git pull. Pulling from the remote i.e. the location of a code(can be GitHub, GitLab, BitBucket, etc.) causes git to git fetch and git merge. Well what does that mean?

这意味着它首先获取代码与远程代码之间的差异(例如,假设远程有两个额外"提交),然后尝试合并将这些差异添加到您的代码库中.现在,如果这两个提交与您在代码上所做的提交(例如,创建新文件)无关,则可以顺利进行.但是,如果在同一部分代码中发生了更改的情况,可以说您的代码是:

It means that it first fetches the difference between your code and the remote's code(for example let's say there are two "extra" commits on the remote), then tries to merge those differences into your codebase. Now, this can go smoothly in case of those 2 commits being unrelated to the commits you made on your code(say creation of a new file(s)). But, if there is a case of a change in the same part of the code, let's say your code was:

int a = 4;
bool x = false;

但是合并想要实现:

int a = 5;
bool x = false;

这会导致合并冲突,因为您的代码说 a 是4,但是合并说是5,git应该考虑哪个?因此,在提取代码时会提示合并冲突.您需要手动解决它们才能使工作正常.

This creates a merge conflict because your code says a is 4 but the merge says its 5, which one should git consider? Hence you get the prompt of a merge conflict when pulling codes. You need to solve them manually to make things work.

git rebase master 可帮助您将在 LocalBranch 中的提交重新应用到master.这意味着它需要您的工作树,并尝试将其与主工作树融合.有关此过程的详细分析,请参见.变基意味着所做的任何提交,所有提交都将被应用在master之上(或者甚至在您需要的时候也可以应用在它们之间).请观看视频,以获得更好的主意.由于此过程是分步进行的,因此将您带到另一个提交上(该提交将首先被重新基础).

git rebase master helps you re-apply your commits in LocalBranch onto the master. What this means is that it takes your working tree and tries to meld it with the master working tree. See this for a detailed analysis of this process. Rebasing would mean that whatever commits you made, all of them will be applied on top of the master(or maybe even in between if you want ;) ). Please see this video to get a better idea. As this process is a step-by-step one, it lands you onto a different commit(the one which was to be rebased first).

git pull (特别是 merge 步骤)将获取差异"提交,并尝试将其"合并"合并到您的主工作树中.只有完成此过程后,您才能进行 add commit ,最后进行 push

git pull(the merge step in particular) will take "difference" commits and try to merge it into your master work tree. Only after the completion of this process you can do an add and a commit and finally push

另外,我想给您一个提示,使用一个交互式rebase ie git rebase -i 进行rebase,因为它为您提供了一个交互式界面来帮助您重新设定.

Also, a tip I would like to give, use an interative rebase i.e. git rebase -i to do a rebase as it provides you an interactive interface to help you rebase.

如果还有更多困惑,请随时发表评论!:)

Feel free to comment if there is any more confusion still! :)

最佳

PS: rebase 的情况下,您可以强制执行以将更改推送到遥控器上.

PS: you can do a force push in case of the rebase to be able to push the changes onto the remote.

这篇关于Git Pull vs Git Rebase头部版本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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