简体   繁体   English

来自远程主服务器的 Git Rebase

[英]Git Rebase from Remote Master

This may have been asked before but I am a little confused on the terminology, so I am not sure what command(s) are the correct ones to accomplish what I need to accomplish.之前可能有人问过这个问题,但我对术语有点困惑,所以我不确定哪些命令是正确的,可以完成我需要完成的任务。 Right now I have a GitHub repository I am working from with my team.现在我有一个与我的团队一起工作的 GitHub 存储库。 On there, I made a branch for a task.在那里,我为一项任务创建了一个分支。 I cloned the repository to my local machine and then checked out that branch locally.我将存储库克隆到我的本地机器,然后在本地签出该分支。 I started making changes that I wish to continue working on... While making these changes, my team has been updating their code and pushing changes back into the master branch on GitHub.我开始进行我希望继续进行的更改......在进行这些更改时,我的团队一直在更新他们的代码并将更改推送回 GitHub 上的主分支。 How do I pull down their changes from the master branch on GitHub so that it updates my branch locally AND on GitHub as well (Right now on GitHub, it says I am 1 commit ahead and 19 commits behind master).我如何从 GitHub 上的 master 分支中提取他们的更改,以便它在本地和 GitHub 上更新我的分支(现在在 GitHub 上,它说我提前 1 次提交,落后于 master 19 次提交)。 Thanks for the advice here...this will be something I plan to do regularly so learning the right process would be instrumental for me...感谢这里的建议......这将是我计划定期做的事情,所以学习正确的过程对我来说很有帮助......

There are a series of methods you could apply here to accomplish what you want.您可以在此处应用一系列方法来完成您想要的操作。 And that may depend on your repository policy.这可能取决于您的存储库策略。 For instance, if you rebase your branch you'll not be able to push it back to the server, you would have to either push it using -f or deleting the remote branch and push a new one.例如,如果您重新设置分支的基础,您将无法将其推送回服务器,您必须使用 -f 将其推送或删除远程分支并推送一个新分支。 Some could say rebase is not a good approach once you have already pushed you branch to the server, if that's the policy of your repo so you would have to merge the master into your branch and than push it back to the remote.有些人可能会说,一旦您已经将分支推送到服务器,rebase 就不是一个好方法,如果这是您的 repo 的策略,那么您必须将 master 合并到您的分支中,而不是将其推送回远程。

Let's talk about both approaches:让我们谈谈这两种方法:

Rebase:变基:

I personally like doing rebase more, but only if I'm sure no one else is using the branch which I'm pushing to on the remote.我个人更喜欢做 rebase,但前提是我确定没有其他人在使用我推送到远程的分支。 I like rebase because this ends up leaving a cleaner history, it does that because it removes my changes from the branch, applies the remote changes and then apply my changes back on top, but, doing that, it changes the commit hashes and it does not match with the ones you had previously for the same commit, for that reason you have to either push it using -f or delete the remote branch and creating it back again.我喜欢变基,因为这最终会留下更清晰的历史记录,这样做是因为它从分支中删除了我的更改,应用远程更改,然后将我的更改重新应用到顶部,但是,这样做,它会更改提交哈希,并且确实如此与您之前为同一提交所做的不匹配,因此您必须使用 -f 推送它或删除远程分支并重新创建它。

I would do like this, on your local repository I would go to the master branch:我会这样做,在您的本地存储库中,我会转到 master 分支:

git checkout master

then update it with the remote changes:然后使用远程更改更新它:

git pull

It will run a git fetch with merge behind the scenes on your git master branch.它将在您的 git master 分支上在幕后运行 git fetch with merge 。

Then go back to you branch:然后回到你的分支:

git checkout my_branch

and then rebase it with the master:然后使用 master 对其进行重新设置:

git rebase master

If everything runs without conflict you're good to go.如果一切运行都没有冲突,你就可以开始了。

you can either use:您可以使用:

git push -f origin my_branch

(BE CAREFUL! this command will override your remote branch, it's very dangerous make sure everything went fine on your rebase before using this command.) (小心!这个命令会覆盖你的远程分支,这是非常危险的,在使用这个命令之前确保你的 rebase 一切正常。)

or you could delete your remote branch end push it normally back to the server:或者您可以删除远程分支端将其正常推送回服务器:

git push origin my_branch

Merge:合并:

Using this approach you'll end up with a merge commit on your history, and the log can get confusing over time, but there are some advantages on this as well, if you do have a conflict, you need to resolve it only once, when you're merging, if you have a conflict when you're rebasing, you may have to resolve it multiple times, since it will try to apply your commits over the new remote commits one by one.使用这种方法,您最终会在您的历史记录中进行合并提交,并且随着时间的推移,日志可能会变得混乱,但是这也有一些优点,如果确实有冲突,则只需解决一次,当您合并时,如果您在变基时遇到冲突,您可能需要多次解决它,因为它会尝试将您的提交一一应用于新的远程提交。

To do this you can just go to your master branch and update it:为此,您只需转到主分支并更新它:

git checkout master

and:和:

git pull

will do the same as we saw earlier和我们之前看到的一样

now go back to your branch:现在回到你的分支:

git checkout my_branch

and run:并运行:

git merge master

if you do not have any conflict you're good to go and you can push it to the server, if you do you only have to resolve it and you can push it normally to the server as well since now you resolved the conflicts on a new commit, there is nothing diverging between your local branch and your remote one.如果您没有任何冲突,您就可以开始了,您可以将其推送到服务器,如果您这样做了,您只需解决它并且您也可以将其正常推送到服务器,因为现在您解决了冲突新提交,您的本地分支和远程分支之间没有任何区别。

git push origin my_branch

And that's it, I hope you find this helpful, you can probably do this in fewer steps, but these are just the steps I like to make, they may not be the shorter ones but I feel safer this way.就是这样,我希望你觉得这有帮助,你可能可以用更少的步骤来做到这一点,但这些只是我喜欢做的步骤,它们可能不是较短的步骤,但我觉得这样更安全。 And of course there are lots of options on merging and rebasing I would advise you to look more into it they are very useful.当然,合并和重新定位有很多选择,我建议您多研究一下,它们非常有用。

That would be help:那会有所帮助:

$ git config --global branch.master.rebase true

or或者

# cat ~/.gitconfig
[user]
        name = xx
        email = xx@xx.com
[branch "master"]
        rebase = true

Thanks.谢谢。

How do I pull down their changes from the master branch on GitHub so that it updates my branch locally我如何从 GitHub 上的 master 分支下拉他们的更改,以便它在本地更新我的分支

git checkout master
git pull
git checkout <your-branch>
git rebase master

AND on GitHub as well (Right now on GitHub, it says I am 1 commit ahead and 19 commits behind master).并且在 GitHub 上也是如此(现在在 GitHub 上,它说我领先 1 次提交,落后于主控者 19 次提交)。

git commit -m "I do this, do that"

upload your branch to remote将您的分支上传到远程

git push -u <remote-name> <your-branch-on-remote>

merge to master and upload your update on master to remote合并到 master 并将 master 上的更新上传到远程

git checkout master
git merge --no-ff <your branch>
git push -u <remote-name> master

在一行中执行此操作的最佳方法...

git pull origin master --rebase

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM