清理 git master 分支并将一些提交移动到新分支?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/5916329/
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
Cleanup git master branch and move some commit to new branch?
提问by Manfred Moser
I have a clone of a repo on Github in which I created a new feature for upstream. The problem is, I did that in my master branch which also contains other stuff I pulled in from other clones. That totally broke my ability to create a reasonable pull request.
我在 Github 上有一个 repo 的克隆,我在其中为上游创建了一个新功能。问题是,我在我的主分支中这样做了,该分支还包含我从其他克隆中提取的其他内容。这完全破坏了我创建合理拉取请求的能力。
So I want to do the following:
所以我想做以下事情:
- Restore my master branch to be exactly the same as upstream master.
- Create a new branch.
- Move some of my old commits to the new branch.
- Create a pull request off the branch.
- 将我的 master 分支恢复为与上游 master 完全相同。
- 创建一个新分支。
- 将我的一些旧提交移至新分支。
- 从分支创建拉取请求。
And, in the future, I will do all my work in branches and create pull requests off them while leaving my master branch alone and just merging down whatever comes in from upstream.
并且,在未来,我将在分支中完成我的所有工作,并从它们中创建拉取请求,同时离开我的主分支,并合并来自上游的任何内容。
My questions are:
我的问题是:
- Is this a reasonable approach?
- How would I actually do steps 1 and 3?
- 这是一个合理的方法吗?
- 我实际上将如何执行第 1 步和第 3 步?
回答by wewals
Make a new branch to hold stuff
创建一个新分支来存放东西
$ git branch old_master
Send to remote for backup (just incase)
发送到远程备份(以防万一)
$ git checkout old_master
$ git push origin old_master
Reset local master to the commit before you started modifying stuff
在开始修改内容之前将本地 master 重置为提交
$ git checkout master
$ git reset --hard 037hadh527bn
Merge in changes from upstreammaster
合并来自上游master 的更改
$ git pull upstream master
Now DELETE master on remote repo
现在删除远程仓库上的主人
On github this won't work without first going into the admin section for the fork and setting the default branch to something other than master temporarily as they try and protect you from blowing stuff away.
在 github 上,如果没有首先进入 fork 的管理部分并将默认分支暂时设置为 master 以外的其他东西,这将无法工作,因为他们试图保护你免受吹走的东西。
$ git push origin :master
And recreate it
并重新创建它
$ git push origin master
On github you should now set the default branch back to master
在 github 上,您现在应该将默认分支设置回 master
回答by Phil Miller
This is almost a reasonable approach, but you're possibly taking things a bit out of order. First thing to do is it create a new branch where your current master
points, so that you don't lose the convenient reference to the work you've already done:
这几乎是一种合理的方法,但您可能会认为事情有点乱。首先要做的是在当前master
指向的位置创建一个新分支,这样您就不会丢失对已经完成的工作的方便引用:
git branch mywork-orig master
After that, you can reset master
to upstream's view (assuming you have master
checked out):
之后,您可以重置master
为上游视图(假设您已master
签出):
git reset --hard origin/master
Then you can make you own branch with the intended changes:
然后,您可以使用预期的更改创建自己的分支:
git checkout -b mywork
Make the changes that you want (cherry-pick them from mywork-orig, etc.), and send a pull request for that.
进行您想要的更改(从 mywork-orig 等中挑选它们),并为此发送拉取请求。
回答by Trevor Norris
This is late, but didn't see anyone suggest this much simpler method:
这很晚了,但没有看到有人建议这种更简单的方法:
# make sure we're in master
git checkout master
# create new branch from current master
git branch tmp_master
# grab most recent changes from upstream w/o applying them
git fetch upstream
# force reset local master to match upstream/master
git reset --hard upstream/master
You saved your local changes to tmp_master
and have forced updated master
to match the most recent upstream/master
. Now to get origin/master
looking like upstream/master
:
您将本地更改保存到tmp_master
并强制更新master
以匹配最新的upstream/master
. 现在origin/master
看起来像upstream/master
:
git push -f origin master
Now go ahead and cherry-pick
out the commits, or rebase
the changes on top of the current master
. After which you would already have your new devel branch.
现在继续执行cherry-pick
提交,或者rebase
在当前master
. 之后,您将拥有新的开发分支。
What you wanted to do is totally possible, just not in the order you asked. And it seemed others forgot you can fetch
remote changes without actually applying them. Makes life a lot more simple.
你想做的完全有可能,只是不是按照你要求的顺序。似乎其他人忘记了您可以在fetch
不实际应用的情况下远程更改。让生活变得简单很多。
回答by Talljoe
git reset origin/master
git checkout -b new-branch
git cherry-pick <hash>
for each commit- create your pull request.
git reset origin/master
git checkout -b new-branch
git cherry-pick <hash>
对于每次提交- 创建您的拉取请求。
Alternatively you can do:
或者你可以这样做:
git checkout -b new-branch
git rebase -i origin/master
- (pick and choose your commits)
git checkout master
git reset origin/master
git checkout -b new-branch
git rebase -i origin/master
- (挑选并选择您的提交)
git checkout master
git reset origin/master
回答by Dominic Mitchell
According to git pushyou can use git push origin +dev:master
to:
根据git push你可以git push origin +dev:master
用来:
Update the origin repository's master branch with the dev branch, allowing non-fast-forward updates. This can leave unreferenced commits dangling in the origin repository.
使用 dev 分支更新源存储库的 master 分支,允许非快进更新。这可能会使未引用的提交悬空在原始存储库中。
I'm not certain if this works with github. I don't have anything I need to wipe out right now. :)
我不确定这是否适用于 github。我现在没有什么需要清除的。:)
It should allow you to make your localmaster look as you want using git rebase -i
, then push the result up to github.
它应该允许您使用 使您的本地master 看起来像您想要的那样git rebase -i
,然后将结果推送到 github。
Alternatively, you can delete the master branch on github(git push origin :master
) then repopulate it from your local, corrected, master. I have a feeling that github may prevent you from doing this if it's the default branch (as masterprobably is). If that's the case, go into the Admin section for your repository and change the default to another branch temporarily.
或者,您可以删除 github( git push origin :master
)上的 master 分支,然后从您的本地、更正的 master 重新填充它。我有一种感觉,如果 github 是默认分支(可能是master分支),它可能会阻止您执行此操作。如果是这种情况,请进入存储库的 Admin 部分并临时将默认值更改为另一个分支。
回答by Bryan Kyle
You can push an arbitrary change set to an arbitrary ref within a git repository by using the git push
command. In this case, you'll need to identify the hash of the changeset that you want to revert to and set it as the head of the master branch in the remote repository. Assuming that that remote repository is called origin
you can use the following, where XXXX
is the hash of the change you want to revert to:
您可以使用该git push
命令将任意更改集推送到 git 存储库中的任意参考。在这种情况下,您需要确定要恢复到的变更集的哈希值,并将其设置为远程存储库中主分支的头部。假设调用了该远程存储库,origin
您可以使用以下内容,其中XXXX
您要恢复的更改的哈希值是:
git push -f origin XXXX:refs/heads/master
The -f
switch will force the change as by default git will not allow you to push non-fastforward changes to a remote repository as it can lead to serious problems if other repositories have been cloned from yours.
该-f
开关将强制更改,因为默认情况下 git 不允许您将非快进更改推送到远程存储库,因为如果其他存储库已从您的存储库中克隆,则可能会导致严重问题。
回答by Matthew McCullough
If you want to force "master" to look like "remotes/origin/master" you can do a forced pull.
如果你想强制“master”看起来像“remotes/origin/master”,你可以强制拉动。
$ git pull +master:master
From git://github.com/matthewmccullough/hellogitworld
+ 1d22ca0...2a52e96 master -> master (forced update)
回答by Hugo Josefson
@Novelocrat suggested almost exactly the same approach I would do. Definitively create a backup branch from the current location of your master
branch:
@Novelocrat 建议的方法与我几乎完全相同。从分支的当前位置明确地创建一个备份master
分支:
git branch mywork-orig master
In your case, I think origin
is your github fork, and upstream
is where you forked from. For that reason, when you have your local master
checked out you should do:
在你的情况下,我认为origin
是你的 github 分支,upstream
也是你分支的地方。出于这个原因,当您master
检查本地时,您应该执行以下操作:
git reset --hard upstream/master
That will reset it to where upstream
's master
is. Then you must also push it to your fork on github:
这会将其重置为upstream
's所在的master
位置。然后你还必须将它推送到你在 github 上的 fork:
git push origin +master
Then create new branches off of your newly reset master
branch, which should now be the same as upstream/master
:
然后从新重置的master
分支创建新分支,现在应该与upstream/master
:
git checkout -b mywork
Because you have done so many merges on your old master
branch, you can probably not cherry-pick much onto the new feature branches you create. Cherry-pick the commits you can, and then simply (or not so simply ;) re-create the ones you can't easily cherry-pick.
因为您在旧master
分支上进行了如此多的合并,所以您可能无法在您创建的新功能分支上挑选太多。樱桃挑选你可以的提交,然后简单地(或不那么简单;)重新创建你不能轻易挑选的那些。
回答by Taylored Web Sites
I have an approach that is logical and as safe as possible. Assumptions:
我有一种合乎逻辑且尽可能安全的方法。假设:
- You must have the authority to force updates to master on origin.
- It assumes that no one else has pulled any of the nodes about to be deleted.
- You can prevent updates to the master branch on origin while you are fixing the master branch locally.
- 您必须有权强制更新主控源。
- 它假设没有其他人拉取任何即将被删除的节点。
- 在本地修复 master 分支时,您可以阻止对原始 master 分支的更新。
Move/rename the local bad master branch to my-bad-master.
将本地坏主分支移动/重命名为 my-bad-master。
git branch -m master my-bad-master
Update your local master branch to match the origin's master branch.
更新您的本地主分支以匹配源的主分支。
git pull origin master:master
Save the origin's master branch to a branch called old-master
将 origin 的 master 分支保存到一个名为 old-master 的分支
git branch old-master master
For safety's sake, push the old-master branch to origin
为了安全起见,将 old-master 分支推送到 origin
git push origin old-master:old-master
checkout master
Make any changes to the master branch. !!!! Be sure no changes are made on origin to the master branch till you are done !!!!
对主分支进行任何更改。!!!! 确保在完成之前不会对主分支的原点进行任何更改!!!!
When done, force the new master branch up to origin.
完成后,强制新的主分支到原点。
git push -f origin master:master