git 将许多提交压缩为更少但更大的提交
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/6218184/
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
Compressing many commits into fewer, but larger, commits
提问by James
I git-commit everything I do every hour. This is nice but I end up with way too many commits.
我 git-commit 我每小时所做的一切。这很好,但我最终得到了太多的提交。
I'd like to be able to purge this so that instead of :
我希望能够清除它,而不是:
1 hour ago
2 hours ago
.
23 hours ago
24 hours ago
I just have something like:
我只是有类似的东西:
1 hour ago
2 hours ago
1 day ago
7 days ago
etc.
等等。
Currently each hour I do:
目前我每小时都做:
git-add .
git-commit -a
How can I remove certain commits? I don't want to undo any changes. I just don't care to have quite so many points to revert to. I'd like to have a lot of commits for the past few hours but then only a few after that (the past day, week, month, etc. or other major points that I keep on purpose).
如何删除某些提交?我不想撤消任何更改。我只是不在乎有这么多点要回复。我希望在过去的几个小时内有很多提交,但之后只有几个(过去的一天、一周、一个月等或我故意保留的其他主要点)。
回答by Abizern
Have a look at the git rebase -i
command. This lets you 'squash' commits into larger ones, which seems like what you want to do.
看看git rebase -i
命令。这使您可以将提交“压缩”为更大的提交,这似乎是您想要做的。
回答by 0xCAFED00D
Using git rebase -i
with the squash
is an simple way of doing this. However this requires user-interaction.
使用git rebase -i
与squash
是这样做的一个简单方法。然而,这需要用户交互。
If you're doing this all the time, you may want to think about writing either a git commit hook for yourself (see Customizing Git - Git Hooks), or just a normal script that you can choose to run when you want. If I had more time I would write it out for you. It seems worth the invested time to automate this more for yourself.
如果您一直这样做,您可能需要考虑为自己编写一个 git commit hook(请参阅自定义 Git - Git Hooks),或者只是一个您可以选择在需要时运行的普通脚本。如果我有更多的时间,我会写出来给你。似乎值得花时间为自己更多地自动化。
To do this without user interaction, you'd need to avoid git rebase -i
. In that case, something like git reset --soft
might be what you're looking for (see Git Resetfor more info). If you ran this as a pre-commit hook, you could check to see if the last commit was yesterday (see below for info on listing the commits by date). If the last commit was yesterday, this would tell you its time to collapse the commits into a single commit. Once you've git reset --soft
to the first commit performed yesterday, you could re-commit all of those changes. Remember that you'd have to specify the original authored date using the --date
option of git commit
, so that the collapsed commit keeps its old date.
要在没有用户交互的情况下执行此操作,您需要避免git rebase -i
. 在这种情况下,git reset --soft
您可能正在寻找类似的东西(有关更多信息,请参阅Git 重置)。如果您将它作为预提交钩子运行,您可以检查最后一次提交是否是昨天(有关按日期列出提交的信息,请参见下文)。如果最后一次提交是在昨天,这会告诉您是时候将提交折叠为单个提交了。一旦您git reset --soft
完成了昨天执行的第一次提交,您就可以重新提交所有这些更改。请记住,您必须使用--date
选项指定原始创作日期git commit
,以便折叠的提交保留其旧日期。
From https://git-scm.com/docs/pretty-formatsyou can create a list of the your most recent commits for parsing, based on the date the commit was originally authored. For example, the following shows how you would create a list of the last 4 commits. I chose 4 just as an example, you may want to choose a number that ensures you see all of the previous days commits, depending on your repo, you may want to list all commits and reorganize your entire history.
从https://git-scm.com/docs/pretty-formats,您可以根据提交的最初创作日期创建最近提交的列表以进行解析。例如,下面显示了如何创建最近 4 次提交的列表。我选择 4 作为示例,您可能希望选择一个数字以确保您看到所有前几天的提交,根据您的存储库,您可能希望列出所有提交并重新组织整个历史记录。
git log --format="%H %ar" | head -n 4
The output looks like this:
输出如下所示:
6c3c5d3970c9f7de9e762f7e8300eaa37db11363 19 hours ago
2e1a8020f59be0cf43500a86033e2acde0707961 2 days ago
9bc3479a257e3f7f9dc9cbb88f3723706369b160 2 days ago
b26353b764cdd3bce0e7f0dec90ea84127c4b292 2 days ago
I'm not sure if it would be easier to parse the list if its listed with relative dates or a different date format.
我不确定如果列表列出了相对日期或不同的日期格式,解析列表是否会更容易。
Also, I realized that if you write this as a pre-commit hook, you would have to git stash
your current working directory temporarily, manage your history, they restore what you stashed.
另外,我意识到如果你把它写成一个预先提交的钩子,你将不得不git stash
临时到你当前的工作目录,管理你的历史,他们恢复你藏匿的东西。