简体   繁体   English

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

[英]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. 我想采用包含带有合并提交的分支的git历史记录,并将其转换为单个线性历史记录, 而无需将所有提交从分支压缩到单个提交中。

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. 在拉动我的更改时,他反复使用git pull (即创建合并提交)而不是重新设置。 I want the history of this feature branch to be linear before merging it into master. 我希望此功能分支的历史记录在将其合并到master之前是线性的。

NB I am aware the commit IDs will likely be different in the result. 注意我知道提交ID可能会在结果中有所不同。 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. 我希望它有所帮助。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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