简体   繁体   中英

Linearize git history, preserving all commits

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 (ie 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.

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM