如何在 git 中的几个独立分支中重新安排提交?
How to rearange commits in few independed branches in git?
我需要重新排列 "linear structure"
temp -C1-C2-C3-C4-C5-C6-C7-C8-C9-C10
/
master -C0
到"fork-like structure".
branch-1 -C1-C2-C3-C4-C5
/
master -C0
\
branch-2 -C6-C7-C8-C9-C10
示例:让我们从一个分支开始(我们将其命名为 temp),派生自 master,然后让我们提交一些提交- C1 .. C10。
取而代之的是,我希望有两个基于 master 的分支(每个功能一个)。我可以通过引用 C1 之前的提交来创建这两个分支。那么,如何重新洗牌(合并?)所有提交,以便在 branch-1 中获得从 C1 到 C5 的提交(源自 master)并从 C6 分支到 C10 in branch-2 也派生自 master?作为 branch-2 日志的结果,我希望有 C0、C6..C10(其中 C0 是 master 分支中的最后一次提交)。
可能可以通过重命名 temp 并执行
来检索 branch-1
git reset --hard HEAD~5 #or 4 (?)
但是对于 branch-2 如何删除 C1..C5 并保留其余部分?
编辑:这是可用于重现案例的测试脚本:
#!/bin/bash
## file names
file1='file1'
file2='file2'
## clean-up for fresh start
if [ -d .git ]
then
rm -rf .git
fi
## create files
echo start1 > $file1
echo start2 > $file2
## start the repository creation
git init
git add $file1 $file2
git commit -am 'C0'
## linear branch
git checkout -b temp
## implement features 1 & 2 in files 1 & 2
for i in {1..10}
do
if [ $i -le 5 ]
then
file=$file1
else
file=$file2
fi
msg='line '$i
echo $msg >> $file
git commit -m "C$i" $file
done
git branch branch-1 C5
git branch -m temp branch-2
git rebase --onto C0 branch-1 branch-2
我需要重新排列 "linear structure"
temp -C1-C2-C3-C4-C5-C6-C7-C8-C9-C10
/
master -C0
到"fork-like structure".
branch-1 -C1-C2-C3-C4-C5
/
master -C0
\
branch-2 -C6-C7-C8-C9-C10
示例:让我们从一个分支开始(我们将其命名为 temp),派生自 master,然后让我们提交一些提交- C1 .. C10。 取而代之的是,我希望有两个基于 master 的分支(每个功能一个)。我可以通过引用 C1 之前的提交来创建这两个分支。那么,如何重新洗牌(合并?)所有提交,以便在 branch-1 中获得从 C1 到 C5 的提交(源自 master)并从 C6 分支到 C10 in branch-2 也派生自 master?作为 branch-2 日志的结果,我希望有 C0、C6..C10(其中 C0 是 master 分支中的最后一次提交)。
可能可以通过重命名 temp 并执行
来检索 branch-1git reset --hard HEAD~5 #or 4 (?)
但是对于 branch-2 如何删除 C1..C5 并保留其余部分?
编辑:这是可用于重现案例的测试脚本:
#!/bin/bash
## file names
file1='file1'
file2='file2'
## clean-up for fresh start
if [ -d .git ]
then
rm -rf .git
fi
## create files
echo start1 > $file1
echo start2 > $file2
## start the repository creation
git init
git add $file1 $file2
git commit -am 'C0'
## linear branch
git checkout -b temp
## implement features 1 & 2 in files 1 & 2
for i in {1..10}
do
if [ $i -le 5 ]
then
file=$file1
else
file=$file2
fi
msg='line '$i
echo $msg >> $file
git commit -m "C$i" $file
done
git branch branch-1 C5
git branch -m temp branch-2
git rebase --onto C0 branch-1 branch-2