简体   繁体   English

Git 壁球特性分支提交的别名

[英]Git alias to squash feature-branch commits

I'm frequently squashing all the commits on a feature branch before committing to master.在提交到 master 之前,我经常压缩功能分支上的所有提交。 Here's what I do currently:这是我目前所做的:

git checkout master
git merge --squash {branch}
git commit -m "{feature_summary}"

Is there an git alias I can use to do this?我可以使用 git 别名来执行此操作吗? The aliases I'm familiar with (eg stashes = stash list or uncommit = reset --soft HEAD~1 ) don't store variables, and in this case the second command needs to know which branch we came from at the beginning of the commands, so I imagine has to store this information.我熟悉的别名(例如stashes = stash listuncommit = reset --soft HEAD~1 )不存储变量,在这种情况下,第二个命令需要知道我们在开始时来自哪个分支命令,所以我想必须存储这些信息。

Ideally I'd like a git alias such as git squash which does all those commands.理想情况下,我想要一个 git 别名,例如git squash ,它可以执行所有这些命令。 I'm fine with either expecting a string as the commit message, or opening an editor to enter the message.我可以期待一个字符串作为提交消息,或者打开一个编辑器来输入消息。

You can use the @{-1} construct which means "last checked out branch", and pass a parameter to an ad-hoc bash function:您可以使用表示“最后签出分支”的@{-1}构造,并将参数传递给临时 bash function:

# ms for merge-squash but call it as you prefer
git config --global alias.ms '!f() { git checkout master && git merge --squash @{-1} && git commit -m "$1"; }; f'

then an example use could be, from your feature branch:那么一个示例使用可能来自您的功能分支:

git ms "My commit message"

You can create an alias yourself :您可以自己创建别名

[alias]
    sf = !git checkout master && git merge --squash "$1" && git commit -m "$2"

You can run it with你可以运行它

git sf <branch> "<message>"

If you have in your PATH a program called git-command , git will use that program when you call git command [args] .如果您的PATH中有一个名为git-command的程序, git将在您调用git command [args]时使用该程序。

So you can add the following script to your PATH, name it git-squash and make it executable.因此,您可以将以下脚本添加到您的 PATH 中,将其命名为git-squash并使其可执行。

#!/bin/bash
branch="${1}"
shift
git checkout master \
&& git merge --squash "${branch}" \
&& git commit "${@}"

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM