Git - Automatically fast forward all tracking branches on pull

后端 未结 6 1742
闹比i
闹比i 2020-12-28 12:32

I\'ve set up tracking branches with the --track option, and when I do a git pull on master, it fetches all branches to origin/br

6条回答
  •  臣服心动
    2020-12-28 13:26

    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.

    Caveat

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

    The alias is a shorthand to "fast-forward tracking (branches)".

提交回复
热议问题