Daily sync with upstream
Before you start work on a tracked feature branch, run a bare git pull so the branch matches its upstream without merge commits.
Git · Command
git pull brings changes from another repository, or from a local branch, into the branch you have checked out. It runs a fetch and then integrates the fetched branch by fast-forwarding, rebasing, merging or squashing.
Last updated
git pull runs in two stages. First it calls git fetch with the same arguments, leaving out merge-only options, to download objects and update remote-tracking refs. Next it chooses a branch to integrate. Branches named on the command line win; otherwise Git uses the current branch's configured upstream (branch.<name>.remote and branch.<name>.merge). Last, it integrates that branch. By default it only allows a fast-forward. You can choose another method with --rebase, --no-rebase (merge) or --squash, or set it once with pull.rebase, pull.squash or pull.ff.
git pull [<options>] [<repository> [<refspec>…]]| Name | Type | Description |
|---|---|---|
<repository> | URL or remote name | The repository to pull from. If you leave it out, Git uses the current branch's upstream, or origin. |
<refspec> | refspec (branch, tag or pattern) | The ref or refs to fetch and integrate, such as main in git pull origin main. If you leave it out, Git uses the configured upstream. |
--ff-only | flag | Update only when the local branch has not diverged from the remote. This is the default when no method is chosen. |
--rebase[=(true|merges|false|interactive)] | flag with optional value | Rebase the current branch onto the upstream after fetching. merges keeps local merge commits, false merges instead, and interactive opens an interactive rebase. |
--no-rebase | flag | Short form of --rebase=false. The fetched branch is merged. |
--squash | flag | Put the merged result in the working tree and index without committing or recording MERGE_HEAD. |
--autostash | flag | Stash local changes before the operation and apply them again when it finishes. |
-s <strategy> / -X <option> | string | Pick the merge strategy, or pass an option to the strategy. |
git pull origin next gives the same result as running git fetch origin and then git merge origin/next. The fetched tip is also kept for a while in FETCH_HEAD.#!/usr/bin/env bash
set -euo pipefail
# Optional: make rebase the default way to pull in this repo
git config pull.rebase true
# Try a fast-forward first; fall back to a rebase if branches diverged
if ! git pull --ff-only origin main; then
echo "Branches diverged; rebasing local commits onto origin/main"
git pull --rebase --autostash origin main
fi
# Same thing in two steps: fetch, then merge
git fetch origin
git merge --ff-only origin/main
git log --oneline -5The script tries a fast-forward pull first and rebases (stashing any uncommitted work) only when the branches have diverged. It then shows the same update done as a separate fetch and merge.
Before you start work on a tracked feature branch, run a bare git pull so the branch matches its upstream without merge commits.
Your push was rejected because others pushed first. Run git pull --rebase to replay your unpublished commits on top of theirs, then push again.
A maintainer runs git pull <URL> <branch> to fetch a contributor's branch and merge it in one step.
Run pull with --depth, --deepen or --unshallow to fetch more history into a shallow CI checkout while still updating the branch.
git fetch only downloads objects and updates remote-tracking refs such as origin/main. Your current branch stays where it is. git pull runs that same fetch and then integrates the chosen remote branch into your current branch. git pull origin next is roughly the same as git fetch origin followed by git merge origin/next.
Follow-up: Which merge options does pull pass to the fetch step? None. Pull hands fetch the same arguments minus the merge-related options, and uses those options only when it integrates.
Pull's default is fast-forward only, so it fails and leaves your branch as it was. You need to say how to reconcile the branches. Use --rebase to replay your commits on the upstream, --no-rebase to create a merge, or set pull.rebase or pull.ff in your config.
Follow-up: How do you get out of a pull that ended in conflicts you don't want to resolve? Run git merge --abort or git rebase --abort, depending on how pull integrated. After a messy pull you can also go back to an earlier state with git reset.
It uses the current branch's upstream, set by branch.<name>.remote and branch.<name>.merge. Without those it falls back to origin and the configured remote.<origin>.fetch refspecs. If branch.<name>.merge is missing, it integrates the first configured refspec. If that refspec is a glob, nothing is integrated.
Follow-up: Why is listing two branches on the command line different from configuring two fetch refspecs? Branches named on the command line are all merged, which gives an octopus merge. Configured refspecs are all fetched, but only the first is merged.
git fetchgit mergegit rebasegit pushgit remotegit configgit stashgit reset