36

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/branchname but doesn't merge with the local tracking branches. This is extra annoying, because if I later do a git push on master, it says that non-fast-forward updates were rejected on the tracking branches, since they weren't fast-forwarded on the initial git pull.

My question is: How do I make it so that git pull with fetch all branches and automatically fast-forward all the tracking branches?

Note: git pull used to fast-forward all my tracking branches with my GitHub repos, but now that I've set up my own repos using Gitolite, this problem is cropping up.

VonC
  • 1,042,979
  • 435
  • 3,649
  • 4,283
Chetan
  • 41,886
  • 27
  • 101
  • 142
  • Can you show the exact commands that you are using? Doing a `git push` on your `master` branch should push anything except your master branch and if you haven't made any changes to master then you shouldn't be trying yo push it. – CB Bailey Jan 02 '11 at 10:04
  • 1
    I'm not exactly sure what your workflow is but my immediate reaction is that you don't need to create local tracking branches if you aren't doing any work on them (i.e. the case where they can be fast forwarded); the remote tracking branches (`origin/*`) are automatically updated and are available for your reference in any case. – CB Bailey Jan 02 '11 at 10:06
  • @CharlesBailey Here's an example: Multiple developers share a set of branches, and do work on all of them at least occasionally, but usually when pulling updates most of the branches can be fast forwarded. – bames53 Mar 01 '12 at 23:27
  • Note: Git 2.0 will help in insuring a pull a ff-only (fast-forward only). See [my edited answer below](http://stackoverflow.com/a/4577950/6309) – VonC Mar 12 '14 at 14:19
  • 1
    Possible duplicate of [Can "git pull --all" update all my local branches?](https://stackoverflow.com/questions/4318161/can-git-pull-all-update-all-my-local-branches) – krlmlr Apr 15 '19 at 18:44
  • https://stackoverflow.com/a/17722977/305118 – Vladimir Reshetnikov Jun 13 '20 at 20:20

6 Answers6

18

Shell script that fast-forwards all branches that have their upstream branch set to the matching origin/ branch without doing any checkouts

  • it doesn't change your current branch at any time, no need to deal with working copy changes and time lost checking out

  • it only does fast-forwards, branches that cannot be fast-forwarded will show an error message and will be skipped

Make sure all your branches' upstream branches are set correctly by running git branch -vv. Set the upstream branch with git branch -u origin/yourbanchname

Copy-paste into a file and chmod 755:

#!/bin/sh

curbranch=$(git rev-parse --abbrev-ref HEAD)

for branch in $(git for-each-ref refs/heads --format="%(refname:short)"); do
        upbranch=$(git config --get branch.$branch.merge | sed 's:refs/heads/::');
        if [ "$branch" = "$upbranch" ]; then
                if [ "$branch" = "$curbranch" ]; then
                        echo Fast forwarding current branch $curbranch
                        git merge --ff-only origin/$upbranch
                else
                        echo Fast forwarding $branch with origin/$upbranch
                        git fetch . origin/$upbranch:$branch
                fi
        fi
done;
qwertzguy
  • 10,735
  • 5
  • 53
  • 59
  • If you use `git fetch --update-head-ok` then you don't need to have a special case for the current branch. – hugomg Jun 10 '20 at 21:56
  • You have to use to add a plus sign at the start like `+origin/foo:foo` to force updates that are not fast forwards. (The man page mentions that this might be different for git < 2.20 though.) – hugomg Jun 11 '20 at 02:27
  • 1
    I think --update-head-ok works the same as the current answer does w.r.t. fast forwards. What it changes is that it allows the command to fetch to happen if you ask it to update the current branch. – hugomg Jun 11 '20 at 02:28
  • Made a one line version to be used as a git alias (not as safe though, doesn't check the upstream name): `ffa = !"for b in $(git for-each-ref refs/heads --format='%(refname:short)') ; do git config --get branch.$b.merge && git fetch . origin/$b:$b --update-head-ok ; done"` – qwertzguy Apr 08 '21 at 06:36
6

If you really want to fast forward all local branches that are tracking remote branches you might want to consider adding this as an alias to your ~/.gitconfig:

[alias]
    pull-all = !"for b in $(git for-each-ref refs/heads --format='%(refname)') ; do git checkout ${b#refs/heads/} ; git pull --ff-only ; done"

You can then run git pull-all, it will iterate through your local branches and run a git pull --ff-only on each.

Jörn Hees
  • 3,017
  • 16
  • 39
  • 5
    The `checkout` may be undesirable because it requires updating the working tree, which may trigger updates in IDEs or anything else watching the files. To avoid that, you could use `update-ref` instead, as seen here: http://stackoverflow.com/questions/5147537/how-do-i-fast-forward-other-tracking-branches-in-git#5148202 – joeytwiddle Jan 04 '14 at 04:04
  • I made a naive guess that the tracking branch is origin/$local_branch: `merge-all = !"for local_branch in $(git for-each-ref refs/heads --format=\"%(refname:short)\") ; do remote_branch=\"origin/$local_branch\"; local_commit=$(git rev-parse --verify \"$local_branch\"); remote_commit=$(git rev-parse --verify \"$remote_branch\"); common_ancestor=$(git merge-base $local_branch $remote_branch); if [ ! \"$common_ancestor\" = \"$local_commit\" ]; then echo \"Could not be fast-forwarded: $local_branch\" >&2; else git update-ref refs/heads/$local_branch $remote_commit $local_commit; fi done"` – joeytwiddle Jan 04 '14 at 04:53
  • In case edit never go through, you can replace the command as below to get back to the branch you were: `pull-all = !"current_branch=$(git rev-parse --abbrev-ref HEAD); for b in $(git for-each-ref refs/heads --format='%(refname)') ; do git checkout ${b#refs/heads/} ; git pull --ff-only ; done; echo $current_branch; git checkout $current_branch"` – lauksas Jun 29 '20 at 15:24
5

But wait:

Note: I suppose you have tracked all your remote branches as in "Track all remote git branches as local branches."


Note: Git 2.0 (Q2 2014) will introduce with commit b814da8 a config push.ff:

pull.ff::

By default, Git does not create an extra merge commit when merging a commit that is a descendant of the current commit. Instead, the tip of the current branch is fast-forwarded.

  • When set to false, this variable tells Git to create an extra merge commit in such a case (equivalent to giving the --no-ff option from the command line).
  • When set to only, only such fast-forward merges are allowed (equivalent to giving the --ff-only option from the command line).
Community
  • 1
  • 1
VonC
  • 1,042,979
  • 435
  • 3,649
  • 4,283
2

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 . <remote-ref>:<branch>

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

krlmlr
  • 22,030
  • 13
  • 107
  • 191
  • Posted the same answer to the alleged duplicate: https://stackoverflow.com/a/55696044/946850. Will delete this answer once the duplicate is identified as such. – krlmlr Apr 15 '19 at 19:28
0

SmartGit, for example, has an option to automatically merge changes from the tracked branch if you switch to a branch. This should do what you want to achieve.

Mot
  • 24,166
  • 22
  • 78
  • 117
  • This particular script also has the undesireable side-effect in that it changes the current branch. Is there any way to modify it to keep the current branch? – Zeke Hansell Mar 03 '14 at 16:02
0

I just wrote a small tool to do so. https://github.com/changyuheng/git-fast-forward-all

Advantages of this tool:

  1. Supports multiple remotes in one repository. (hub sync doesn't support multiple remotes at the moment.)
  2. Supports having different names on the local branch and the corresponding remote tracking branche.
  3. Much faster than other scripts that fetches remote for every single branch.
  4. No error-prone regex parsing/editing.
changyuheng
  • 984
  • 2
  • 11
  • 22