git push and upstream tracking
- Version
- 2.51
- Review
- Draft
Summary#
Use git push -u to publish a branch and record its upstream. Git uses that association for git pull and ahead/behind reporting. @{upstream} names the local remote-tracking ref in the usual setup.
Syntax#
git push -u origin topic
git push -u origin topic:review-topic
git push
Example#
(
set -eu
scratch=$(mktemp -d "${TMPDIR:-/tmp}/devlookup-git.XXXXXX")
trap 'rm -rf -- "$scratch"' EXIT
unset GIT_CONFIG_GLOBAL $(git rev-parse --local-env-vars)
export GIT_CONFIG_NOSYSTEM=1 GIT_TEMPLATE_DIR= LC_ALL=C
export HOME="$scratch" XDG_CONFIG_HOME="$scratch" GIT_ATTR_NOSYSTEM=1
export GIT_AUTHOR_NAME="Reference Example" GIT_AUTHOR_EMAIL=example@example.invalid
export GIT_COMMITTER_NAME="Reference Example" GIT_COMMITTER_EMAIL=example@example.invalid
git init -q --bare -b trunk "$scratch/server.git"
git init -q -b trunk "$scratch/work"
cd "$scratch/work"
git commit -q --allow-empty -m "Start"
git remote add origin "$scratch/server.git"
git push -q -u origin trunk > /dev/null
git rev-parse --abbrev-ref --symbolic-full-name '@{upstream}'
git clone -q "$scratch/server.git" "$scratch/peer"
git -C "$scratch/peer" commit -q --allow-empty -m "Peer update"
git -C "$scratch/peer" push -q
git commit -q --allow-empty -m "Local update"
if git push -q 2> /dev/null; then exit 1; fi
echo "divergent push rejected"
git --git-dir="$scratch/server.git" log -1 --format=%s trunk
)
Expected output#
origin/trunk
divergent push rejected
Peer update
Common mistakes#
fatal: The current branch topic has no upstream branch. means that a plain git push has no destination for this branch. Name it once with git push -u origin topic. A push rejected with (fetch first) or (non-fast-forward), followed by a hint that starts Updates were rejected because, means that the remote branch holds commits your branch lacks. Fetch and inspect them before choosing how to integrate them.
In topic:review-topic, the left side is local and the right side is remote. With the default push.default=simple, a plain push to the upstream remote requires matching branch names. Repeat the explicit refspec if the names differ.
Version applicability#
Git 1.7.0 added --set-upstream and @{upstream}. Git 2.0 made simple the default push mode. The complete example requires Git 2.28 or later for git init -b.
Sources#
- Attribution
- DevLookup contributors
- License
- CC-BY-4.0