有没有一种方法可以非交互地压缩大量提交?

发布于 2024-12-02 16:23:23 字数 81 浏览 3 评论 0原文

我正在尝试压缩一系列提交 - HEAD 到 HEAD~3。有没有快速的方法来做到这一点,或者我需要使用 rebase --interactive ?

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(8

明月松间行 2024-12-09 16:23:24

确保你的工作树是干净的,然后

git reset --soft HEAD~3
git commit -m 'new commit message'

Make sure your working tree is clean, then

git reset --soft HEAD~3
git commit -m 'new commit message'
貪欢 2024-12-09 16:23:24

我个人喜欢 wilhelmtell 解决方案:

git reset --soft HEAD~3
git commit -m 'new commit message'

但是,我创建了一个别名并进行了一些错误检查,以便您可以执行此操作:

g.squash 3 'my commit message'

我建议设置建立实际运行脚本的别名,以便更容易(a)编写脚本代码和(b)通过错误检查进行更复杂的工作。下面是一个执行压缩工作的脚本。我将其放在主路径的脚本文件夹中。

挤压脚本 (squash.sh)

#!/bin/bash
#

#get number of commits to squash
squashCount=$1

#get the commit message
shift
commitMsg=$@

#regular expression to verify that squash number is an integer
regex='^[0-9]+

然后,要将 squash.sh 脚本连接到别名,我将以下内容添加到我的 .zprofile 中:

export PATH="$PATH:$HOME/scripts" # Add scripts folder to PATH
...
alias g.squash='function _gSquash(){ sh squash.sh $1 $2; };_gSquash'
...

注意:您可以将您的别名任何你想要的。我有很多 git 快捷方式 g.

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

然后,要将 squash.sh 脚本连接到别名,我将以下内容添加到我的 .zprofile 中:

注意:您可以将您的别名任何你想要的。我有很多 git 快捷方式 g.

I personally like wilhelmtell's solution:

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:

g.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 squashing. I put that in a scripts folder in my HOME path.

Script for squashing (squash.sh)

#!/bin/bash
#

#get number of commits to squash
squashCount=$1

#get the commit message
shift
commitMsg=$@

#regular expression to verify that squash number is an integer
regex='^[0-9]+

Then to hook up that squash.sh script to an alias, I add the following to my .zprofile:

export PATH="$PATH:$HOME/scripts" # Add scripts folder to PATH
...
alias g.squash='function _gSquash(){ sh squash.sh $1 $2; };_gSquash'
...

Note: You can make your alias anything you want. I have my a lot of my git shortcuts as g.<myCommand>

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.sh script to an alias, I add the following to my .zprofile:

Note: You can make your alias anything you want. I have my a lot of my git shortcuts as g.<myCommand>

静谧 2024-12-09 16:23:24

要添加 wilhelmtell 的答案,我发现软重置到 HEAD~2 很方便,然后修改 HEAD~3 的提交:

git reset --soft HEAD~2
git commit --all --amend --no-edit    

这会将所有提交合并到 HEAD~3 提交并使用其提交消息。确保从干净的工作树开始。

To add to the answer by wilhelmtell I find it convenient to soft reset to HEAD~2 and then amending the commit of 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.

Oo萌小芽oO 2024-12-09 16:23:24

我用过:

EDITOR="sed -i '2,/^$/s/^pick\b/s/'" git rebase -i <ref>

效果很好。只是不要尝试拥有以“pick”开头的行的提交日志:)

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" :)

总攻大人 2024-12-09 16:23:24

使用以下命令压缩最后一次提交中的最后 4 次提交:

git squash 4

使用别名:

squash = !"f() { NL=$1; 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 $1
sqpsf = !git squash $1 && git psf 

来自 https://github.com/brauliobo/gitconfig/blob/master/configs/.gitconfig

Use the following command to squash the last 4 commits within the last commit:

git squash 4

With the alias:

squash = !"f() { NL=$1; 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 $1
sqpsf = !git squash $1 && git psf 

From https://github.com/brauliobo/gitconfig/blob/master/configs/.gitconfig

沉鱼一梦 2024-12-09 16:23:24

这是一个用来压缩最后 2 次提交的一行代码。在此示例中,将保留倒数第二次提交的消息。您可以根据需要更改该消息。

git commit -am "$(git log -1 --skip=1 --pretty=%B | xargs && git reset --soft HEAD~2)"

如果您为此命令创建别名并改用该别名,则该命令将非常有用。

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.

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.

乖乖 2024-12-09 16:23:24

自从分支从 master 分叉以来,要压缩所有内容:

git reset --soft $(git merge-base --fork-point master) \
  && git commit --verbose --reedit-message=HEAD --reset-author

To squash everything since the branch was forked from master:

git reset --soft $(git merge-base --fork-point master) \
  && git commit --verbose --reedit-message=HEAD --reset-author
溺孤伤于心 2024-12-09 16:23:24

你可以非常接近

git rebase --onto HEAD~4 HEAD~ master

这假设你是一个具有线性历史的主人。它不完全是一个挤压,因为它丢弃了中间提交。您需要修改新的 HEAD 来修改提交消息。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文