如何将多个提交从一开始压缩成一个?

huangapple go评论55阅读模式
英文:

How to squash multiple commits from the beginning into one?

问题

我有一个用于个人笔记的存储库。我在多台机器和多个操作系统上使用相同的存储库。同步是通过git完成的。每台机器都有一个每5分钟运行一次的cron作业,既推送又拉取(相同的)存储库,以保持所有机器的一致性。

我随着时间的推移积累了成千上万次的提交,我不真的需要一个月前的提交(我有一年多的更新记录)。所以我需要将前1000次提交合并成一个,而不必改变整个设置,重新创建整个存储库和重新设置所有东西!

我该怎么做来将前1000次提交合并成一个,从而减小存储库的大小?

英文:

I have a repo which I use for personal notes. I use the same repo across multiple machines and multiple OSes. The sync happens using git. Each machine has a 5-minute cron job that both pushes and pulls from the (same) repo - that keeps it consistent across all the machines.

I have accumulated thousands of commits over time and I don't really need commits from over a month ago (I have more than a year worth of updates). So I need to squash say first 1000 commits into one without having to change my entire setup by recreating the entire repository and setting up everything again!

What can I do to squash the first 1000 commits into one thus making the repo size smaller?

答案1

得分: 1

树历史示例:顺序从最旧的提交到最新的提交。

我们只能将最新的提交压缩成旧的提交。这意味着我们可以将1压缩成2,或者将1、2压缩成3,或者将2、3压缩成4,依此类推。

当然,在压缩后,提交哈希 ID 也会被压缩,与上面的示例对应的是2或3或4,依此类推;也会发生变化。

		  6    5    4    3     2    1

如果你想要压缩(最新的4个提交)- 4 3 2 1,可以这样做:

首先:

git rebase -i HEAD~4(将1、2、3合并到4)

其次:

弹出窗口(vim编辑器),你必须将(3、2、1)中的pick替换为s或squash

最后:

wq!

如果你想要将这些更改推送到远程仓库,必须指定-f。

英文:

Exmaple a tree history: the order is from oldest to newest commit.

We are only able to squash commits newest into a old commit. it means we can squash 1 into 2, or 1, 2 into 3 or 2, 3 in to 4, so forth.

Of course, after squash, commit hash id was squashed into, corresponding to examples above are 2 or 3 or 4, so forth; also will be changed.

(master): m1<--m2<--m3<--f1<--f2<--f3(HEAD)
		  6    5    4    3     2    1

you want to squash (4 commits latest) - 4 3 2 1, you can do this

first:

git rebase -i HEAD~4 (1, 2, 3 merge into 4)

second:

popup appear (vim editor), you have to replace pick with s or squash in (3, 2, 1)

finally:

wq!

if you want to push those changes to remote, you must specify -f

答案2

得分: 1

我提供了一个脚本,可以轻松完成这个操作,包括合并:

https://github.com/eantoranz/git-replay

所以...从具有您想要的树作为第一个提交的提交开始,检出一个新的分支。假设这个提交是 X

git checkout --orphan new-branch X
git add .
git commit -m "first commit"

现在,使用脚本

./git-replay HEAD X original-branch

最后,它将打印出一个具有与您想要的相似历史的提交的ID。检查它,如果您喜欢,使用该提交来设置您想要的分支。

英文:

I have provided a script to do this without much hassle, including merges:

https://github.com/eantoranz/git-replay

So.... Check-out a new branch starting from the commit that has the tree that you want as the first commit. Suppose this commit is X

git checkout --orphan new-branch X
git add .
git commit -m "first commit"

Now, use the script

./git-replay HEAD X original-branch

That Will print, in the end, the id of a commit that has a history just like the one you want. Check it and, if you like it, use that commit to set the branch you want there.

huangapple
  • 本文由 发表于 2023年5月7日 14:23:05
  • 转载请务必保留本文链接:https://go.coder-hub.com/76192475.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定