整理Git的旧历史 [英] Flatten old history in Git

查看:66
本文介绍了整理Git的旧历史的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个已经运行了一段时间的git项目,现在我想抛弃旧的历史,比如说从现在开始到两年后.抛弃式的意思是用一次相同的提交替换这段时间内的许多提交.

I have a git project that has run for a while and now I want to throw away the old history, say from start to two years back from now. With throw away I mean replace the many commits within this time with one single commit doing the same.

我检查了git rebase -i,但这不会删除包含git中所有提交的其他(完整)历史记录.

I checked git rebase -i but this does not remove the other (full) history containing all commits from git.

以下是图形表示形式(d为变更集):

Here a graphical representation (d being the changesets):

(base) -> d1 -> d2 -> d3 -> (HEAD)

我想要的是:

(base) -> d1,d2 -> d3 -> (HEAD)

这怎么办? 谢谢.

编辑

我了解了

git rebase -i cd1e8c9

,其中cd1e8c9是壁球的起始修订版(基础).然后,我使用fixup将修订内容融合在一起.谢谢.

with cd1e8c9 being the start revision (base) to squash. Then I used fixup to meld the revisions together. Thanks.

推荐答案

如果您不太在乎整个历史,则另一种简单的方法是采用当前分支并基于此创建一个孤立分支.然后将所有文件添加到该分支并进行一次初始提交(这将丢失所有历史记录).然后可以将其推送到远程存储库.

If you do not really care about the whole history, another simple way to do this would be to take the current branch and create an orphan branch based on this. Then add all files to this branch and make one initial commit (which would lose all history). This can then be pushed to the remote repository.

假设您在要展平的分支中.首先检查是否干净:

Assuming you are in the branch that you want to flatten. First check if it is clean:

git status -s

上面的命令不应输出任何内容.

The above command should not output anything.

现在创建一个孤立分支:

Now create a orphan branch:

git checkout --orphan flattened

添加所有文件

git add .

创建单次提交

git commit -m "Initial flattened commit"

检查是否一切都按需,然后推送到远程(例如):

Check if everything is as wanted and push to remote (ex):

git status -s

# (original_branch being the branch with the full history)
git diff original_branch..flattened  

# (assuming your remote is origin and the branch to overide is master) 
# Think twice before doing this!
git push origin +flattened:master 

这篇关于整理Git的旧历史的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆