Black Friday sale: 30% off Pro and Unlimited, until 22 December

Update many Git repositories with git pull --ff-only

Why --ff-only is the calm way to pull, what it refuses, and one loop that brings a whole folder of repositories up to date without ever creating a merge commit.

You come back to a repository after a week. Teammates pushed to main while you were gone, and you want their work before you start. Plain git pull is one command, but it can also open an editor for a merge commit you never asked for, or stop halfway into a conflict.

git pull --ff-only does one thing: it moves your branch forward to match the server, or it stops and changes nothing.

What --ff-only does

A pull is a fetch followed by a merge. With --ff-only the merge is allowed only when your branch has nothing the server doesn’t, so Git can slide it forward along the server’s commits. That is a fast-forward. Nothing is rebased, nothing is merged, and no merge commit appears.

Here is acme/web, cloned to ~/code/web. Fetch first and look at what is waiting:

git fetch --no-prune --no-prune-tags
git status -sb
git log --oneline HEAD..@{u}
## main...origin/main [behind 2]
40e7be1 Fix cart rounding
599c5f4 Add coupon field

HEAD..@{u} lists the commits the upstream has and your branch doesn’t. Now pull:

git pull --ff-only
Updating ba33fa7..40e7be1
Fast-forward
 cart.txt | 2 ++
 1 file changed, 2 insertions(+)
 create mode 100644 cart.txt

git status -sb now shows ## main...origin/main with no count. Your branch is level with the server.

To make this the default for every plain git pull on the machine:

git config --global pull.ff only

What it refuses

Three situations stop a pull. In each one Git changes nothing, which is the point.

Both sides have new commits. This is a diverged branch. In ~/code/docs you added a commit and the server got one too:

## main...origin/main [ahead 1, behind 1]
git pull --ff-only
fatal: Not possible to fast-forward, aborting.

Git won’t choose between rebase and merge for you. See both sides with git log --oneline --left-right main...@{u}, then follow how to fix a diverged branch safely.

< 332c33a Add FAQ
> 27a2a39 Reword setup guide

Your uncommitted edits are in the way. In ~/code/tools you have a half-done change to notes.txt, and the incoming commit changes the same file:

error: Your local changes to the following files would be overwritten by merge:
	notes.txt
Please commit your changes or stash them before you merge.
Aborting

Your edit is untouched. Decide first: commit it, stash it or move it to a worktree, then pull again. An edit to a file the incoming commits don’t touch doesn’t block the pull.

The branch has no upstream. In a repository whose branch tracks nothing, Git doesn’t know what to fast-forward to:

There is no tracking information for the current branch.
Please specify which branch you want to merge with.

Fix that with the no-upstream how-to, or with git branch --set-upstream-to=origin/main.

Every repository in a folder

If your repositories sit side by side in ~/code, one loop fetches each, then fast-forwards the ones that can move and says why it skipped the rest:

for d in ~/code/*/; do
  [ -d "$d.git" ] || continue
  repo=$(basename "$d")
  git -C "$d" fetch --no-prune --no-prune-tags --quiet || { echo "$repo: fetch failed"; continue; }
  git -C "$d" rev-parse --verify --quiet '@{u}' >/dev/null 2>&1 || { echo "$repo: no upstream, skipped"; continue; }
  read -r ahead behind <<EOT
$(git -C "$d" rev-list --left-right --count 'HEAD...@{u}')
EOT
  if [ "$behind" -eq 0 ]; then
    echo "$repo: up to date"
  elif [ "$ahead" -gt 0 ]; then
    echo "$repo: diverged ($ahead ahead, $behind behind), left alone"
  elif git -C "$d" merge --ff-only --quiet '@{u}' 2>/dev/null; then
    echo "$repo: pulled, was $behind behind"
  else
    echo "$repo: refused, look at it by hand"
  fi
done
api: pulled, was 1 behind
docs: diverged (1 ahead, 1 behind), left alone
scratch: no upstream, skipped
tools: refused, look at it by hand
web: pulled, was 2 behind

Run it again straight away and nothing moves twice:

api: up to date
docs: diverged (1 ahead, 1 behind), left alone
scratch: no upstream, skipped
tools: refused, look at it by hand
web: up to date
  • Fetch and merge are run separately. git pull is both in one command, but it has no --no-prune-tags. The loop fetches with --no-prune --no-prune-tags so that no setting can delete a tag or a remote-tracking ref as a side effect, then runs git merge --ff-only @{u}, which is the second half of a pull.
  • [ -d "$d.git" ] skips linked worktrees, which have a .git file and share their repository’s branches.
  • api was pulled with an uncommitted edit in it. The incoming commit didn’t touch that file, so nothing was in the way.
  • refused can mean local changes, as on tools. Run git pull --ff-only in that repository to read Git’s own reason.
  • --quiet hides the fetch lines. Drop it to see each branch that moved.

Where the loop stops working

  • Only the current branch moves. Other branches stay where they were. The every-branch listing shows how each one stands.
  • It looks one level deep in one folder. A repository cloned elsewhere is never asked.
  • Uncommitted edits and your own commits stay yours. The loop never stashes, rebases or merges, so diverged and dirty repositories wait for you.
  • You have to remember to run it. After a long week, that is exactly when you don’t.

Or let your Mac keep watch

The other half of staying in sync is the commits you haven’t pushed. push& (Pushand) is a Mac menu bar app that watches the repositories you choose, worktrees included, and keeps a count of the commits waiting to be pushed. Expand a repository to read them, click a ready branch to push. Free for 3 repositories, and every install starts with a 14-day trial, no card and no account.

See the features

More Git how-tos