Can “git pull --all” update all my local branches?

前端 未结 25 2161
失恋的感觉
失恋的感觉 2020-11-22 16:33

I often have at least 3 remote branches: master, staging and production. I have 3 local branches that track those remote branches.

Updating all my local branches is

25条回答
  •  无人及你
    2020-11-22 17:28

    The following one-liner fast-forwards all branches that have an upstream branch if possible, and prints an error otherwise:

    git branch \
      --format "%(if)%(upstream:short)%(then)git push . %(upstream:short):%(refname:short)%(end)" |
      sh
    

    How does it work?

    It uses a custom format with the git branch command. For each branch that has an upstream branch, it prints a line with the following pattern:

    git push . :
    

    This can be piped directly into sh (assuming that the branch names are well-formed). Omit the | sh to see what it's doing.

    Caveats

    The one-liner will not contact your remotes. Issue a git fetch or git fetch --all before running it.

    The currently checked-out branch will not be updated with a message like

    ! [remote rejected] origin/master -> master (branch is currently checked out)
    

    For this, you can resort to regular git pull --ff-only .

    Alias

    Add the following to your .gitconfig so that git fft performs this command:

    [alias]
            fft = !sh -c 'git branch --format \"%(if)%(upstream:short)%(then)git push . %(upstream:short):%(refname:short)%(end)\" | sh' -
    

    See also my .gitconfig. The alias is a shorthand to "fast-forward tracking (branches)".

提交回复
热议问题