线性化 git 历史记录,保留所有提交



我想获取一个包含合并提交的分支的 git 历史记录,并将其转换为单个线性历史记录,而不会将分支中的所有提交压缩为单个提交。

也就是说,从以下几点开始:

* d667df5 (HEAD -> master) Modify merged file
*   233e181 Merge branch 'featurebranch'
|
| * bca198d (featurebranch) Modify the second file
| * fdc4e08 Add a different file
* | 5e0c25f Modify file yet again
* | 2426135 Modify file again
* | eb56b32 Modify file
|/
* c94a83e Add a file
* bfbfaaa Initial commit

。我想要一行如下:

* d667df5 (HEAD -> master) Modify merged file
* bca198d Modify the second file
* fdc4e08 Add a different file
* 5e0c25f Modify file yet again
* 2426135 Modify file again
* eb56b32 Modify file
* c94a83e Add a file
* bfbfaaa Initial commit

我想要这个的原因是因为我正在与另一个开发人员在功能分支中工作。在拉取我的更改时,他反复使用git pull(即创建合并提交(而不是变基。我希望这个特征分支的历史记录在将其合并到 master 之前是线性的。

注意 我知道提交 ID 在结果中可能会有所不同。我也意识到改写历史的所有常见后果。

更新:不是这个问题的重复,因为我想在分支中保留各个提交,而不是将它们压缩为一个。

我想你想做一个变基。确保工作目录没有任何变化。我喜欢做的全局想法是建立一个新的分支来重组历史,然后使这个分支成为主分支。

git checkout -b workingBranch featurebranch // create a new branch and checkout to featurebranch
git rebase 5e0c25f // This create your linear history, instead of merge. You may have to resolve commit for each commits of featurebranch
git checkout master
git reset --hard workingBranch // You will lose the commit d667df5 (HEAD -> master) Modify merged file. If it is not wanted, cherry-pick or rebase again
git branch -D workingBranch

我希望它有所帮助。

最新更新