简体   繁体   中英

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 . 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.

I don't know what is happening inside the interactive rebase, but I will mention that this is a more typical rebase workflow:

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. Then, we can just fast forward the master branch with those new commits from the feature branch.

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).

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. Other Git commands are similar. Hence this is achievable as small bash / sh shell function:

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. 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. 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:

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

and then use "$target" instead of the literal master .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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