Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Git - Automatically fast forward all tracking branches on pull

Tags:

git

branch

pull

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.

like image 937
Chetan Avatar asked Jan 02 '11 09:01

Chetan


People also ask

How do you git pull all branches at once?

The git fetch –all command retrieves metadata on each change made to all the branches in a repository. The git pull –all command downloads all of the changes made across all branches to your local machine.

Does git pull fast forward?

With git pull --ff-only , Git will update your branch only if it can be “fast-forwarded” without creating new commits. If this can't be done (if local and remote have diverged), git pull --ff-only simply aborts with an error message: $ git pull --ff-only upstream master # ...

Does git pull affect all branches?

It will only change your current local branch in the merge part, but the fetch part will update all of the other remote branches if they had changes since the last fetch.

Does git fetch update all branches?

git fetch. On its own, git fetch updates all the remote tracking branches in local repository. No changes are actually reflected on any of the local working branches.


2 Answers

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; 
like image 183
qwertzguy Avatar answered Sep 20 '22 13:09

qwertzguy


But wait:

  • git won't merge (the second step of git pull after the fetch part) files unless the branch is checked out first. See "Can “git pull --all” update all my local branches?"
  • git pull on master will merge files on master, meaning the next push will be a fast-forward one. A non fast-forward can only occur if a push to the remote master from another repo has been done prior to your push.

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).
like image 44
VonC Avatar answered Sep 17 '22 13:09

VonC