Git 2 reference · Remotes

git push Command (Git)

Upload local commits and tags to a remote branch; -u sets the upstream, and --force-with-lease rewrites history safely.

Git 2

Create a free account to save this page, take notes and track your progress. Sign in

Syntax

git push [-u] [REMOTE] [BRANCH]   git push --force-with-lease   git push --tags

Parameters

ParameterDescription
-u origin BRANCH Push and set upstream tracking.
--force-with-lease Overwrite only if the remote hasn't moved.
--tags Push tags.
-d origin BRANCH Delete a remote branch.

Examples

First push with upstream
git init -q -b main app && cd app
echo '# Shop' > README.md
git add README.md && git commit -q -m 'Initial commit'
git init -q --bare ../origin.git; git remote add origin ../origin.git
git push -q -u origin main
git status -sb | head -1
git ls-remote ../origin.git | cut -c42-

Output

## main...origin/main
refs/heads/main

Run this on your computer — see “Get Started” for setup.

Rewriting a pushed branch
git init -q -b main app && cd app
echo '# Shop' > README.md
git add README.md && git commit -q -m 'Initial commit'
git init -q --bare ../origin.git; git remote add origin ../origin.git; git push -q -u origin main
git commit -q --amend -m 'Initial commit (reworded)'
git push -q origin main 2>&1 | grep -o 'rejected' | head -1
git push -q --force-with-lease origin main && echo 'rewritten safely'

Output

rejected
rewritten safely

Run this on your computer — see “Get Started” for setup.

Was this page helpful?