我之前曾错误地创建了一个分支,结果有一个提交留在了另一个分支的起点:
* 03431cb (HEAD -> bar) a2
| * d332e4d (foo) b2
| * 9b29ae3 b1
| * 4656a98 a1
|/
* 6ebca20 (master) root
如何将代码库中的
a1
从 foo
移到 bar
,以便 bar
的历史记录是 root -> a1 -> a2
,并且 a1
不在 foo
中?是否可以使用一个 git 提交完成此操作?由于未推送,因此无需担心破坏其他人的本地代码库。
我最初考虑了对
a1
进行 cherry-pick,然后在 a2
和 a1
之间更正顺序。但问题在于,在我的实际情况下,这两个提交会产生冲突,并且在执行 cherry-pick 和调整它们的顺序时都需要解决冲突。
Bash 中的最小可复现示例:
#!/bin/bash
set -e
rm -rf .git
git init -b master
echo content > my-file
git add my-file
git commit -m root
git checkout -B foo
echo asd >> my-file
git add my-file
git commit -m a1
echo qwe >> my-file
git add my-file
git commit -m b1
echo zxc >> my-file
git add my-file
git commit -m b2
git checkout master
git checkout -B bar
echo jkl >> my-file
git add my-file
git commit -m a2