git 如何使用远程主服务器重新设置本地分支

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

How to rebase local branch with remote master

gitclonegit-rebase

提问by Damir

I have a cloned project from a master branch from remote repository remote_repo. I create a new branch and I commit to that branch. Other programmers pushed to remote_repoto the master branch.

我有一个来自远程存储库的 master 分支的克隆项目remote_repo。我创建了一个新分支并提交到该分支。其他程序员推送到remote_repomaster 分支。

I need now to rebase my branch RB onto remote_repomaster.

我现在需要将我的分支 RB 重新设置为remote_repomaster。

How to do this? What commands to type to a terminal?

这该怎么做?终端输入什么命令?

回答by Frerich Raabe

First fetch the new master from the upstream repository, then rebase your work branch on that:

首先从上游存储库中获取新的 master,然后在其上重新设置您的工作分支:

git fetch origin            # Updates origin/master
git rebase origin/master    # Rebases current branch onto origin/master

Update: Please see Paul Draper's answerfor a more concise way to do the same - recent Git versions provide a simpler way to do the equivalent of the above two commands.

更新:请参阅Paul Draper 的回答以获取更简洁的方法 - 最近的 Git 版本提供了一种更简单的方法来执行上述两个命令的等效操作。

回答by Paul Draper

git pull --rebase origin master
# where --rebase[=(false|true|merges|preserve|interactive)]

回答by CharlesB

After committing changes to your branch, checkout masterand pull it to get its latest changes from the repo:

将更改提交到您的分支后,签出master并拉取它以从存储库中获取最新更改:

git checkout master
git pull origin master

Then checkout your branch and rebase your changes on master:

然后检查您的分支并将您的更改重新设置为master

git checkout RB
git rebase master

...or last two commands in one line:

...或一行中的最后两个命令:

git rebase master RB

When trying to push back to origin/RB, you'll probably get an error; if you're the only one working on RB, you can force push:

当试图推回到 时origin/RB,你可能会得到一个错误;如果你是唯一一个工作的人RB,你可以强制推送:

git push --force origin RB

...or as follows if you have git configured appropriately:

...或者如下所示,如果您正确配置了 git:

git push -f

回答by bh4r4th

Note:If you have broad knowledge already about rebase then use below one liner for fast rebase. Solution:Assuming you are on your working branch and you are the only person working on it.

注意:如果您已经对 rebase 有广泛的了解,那么使用下面的一个衬垫来快速 rebase。 解决方案:假设你在你的工作分支上并且你是唯一在它上面工作的人。

git fetch && git rebase origin/master

Resolve any conflicts, test your code, commit and push new changes to remote branch.

解决任何冲突,测试您的代码,提交并将新更改推送到远程分支。

                            ~:   For noobs   :~

The following steps might help anyone who are new to git rebaseand wanted to do it without hassle

以下步骤可能对任何新手git rebase并希望轻松完成的人有所帮助

Step 1:Assuming that there are no commits and changes to be made on YourBranch at this point. We are visiting YourBranch.

第 1 步:假设此时没有在 YourBranch 上进行提交和更改。我们正在访问 YourBranch。

git checkout YourBranch
git pull --rebase

What happened?Pulls all changes made by other developers working on your branch and rebases your changes on top of it.

发生了什么?提取在您的分支上工作的其他开发人员所做的所有更改,并在其基础上重新设置您的更改。

Step 2:Resolve any conflicts that presents.

第 2 步:解决出现的任何冲突。

Step 3:

第 3 步:

git checkout master
git pull --rebase

What happened?Pulls all the latest changes from remote master and rebases local master on remote master. I always keep remote master clean and release ready! And, prefer only to work on master or branches locally. I recommend in doing this until you gets a hand on git changes or commits. Note: This step is not needed if you are not maintaining local master, instead you can do a fetch and rebase remote master directly on local branch directly. As I mentioned in single step in the start.

发生了什么?从远程主服务器拉取所有最新更改,并在远程主服务器上重新设置本地主服务器。我总是保持远程主清洁并准备好发布!而且,更喜欢只在本地处理 master 或分支。我建议这样做,直到您掌握 git 更改或提交。注意:如果您不维护本地 master,则不需要此步骤,而是可以直接在本地分支上直接执行 fetch 和 rebase 远程 master。正如我在开始时单步提到的那样。

Step 4:Resolve any conflicts that presents.

第 4 步:解决出现的任何冲突。

Step 5:

第 5 步:

git checkout YourBranch
git rebase master

What happened?Rebase on master happens

发生了什么?发生基于 master 的 rebase

Step 6:Resolve any conflicts, if there are conflicts. Use git rebase --continueto continue rebase after adding the resolved conflicts. At any time you can use git rebase --abortto abort the rebase.

第 6 步:解决任何冲突(如果存在冲突)。用于git rebase --continue在添加已解决的冲突后继续变基。您可以随时使用git rebase --abort中止变基。

Step 7:

第 7 步:

git push --force-with-lease 

What happened?Pushing changes to your remote YourBranch. --force-with-leasewill make sure whether there are any other incoming changes for YourBranch from other developers while you rebasing. This is super useful rather than force push. In case any incoming changes then fetch them to update your local YourBranch before pushing changes.

发生了什么?将更改推送到您的远程 YourBranch。--force-with-lease将确保在您变基时是否有其他开发人员对 YourBranch 的任何其他传入更改。这非常有用,而不是强制推送。如果有任何传入更改,则在推送更改之前获取它们以更新您的本地 YourBranch。

Why do I need to push changes?To rewrite the commit message in remote YourBranch after proper rebase or If there are any conflicts resolved? Then you need to push the changes you resolved in local repo to the remote repo of YourBranch

为什么我需要推送更改?在适当的变基后重写远程 YourBranch 中的提交消息,或者是否解决了任何冲突?然后你需要将你在本地 repo 中解决的更改推送到 YourBranch 的远程 repo

Yahoooo...! You are succesfully done with rebasing.

呜呜呜……!您已成功完成变基。

You might also be looking into doing:

您可能还在考虑这样做:

git checkout master
git merge YourBranch

When and Why?Merge your branch into master if done with changes by you and other co-developers. Which makes YourBranch up-to-date with master when you wanted to work on same branch later.

什么时候和为什么?如果您和其他合作开发人员进行了更改,则将您的分支合并到 master 中。当您以后想在同一个分支上工作时,这使 YourBranch 与 master 保持同步。

                            ~:   (?? ? ?)? rebase   :~

回答by GauthamManivannan

Step 1:

第1步:

git fetch origin

Step 2:

第2步:

git rebase origin/master

Step 3:(Fix if any conflicts)

第 3 步:(如果有冲突,请修复)

git add .

Step 4:

第四步:

git rebase --continue

Step 5:

第 5 步:

git push --force

回答by N Djel Okoye

1.Update Master first...

1.先更新Master...

git checkout [master branch]
git pull [master branch]

2.Now rebase source-branch with master branch

2.现在用主分支rebase source-branch

git checkout [source branch]
git rebase [master branch]
git pull [source branch] (remote/source branch)
git push [source branch]

IF source branch does not yet exist on remote then do:

如果远程上尚不存在源分支,则执行以下操作:

git push -u origin [source branch]

"et voila..."

“等等,瞧……”

回答by Naz

git fetch origin master:masterpulls the latest version of master without needing to check it out.

git fetch origin master:master无需检查即可拉取最新版本的 master。

So all you need is:

所以你只需要:

git fetch origin master:master && git rebase master

git fetch origin master:master && git rebase master