在 Git 中压平旧的历史

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/4506758/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-19 04:51:33  来源:igfitidea点击:

Flatten old history in Git

gitgit-rebase

提问by schoetbi

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 项目,现在我想扔掉旧的历史,比如从现在开始到两年前。扔掉我的意思是用一个单一的提交来替换这段时间内的许多提交。

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

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

Here a graphical representation (d being the changesets):

这是一个图形表示(d 是变更集):

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

What I want is:

我想要的是:

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

How could this be done? Thanks.

这怎么可能?谢谢。

EDIT

编辑

I got it working with

我得到它的工作

git rebase -i cd1e8c9

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

cd1e8c9 是压缩的开始版本(基础)。然后我使用 fixup 将修订融合在一起。谢谢。

回答by murraybo

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

Add all files

添加所有文件

git add .

Create single commit

创建单次提交

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 

回答by Noufal Ibrahim

I'm not very comfortable with doing rebasing so try this on a separate clone to see if it works before doing it on your real work space.

我对做 rebase 不是很舒服,所以在单独的克隆上试试这个,看看它是否有效,然后再在你的真实工作空间上做。

Here are my commits

这是我的承诺

noufal@sanitarium% git log --pretty=oneline
967122e7d4e687c0707d25c62cb0d3b6a45d337f Added h
3b82cae737d5fb3317bc7a686a2fdf0fdd9d1c7e Added g
94d89e0455b12e1e4843e64a8f62f3ad6cdf42f3 Added f
a30321b7a367d9b7da6369783650acadbb773cfb Added e
04453f1c90ffde0574c9c8a76f154d741d7d83f4 Added d
ec723a3266e56cc39967bf117154465575905e31 Added c
f415d1f58e2f7bd4beea80ab9acd8309bf5b64e7 Added b
7f1f8d1f903168aa929818a0eb81e0ec7743fb85 Added a
21790602bd6c0a009899ea33e64fec63559c0a76 Added it

I'm rebasing 04453f1c90ffde0574c9c8a76f154d741d7d83f4 (Added d)onto 21790602bd6c0a009899ea33e64fec63559c0a76(the first commit) and squashing them all and I do it with this command

我重订04453f1c90ffde0574c9c8a76f154d741d7d83f4 (Added d)21790602bd6c0a009899ea33e64fec63559c0a76(第一次提交)和挤压所有这些,我用这个命令做

git rebase 04453f1c90ffde0574c9c8a76f154d741d7d83f4 --onto 21790602bd6c0a009899ea33e64fec63559c0a76

After I finish this, the logs look like this

完成此操作后,日志如下所示

noufal@sanitarium% git log --pretty=oneline
c76290666c8b868d36290d8f5276b879bb78d05d Added h
7001ce74f0837b35c0b82abbb82ad8f40801449c Added g
051062042e56759d83258c5e90a9876aa6f52764 Added f
fb1a62b0c0faefa0110ef7b8eee01a13f2f62034 Added e
21790602bd6c0a009899ea33e64fec63559c0a76 Added it

Is this what you're looking for?

这是你要找的吗?

回答by Alan Haggai Alavi

squashthe respective commits into one using git rebase --interactive.

squash各自使用git rebase --interactive.

回答by willnode

This may seems unconventional, but if you don't care about the history, and the repository have only single masterbranch and have not been publishedto Github.com or other sites, you can:

这可能看起来非常规,但如果你不关心历史,并且存储库只有一个master分支并且没有发布到 Github.com 或其他站点,你可以:

  1. Delete hidden .gitfolder in the root of repo
  2. git init
  3. Commit
  1. 删除.gitrepo根目录下的隐藏文件夹
  2. git init
  3. 犯罪

Remember the first step will clear all git data including branch and commit, so please be careful.

记住第一步会清除所有git数据,包括分支和提交,所以请小心。