我正在为我的Git工作流编写一些脚本.
我需要将其他(现有)分支重置为当前分支,而无需结帐.
之前:
CurrentBranch: commit A
OtherBranch: commit B
Run Code Online (Sandbox Code Playgroud)
后:
CurrentBranch: commit A
OtherBranch: commit A
Run Code Online (Sandbox Code Playgroud)
相当于
$ git checkout otherbranch
$ git reset --soft currentbranch
$ git checkout currentbranch
Run Code Online (Sandbox Code Playgroud)
(注意--soft:我不想影响工作树.)
这可能吗?
Col*_*ett 199
设置otherbranch为指向与currentbranch运行相同的提交
git branch -f otherbranch currentbranch
Run Code Online (Sandbox Code Playgroud)
该-f(力)选项告诉git branch 是的,我真的要覆盖任何现有的otherbranch与新的一个参考.
从文档:
-f
--force如果已存在则重置为.没有-f git branch拒绝更改现有分支.
P S*_*ved 77
您描述的工作流程并不等同:执行时,您reset --hard将丢失工作树中的所有更改(您可能想要创建它reset --soft).
你需要的是什么
git update-ref refs/heads/OtherBranch refs/heads/CurrentBranch
Run Code Online (Sandbox Code Playgroud)
Den*_*sov 20
您可以随时使用此命令与您的分支同步
$ git push . CurrentBranch:OtherBranch -f
Run Code Online (Sandbox Code Playgroud)
也没有-f它会替换这组命令
$ git checkout OtherBranch
$ git merge CurrentBranch
$ git checkout CurrentBranch
Run Code Online (Sandbox Code Playgroud)
当您不需要在CurrentBranch中提交所有文件时,它可能很有用,因此您无法切换到另一个分支.