Linearize git history, preserving all commits

左心房为你撑大大i 提交于 2019-12-10 18:40:06

问题


I would like to take a git history that contains branches with merge commits, and turn it into a single linear history without squashing all the commits from the branch into a single commit.

That is, starting from the following:

* 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

... I would like a single line as follows:

* 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

The reason I want this is because I am working in a feature branch with another developer. When pulling my changes he has repeatedly used git pull (i.e. creating merge commits) rather than rebasing. I want the history of this feature branch to be linear before merging it into master.

NB I am aware the commit IDs will likely be different in the result. I am also aware of all the usual consequences of rewriting history.

Update: Not a duplicate of this question since I want to preserve the individual commits in the branch rather than squash them into one.


回答1:


I guess you want to make a rebase. Be sure to not have any changes in working dir. The global idea I like to do is to work on a new branch to reorganize the history, then make this branch the master one.

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

I hope it helps.



来源:https://stackoverflow.com/questions/49317019/linearize-git-history-preserving-all-commits

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!