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

标签 git git-rebase

我想获取包含具有 merge 提交的分支的 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(即创建 merge 提交)而不是 rebase 。我希望这个功能分支的历史在 merge 到 master 之前是线性的。

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

更新:不是 this question 的副本因为我想保留分支中的单个提交,而不是将它们压缩成一个。

最佳答案

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

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

希望对你有帮助。

关于git - 线性化 git 历史,保留所有提交,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49317019/

相关文章:

java - Android Studio Git : having to commit everytime i change branch

Git pull 说它是最新的,但它不是

eclipse - EGit:修剪远程仓库上已删除的远程跟踪分支

git - git pull --rebase --preserve-merges

git - 有没有一种简单的方法可以压缩 git 中的许多提交?

git - 如何强制用户在 GIT 中使用小写的电子邮件地址

Git:整个文件到标准输出

git - 将最新提交拆分为多个分支

git rebase 颠倒过来

git rebase 到远程更新