gpt4 book ai didi

git - 如何使现有分支成为git中的孤儿

转载 作者:太空狗 更新时间:2023-10-29 12:49:50 26 4
gpt4 key购买 nike

有没有办法让现有分支在Git中成为孤儿?
git checkout --orphan似乎只创建了一个新的孤儿?

最佳答案

我明白你的意思了吗?你希望孤立的分支已经有提交的历史了?如果是,这里有一个解决方案。
首先,您需要选择一个commit来启动新的分支。在我的示例中,这将是HEAD~2sha1=df931da
我们有一个简单的回购协议。git log --oneline --graph --decorate显示以下内容:

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

现在,行动!
# Move to the point where we want new branch to start.
➜ gitorphan git:(master) git checkout HEAD~2

这里和后面的 ➜ gitorphan git:(master)部分是zsh的提示符,而不是命令的一部分。
# 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

现在,分支 orphanbranch在一次提交中有一个位于df931da的工作树快照,并像在主节点中一样进一步提交。
➜  gitorphan git:(orphanbranch) git log --oneline
4d8cc9d 4
7387de1 3
f0d071a first commit in orphan

关于git - 如何使现有分支成为git中的孤儿,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30283797/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com