简体   繁体   English

如何将本地分支变基到远程主机

[英]How to rebase local branch onto remote master

I have a cloned project from a master branch from remote repository remote_repo .我有一个来自远程存储库remote_repo主分支的克隆项目。 I create a new branch and I commit to that branch.我创建了一个新分支并提交给该分支。 Other programmers pushed to remote_repo to the master branch.其他程序员将remote_repo推送到 master 分支。

I now need to rebase my local branch RB onto remote_repo 's master branch.我现在需要将我的本地分支RB变基到remote_repomaster分支上。

How to do this?这该怎么做? What commands to type to a terminal?向终端输入什么命令?

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 answer for 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 版本提供了一种更简单的方法来执行上述两个命令的等效操作。

git pull --rebase origin master

After committing changes to your branch, checkout master and pull it to get its latest changes from the repo:将更改提交到您的分支后,签出master并将其拉取以从 repo 中获取其最新更改:

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;当试图推回到origin/RB ,你可能会得到一个错误; if you're the only one working on RB , you can force push:如果你是唯一一个在RB工作的人,你可以强制推送:

git push --force origin RB

...or as follows if you have git configured appropriately: ...或者如下所示,如果您正确配置了 git:

git push -f

Note: If you have broad knowledge already about rebase then use below one liner for fast rebase.注意:如果您已经对 rebase 有广泛的了解,那么使用下面的一个衬垫来快速 rebase。 Solution: Assuming you are on your working branch and you are the only person working on it.解决方案:假设你在你的工作分支上并且你是唯一在它上面工作的人。

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 rebase and 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.第 1 步:假设此时没有在 YourBranch 上进行提交和更改。 We are visiting 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.并且,更喜欢只在本地处理 master 或分支。 I recommend in doing this until you gets a hand on git changes or commits.我建议这样做,直到您掌握 git 更改或提交。 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.注意:如果您不维护本地 master,则不需要此步骤,而是可以直接在本地分支上直接执行 fetch 和 rebase 远程 master。 As I mentioned in single step in the start.正如我在开始时单步提到的那样。

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.第 6 步:解决任何冲突(如果存在冲突)。 Use git rebase --continue to continue rebase after adding the resolved conflicts.添加已解决的冲突后,使用git rebase --continue继续变基。 At any time you can use git rebase --abort to abort the rebase.您可以随时使用git rebase --abort中止变基。

Step 7:第 7 步:

git push --force-with-lease 

What happened?发生了什么? Pushing changes to your remote YourBranch.将更改推送到您的远程 YourBranch。 --force-with-lease will make sure whether there are any other incoming changes for YourBranch from other developers while you rebasing. --force-with-lease将确保在您变基时是否有其他开发人员对 YourBranch 的任何其他传入更改。 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。

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?在适当的变基后重写远程 YourBranch 中的提交消息,或者是否解决了任何冲突? Then you need to push the changes you resolved in local repo to the remote repo of 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.如果您和其他合作开发人员进行了更改,则将您的分支合并到 master 中。 Which makes YourBranch up-to-date with master when you wanted to work on same branch later.当您以后想在同一个分支上工作时,这使 YourBranch 与 master 保持同步。

                            ~:   (๑ơ ₃ ơ)♥ rebase   :~

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

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..." “等等,瞧……”

git fetch origin master:master pulls 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 👌

If the current branch has a lot of commits and they are needed to be squashed, fixed, and rewritten before rebasing, then interactive rebase is the correct answer.如果当前分支有很多提交,并且需要在 rebase 之前压缩、修复和重写它们,那么交互式 rebase 是正确的答案。 When software engineers say "rebase on top of master", what they usually mean is "do interactive rebase on top of origin/master and make sure it looks great and unnecessary commits are squashed, and commit messages are corrected".当软件工程师说“在 master 之上 rebase”时,他们通常的意思是“在 origin/master 之上进行交互式 rebase,并确保它看起来很棒,并压缩了不必要的提交,并更正了提交消息”。

First, check git status and make sure to start in feature branch.首先,检查git status并确保从功能分支开始。

If not in feature brach, try git checkout feature Then如果不在功能分支中,请尝试git checkout feature然后

git fetch origin
git rebase -i origin/master

Rarely, a commit history is ready to be rebased as is when rebase on top of master is requested.很少有提交历史准备好重新定位,因为当请求在 master 之上重新定位时。 In most cases, the existing commits are first revised using the interactive rebase.在大多数情况下,首先使用交互式 rebase 修改现有提交。

simple solution:简单的解决方案:

git checkout master && git pull

git checkout branch

git rebase master -> resolve conflicts if any git rebase master -> 解决冲突(如果有的话)

git add.

git rebase --continue

git push --force-with-lease origin branch

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

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