简体   繁体   English

Git:包含交互式基础的合并工作流的快捷方式

[英]Git: Shortcut for a merge workflow that includes an interactive rebase

When I want to bring the changes from a feature branch into my master branch, I often do the following: 当我想将功能分支中的更改引入主分支时,我经常执行以下操作:

$ git checkout feature-branch
$ git rebase -i master # Clean up the history
$ git checkout master
$ git reset --hard feature-branch

Is there a shortcut for this sequence of commands? 此命令序列是否有快捷方式?

EDIT: Tim Biegeleisen points out that instead of running git reset --hard feature-branch I might simply git merge feature-branch . 编辑: Tim Biegeleisen指出,与其运行git reset --hard feature-branch ,不如简单地git merge feature-branch What I would like to have though is a shortcut like git rebase-merge feature-branch that results in the same master branch as my sequence of commands. 我想拥有的是git rebase-merge feature-branch类的快捷方式,该快捷方式导致了与我的命令序列相同的master分支。

I don't know what is happening inside the interactive rebase, but I will mention that this is a more typical rebase workflow: 我不知道交互式rebase内部发生了什么,但是我会提到这是一个更典型的rebase工作流程:

git checkout feature-branch
git rebase master
git checkout master
git merge feature-branch

The basic idea being that we rewrite the feature branch by bringing in the latest commits from master, then reapplying the unique commits from the feature branch. 基本思想是,我们通过引入master的最新提交来重写功能分支,然后重新应用来自功能分支的唯一提交。 Then, we can just fast forward the master branch with those new commits from the feature branch. 然后,我们可以使用来自功能分支的那些新提交来快速转发master分支。

Note that this isn't shorter than what you currently have, but I believe this is the more typical workflow used with rebasing. 请注意,这并不比您当前拥有的短,但是我相信这是用于重新定基的更典型的工作流程。 Depending on what you do in your interactive rebase, the above workflow I suggested might no longer work. 根据您在交互式基准库中所做的工作,我建议的上述工作流程可能不再起作用。

Just write yourself a little script or alias that does the rebase and, if (and only if!) it succeeded, does a fast-forward operation on master (given that this is the result you want). 只需为自己编写一个小的脚本或别名即可进行变基,如果成功(且仅当成功!),它会对master进行快进操作(假设这是您想要的结果)。

Remember, any rebase can go wrong, either by being aborted by the user or due to a merge failure during one of the cherry-pick steps. 请记住,任何重新设置都可能出错,这可能是由于用户中止操作,也可能是由于其中一种樱桃选择步骤导致合并失败。 Fortunately git rebase itself is a well-behaved Unix / Linux / POSIX tool: it exits zero on success and nonzero on failure, so that you can tell whether it succeeded or failed. 幸运的是, git rebase本身是一种行为良好的Unix / Linux / POSIX工具:成功时退出零,失败时退出非零,这样您就可以知道它是成功还是失败。 Other Git commands are similar. 其他Git命令与此类似。 Hence this is achievable as small bash / sh shell function: 因此,这可以通过小的bash / sh shell函数来实现:

rebase-merge() {
    case $# in
    1) ;; # good
    *) echo "usage: rebase-merge <branch>" 1>&2; exit 1;;
    esac
    git checkout "$1" &&
        git rebase -i master &&
        git checkout master &&
        git merge --ff-only "$1"
}

It's possible to smush this into a one-liner expression that goes in your global .gitconfig as a Git alias. 可以将其混淆成一个单行表达式,该表达式作为Git别名出现在全局.gitconfig中。 I leave doing that as an exercise for the reader. 我将其作为练习留给读者。

There are other, fancier ways to do this, but this is literally what you're doing (but simplified to use git mege --ff-only to make sure that Git does a fast-forward merge only, or fails if that's not possible), just turned into a command. 还有其他一些更高级的方法可以做到这一点,但这实际上就是您正在做的事情(但简化为使用git mege --ff-only来确保Git git mege --ff-only进行快速合并,否则将失败。 ),只是变成了命令。 I added the check to make sure that "$#" (the number of arguments) is exactly 1, using the construct I prefer since it lets you provide two arguments if you decide you'd like the target branch (currently hardcoded as master , twice) to be optional: just change the setup from "case ..." to: 我添加了检查以确保“ $#”(参数数)正好为1,使用我喜欢的结构,因为如果您决定要使用目标分支(当前硬编码为master ,则可以提供两个参数)两次)为可选:只需将设置从“ case ...”更改为:

case $# in
1) target=master;;
2) target="$2";;
*) echo "usage ..." ...
esac

and then use "$target" instead of the literal master . 然后使用"$target"代替文字master

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

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