如何使现有的分支在Git孤儿 [英] How to make existing branch an orphan in git

查看:99
本文介绍了如何使现有的分支在Git孤儿的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有没有办法在git中创建一个现有分支作为孤儿?



git checkout --orphan 似乎只有创建一个新的孤儿?

解决方案

你是对的,你想孤儿院已经有提交历史了吗?如果是这样,这里有一个解决方案。



首先,你需要选择一个提交来启动新分支。在我的例子中,这将是 HEAD〜2 sha1 = df931da



说,我们有一个简单的回购。 git log --oneline --graph --decorate 显示以下内容:

  * 4f14671(HEAD,master)4 
* 1daf6ba 3
* df931da 2
* 410711d 1

现在,行动!
$ b

 #移至点我们希望新的分支开始。 
➜gitphan git:(master)git checkout HEAD〜2

code>➜gitphan git :( master)部分是zsh的提示符,不是命令的一部分。

 #创建一个孤立分支
➜gitorphan git:(df931da)git checkout --orphan orphanbranch
切换到一个新分支'orphanbranch'

#首先在其中提交
➜gitphan git:(orphanbranch)✗git commit -m'首先在孤儿中提交'
[orphanbranch(root-commit)f0d071a]首次提交orphan
2个文件已更改,2个插入(+)
创建模式100644 1
创建模式100644 2

#检查这是否为孤儿分支
➜gitorphan git:(orphanbranch)git checkout HEAD ^
错误:pathspec'HEAD ^'与git已知的任何文件都不匹配。

#现在从前一分支挑选一系列提交
➜gitorphan git:(orphanbranch)git cherry-pick df931da..master
[orphanbranch 7387de1] 3
1文件已更改,1个插入(+)
创建模式100644 3
[orphanbranch 4d8cc9d] 4
1个文件已更改,1个插入(+)
创建模式100644 4

现在分支 orphanbranch 有一个工作快照树在df931da在一个提交,并进一步提交,就像他们在主人。


$ b

 ➜ gitphan git:(orphanbranch)git log --oneline 
4d8cc9d 4
7387de1 3
f0d071a首次提交orphan


Is there a way to make an existing branch an orphan in git?

git checkout --orphan seems to only create a new orphan?

解决方案

Do I understand you right, that you want the orphaned branch to already have a history of commits? If so, here's a solution.

First you need to pick a commit to start the new branch at. In my example this will be HEAD~2, sha1=df931da.

Say, we've got a simple repo. git log --oneline --graph --decorate shows the following:

* 4f14671 (HEAD, master) 4
* 1daf6ba 3
* df931da 2
* 410711d 1

Now, action!

# Move to the point where we want new branch to start.
➜  gitorphan git:(master) git checkout HEAD~2

Here and further the ➜ gitorphan git:(master) part is zsh's prompt and not a part of the command.

# make an orphan branch
➜  gitorphan git:(df931da) git checkout --orphan orphanbranch
Switched to a new branch 'orphanbranch'

# first commit in it
➜  gitorphan git:(orphanbranch) ✗ git commit -m'first commit in orphan'
[orphanbranch (root-commit) f0d071a] first commit in orphan
 2 files changed, 2 insertions(+)
 create mode 100644 1
 create mode 100644 2

# Check that this is realy an orphan branch
➜  gitorphan git:(orphanbranch) git checkout HEAD^
error: pathspec 'HEAD^' did not match any file(s) known to git.

# Now cherry-pick from previous branch a range of commits
➜  gitorphan git:(orphanbranch) git cherry-pick df931da..master
[orphanbranch 7387de1] 3
 1 file changed, 1 insertion(+)
 create mode 100644 3
[orphanbranch 4d8cc9d] 4
 1 file changed, 1 insertion(+)
 create mode 100644 4

Now the branch orphanbranch has a snapshot of working tree at df931da in a single commit and further commits just as they were in the master.

➜  gitorphan git:(orphanbranch) git log --oneline
4d8cc9d 4
7387de1 3
f0d071a first commit in orphan

这篇关于如何使现有的分支在Git孤儿的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆