简体   繁体   English

如何将未发布的提交从一个分支移动到另一个分支?

[英]How to move unpublished commits from one branch to another?

I have committed my changes on my local master and was about to create a new feature branch for these commits.我已经在我的本地 master 上提交了我的更改,并且即将为这些提交创建一个新的功能分支。 The local master is up-to-date with the master on my remote.本地主机与我遥控器上的主机是最新的。 I forgot that the branch already exists both locally and on my remote, so this happened:我忘记了该分支已经存在于本地和我的遥控器上,所以发生了这种情况:

git checkout -b myFeatureBranch
fatal: A branch named 'myFeatureBranch' already exists.

The branch is based on an old master and includes couple of commits.该分支基于旧的 master 并包括几个提交。

My goal is to get rid of the old commits on that branch, rebase the feature branch on the current master and apply my new commits that are on my local master branch.我的目标是摆脱该分支上的旧提交,在当前主分支上重新设置功能分支并应用我在本地主分支上的新提交。

What's an elegant way to do this?什么是优雅的方式来做到这一点? I think a not so elegant way would be to:我认为不太优雅的方法是:

  1. Checkout the old feature branch签出旧的功能分支
  2. Rebase it on origin/master重新基于原点/主
  3. Replace the files I have edited with the most recent version of themselves (the version of my local master)将我编辑过的文件替换为自己的最新版本(我的本地主版本)
  4. force push (Im the only one working on that branch)强制推送(我是唯一在该分支上工作的人)
  5. 'force pull' master to get rid of my changes that I now have on the feature branch 'force pull' master 摆脱我现在在功能分支上所做的更改

I'm on git version 2.18.1 (most recent available release on Centos 8) and prefer to use the CLI.我使用的是 git 版本 2.18.1(Centos 8 上的最新可用版本)并且更喜欢使用 CLI。

My goal is to get rid of the old commits on that branch, rebase the feature branch on the current master and apply my new commits that are on my local master branch.我的目标是摆脱该分支上的旧提交,在当前主分支上重新设置功能分支并应用我在本地主分支上的新提交。

I think the first thing to do is clear up some confusion here.我认为首先要做的是清除这里的一些混乱。 If you've removed the commits from the feature branch, then there is nothing to rebase.如果您已经从功能分支中删除了提交,那么就没有什么可以变基的了。 rebase is something you do to commits; rebase是你对提交所做的事情; it means "make a commit (usually with a different parent than the old commit) whose patch relative to its parent is the same as the old commit's patch relative to its parent".这意味着“进行一个提交(通常使用与旧提交不同的父提交),其相对于其父提交的补丁与相对于其父提交的旧提交补丁相同”。

When people say they are rebasing a branch, they mean they are rebasing the commit(s) from the end of that branch, and then moving the branch to the new copies of the commits.当人们说他们正在重新设置分支时,他们的意思是他们正在重新设置从该分支末尾的提交,然后将分支移动到提交的新副本。

And to be fair, you can use the rebase command without rewriting any commits, in which case it just moves the branch - which works out conveniently in cases where you don't want to worry about whether there are any commits to rewrite or not.公平地说,您可以在不重写任何提交的情况下使用 rebase 命令,在这种情况下,它只会移动分支 - 在您不想担心是否有任何提交要重写的情况下,这很方便。

But it's still worth understanding these details, because if you realize that you just want to move the branch without any commits, there are easier ways to do it.但是了解这些细节仍然值得,因为如果您意识到您只想在没有任何提交的情况下移动分支,那么有更简单的方法来做到这一点。 (You don't even have to explicitly delete the old commits; it's fine to just leave them behind when moving the branch, if they are in fact no longer needed.) (您甚至不必显式删除旧提交;如果实际上不再需要它们,则在移动分支时将它们留在后面即可。)

The one caveat is that if you remove the old commits from the branch (whether by rebasing - which replaces those commits with new copies - or by just moving the branch or by any other means) then all other repos have to deal with an "upstream rebase" condition (see the git rebase docs) and, if they do it wrong, they may undo your work.一个警告是,如果您从分支中删除旧提交(无论是通过变基 - 用新副本替换这些提交 - 或者只是移动分支或通过任何其他方式),那么所有其他存储库都必须处理“上游rebase”条件(请参阅 git rebase 文档),如果他们做错了,他们可能会撤消您的工作。

That said, the simplest thing to do is to replace your checkout -b command with branch -f - ie instead of creating the branch, force it to move.也就是说,要做的最简单的事情是用branch -f替换checkout -b命令 - 即不是创建分支,而是强制它移动。 With the new commit checked out (just as it would've been for the checkout -b ):签出新提交后(就像签出checkout -b ):

git branch -f myFeatureBranch
git push -f
# all other clones must now recover from upstream rebase

You could just try resetting your feature branch to master :您可以尝试将您的功能分支重置为master

# from myFeatureBranch
git reset --hard master

After this step, your feature branch would be starting off at whatever point in which the local master branch happens to be.在这一步之后,您的功能分支将在本地master分支所在的任何点开始。

Note that you would then need to force push your reset feature branch to the remote via:请注意,您需要通过以下方式将重置功能分支强制推送到远程:

git push --force origin myFeatureBranch

A. If you need your old branch commits, then you can simply create another branch instead of myFeatureBranch : A. 如果您需要旧分支提交,那么您可以简单地创建另一个分支而不是myFeatureBranch

git checkout -b myFeatureBranch2

And then you can push your new branch and do what you want with that branch (eg. give a pull request to master).然后你可以推送你的新分支并使用该分支做你想做的事情(例如,向 master 发出拉取请求)。

B. But if you don't need your old branch commits (neither locally or remotely) you can delete old branch and then use that branch name: B. 但是,如果您不需要旧分支提交(本地或远程),您可以删除旧分支,然后使用该分支名称:

git push -d origin myFeatureBranch
git branch -d myFeatureBranch
git checkout -b myFeatureBranch

C. If you have some useful commits on old myFeatureBranch, and don't want to miss them, then you can go to that old branch and rebase it with master (containing new commits) and push it: C. 如果你在旧的 myFeatureBranch 上有一些有用的提交,并且不想错过它们,那么你可以去那个旧的分支并用 master(包含新的提交)重新设置它并推送它:

git checkout myFeatureBranch
git rebase -i master
git push -f
git checkout master
git fetch origin
git reset --hard origin/master

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

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