[英]git shortcut to rebase from another branch onto the current branch
我想重新设置分支并将其签出到当前分支而不先签出。 这节省了检出旧存储库 state 并重新编译它所涉及的文件的时间。
例如,我有这个:
A -> B -> C (HEAD -> main)
\
D -> E (old)
我想要以下。 基本上检查old
和 rebase 在main
之上。
A -> B -> C (main) -> D -> E (HEAD -> old)
我通常会运行:
git checkout old
git rebase main
问题是我几个月前在old
分支上工作。 检查它会触及许多文件。 存储库很大,如果这样做,我将需要花费数小时重新编译。 我真正想做的是以下几点:
# Don't update 'main'
git checkout --detach
# Automatically cherry-picks commits after the common ancestor,
# just like a 'rebase' from that branch would do.
git cherry-pick main..old
# Update the branch ref
git branch -f old HEAD
# Check out the "rebased" branch
git checkout old
有没有更短的方法来做到这一点?
这只是将问题中的命令组合成一个宏:
git config --global alias.checkoutrebase '!git checkout --detach && git cherry-pick HEAD..$1 && git branch -f $1 HEAD && git checkout $1 && :'
Bash 完成(添加到 ~/.bashrc): __git_complete checkoutrebase _git_checkout
用法:
# Rebase another_branch onto the current HEAD and check it out
git checkoutrebase another_branch
如果another_branch
实际上不是分支而是另一种引用,它将中断。
测试:
# Setup
git init --initial-branch=main
touch one two
git add one two
git commit -am A
echo D > two ; git commit -am D
echo E > two ; git commit -am E
git branch old
git reset --hard HEAD~2
echo B > one ; git commit -am B
echo C > one ; git commit -am C
# State before
git log --all --graph --oneline
* 203b3f2 (HEAD -> main) C
* ee8761e B
| * aaf8ea8 (old) E
| * 0e592b1 D
|/
* fede4c2 A
# Run the above alias
git checkoutrebase old
HEAD is now at 203b3f2 C
[detached HEAD 11bca25] D
Date: Mon Jan 10 16:47:13 2022 -0800
1 file changed, 1 insertion(+)
[detached HEAD 7e22429] E
Date: Mon Jan 10 16:47:17 2022 -0800
1 file changed, 1 insertion(+), 1 deletion(-)
Switched to branch 'old'
# State after
git log --all --graph --oneline
* 7e22429 (HEAD -> old) E
* 11bca25 D
* 203b3f2 (main) C
* ee8761e B
* fede4c2 A
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.