git 有没有办法以非交互方式压缩多个提交?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/7275508/
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
Is there a way to squash a number of commits non-interactively?
提问by Phillip
I'm trying to squash a range of commits - HEAD to HEAD~3. Is there a quick way to do this, or do I need to use rebase --interactive?
我正在尝试压缩一系列提交 - HEAD to HEAD~3。有没有一种快速的方法可以做到这一点,还是我需要使用 rebase --interactive?
回答by wilhelmtell
Make sure your working tree is clean, then
确保你的工作树是干净的,然后
git reset --soft HEAD~3
git commit -m 'new commit message'
回答by n8tr
I personally like wilhelmtell'ssolution:
我个人喜欢wilhelmtell 的解决方案:
git reset --soft HEAD~3
git commit -m 'new commit message'
However, I made an alias with some error checking so that you can do this:
但是,我做了一个带有一些错误检查的别名,以便您可以这样做:
git squash 3 'my commit message'
I recommend setting up aliases that actually run scripts so that it is easier to (a) code up your scripts and (b) do more complex work with error checking. Below is a script that does the work of squash and then below that is a script for setting up your git aliases.
我建议设置实际运行脚本的别名,以便更容易地 (a) 编写脚本和 (b) 执行更复杂的错误检查工作。下面是一个执行壁球工作的脚本,然后是一个用于设置 git 别名的脚本。
Script for squashing (squash.sh)
挤压脚本 (squash.sh)
#!/bin/bash
#
#get number of commits to squash
squashCount=
#get the commit message
shift
commitMsg=$@
#regular expression to verify that squash number is an integer
regex='^[0-9]+$'
echo "---------------------------------"
echo "Will squash $squashCount commits"
echo "Commit message will be '$commitMsg'"
echo "...validating input"
if ! [[ $squashCount =~ $regex ]]
then
echo "Squash count must be an integer."
elif [ -z "$commitMsg" ]
then
echo "Invalid commit message. Make sure string is not empty"
else
echo "...input looks good"
echo "...proceeding to squash"
git reset --soft HEAD~$squashCount
git commit -m "$commitMsg"
echo "...done"
fi
echo
exit 0
Then to hook up that squash.shscript to a git alias, make another script for setting up your git aliases like so (create_aliases.commandor create_aliases.sh):
然后将该squash.sh脚本连接到 git 别名,制作另一个脚本来设置您的 git 别名,如下所示(create_aliases.command或create_aliases.sh):
#!/bin/sh
echo '-----------------------'
echo 'adding git aliases....'
echo '-----------------------'
echo
git config --global alias.squash "!bash -c 'bash <path to scripts directory>/squash.sh $1 $2' -"
#add your other git aliases setup here
#and here
#etc.
echo '------------------------------------'
echo 'here is your global gitconfig file:'
echo '------------------------------------'
more ~/.gitconfig
echo
echo
echo '----------------'
echo 'end of script...'
echo '----------------'
回答by harmonious
To add to the answer by wilhelmtellI find it convenient to soft reset to HEAD~2
and then amending the commit of HEAD~3
:
要添加wilhelmtell的答案,我发现软重置到HEAD~2
然后修改提交很方便HEAD~3
:
git reset --soft HEAD~2
git commit --all --amend --no-edit
This will merge all commits to the HEAD~3
commit and use its commit message. Be sure to start from a clean working tree.
这会将所有提交合并到HEAD~3
提交并使用其提交消息。一定要从一个干净的工作树开始。
回答by Julien Wajsberg
I used:
我用了:
EDITOR="sed -i '2,/^$/s/^pick\b/s/'" git rebase -i <ref>
Worked quite fine. Just don't try to have a commit log with a line that starts with "pick" :)
工作得很好。只是不要尝试使用以“pick”开头的行来提交日志:)
回答by brauliobo
Use the following command to squash the last 4 commits within the last commit:
使用以下命令压缩最后一次提交中的最后 4 次提交:
git squash 4
With the alias:
使用别名:
squash = !"f() { NL=; GIT_EDITOR=\"sed -i '2,$NL s/pick/squash/;/# This is the 2nd commit message:/,$ {d}'\"; git rebase -i HEAD~$NL; }; f"
sq = !git squash
sqpsf = !git squash && git psf
From https://github.com/brauliobo/gitconfig/blob/master/configs/.gitconfig
来自https://github.com/brauliobo/gitconfig/blob/master/configs/.gitconfig
回答by Jasir
Here is a one liner to squash the last 2 commits. In this example, the message of second last commit will be retained. You may change the message as you wish.
这是一个用于压缩最后 2 个提交的单行代码。在本例中,将保留倒数第二次提交的消息。您可以根据需要更改消息。
git commit -am "$(git log -1 --skip=1 --pretty=%B | xargs && git reset --soft HEAD~2)"
This command will be very useful if you create an alias for this command and use the alias instead.
如果您为此命令创建别名并改用别名,则此命令将非常有用。
回答by Andy
To squash everything since the branch was forked from master:
自分支从 master 分叉以来,要压缩所有内容:
git reset --soft $(git merge-base --fork-point master) \
&& git commit --verbose --reedit-message=HEAD --reset-author
回答by Greg Bacon
You can get pretty close with
你可以非常接近
git rebase --onto HEAD~4 HEAD~ master
This assumes you're on master with a linear history. It's not quite a squash because it discards the intermediate commits. You'd need to amend the new HEAD to modify the commit message.
这假设您是具有线性历史记录的大师。它不是一个壁球,因为它丢弃了中间提交。您需要修改新的 HEAD 以修改提交消息。