Merge, update, and pull Git branches without using checkouts

前端 未结 17 1695
臣服心动
臣服心动 2020-11-22 07:42

I work on a project that has 2 branches, A and B. I typically work on branch A, and merge stuff from branch B. For the merging, I would typically do:

git mer         


        
17条回答
  •  执念已碎
    2020-11-22 08:36

    I wrote a shell function for a similar use case I encounter daily on projects. This is basically a shortcut for keeping local branches up to date with a common branch like develop before opening a PR, etc.

    Posting this even though you don't want to use checkout, in case others don't mind that constraint.

    glmh ("git pull and merge here") will automatically checkout branchB, pull the latest, re-checkout branchA, and merge branchB.

    Doesn't address the need to keep a local copy of branchA, but could easily be modified to do so by adding a step before checking out branchB. Something like...

    git branch ${branchA}-no-branchB ${branchA}
    

    For simple fast-forward merges, this skips to the commit message prompt.

    For non fast-forward merges, this places your branch in the conflict resolution state (you likely need to intervene).

    To setup, add to .bashrc or .zshrc, etc:

    glmh() {
        branchB=$1
        [ $# -eq 0 ] && { branchB="develop" }
        branchA="$(git branch | grep '*' | sed 's/* //g')"
        git checkout ${branchB} && git pull
        git checkout ${branchA} && git merge ${branchB} 
    }
    

    Usage:

    # No argument given, will assume "develop"
    > glmh
    
    # Pass an argument to pull and merge a specific branch
    > glmh your-other-branch
    

    Note: This is not robust enough to hand-off of args beyond branch name to git merge

提交回复
热议问题