iwantcoding.com
🔥 Daily 👥 Rooms 🏆 Top Log in Sign up

git push

git push uploads commits to a remote branch. The -u flag sets upstream; force-with-lease pushes safely after a rebase; tags push separately by default.

push, force-with-lease, tags, safety

EXAMPLE
# First push of a new branch — set upstream
git push -u origin feat/checkout

# Subsequent pushes — just 'git push'
git push

# After a rebase, your local history diverges from upstream.
# NEVER --force; ALWAYS --force-with-lease.
git push --force-with-lease
#  → refuses if someone else pushed since your last fetch.
#    Protects collaborators from silent overwrites.

# Push a single tag
git push origin v1.4.0
# Push all tags (use sparingly)
git push --tags

# Delete a remote branch
git push origin --delete feat/checkout
# Or with shorter syntax
git push origin :feat/checkout

# Push to a different remote / branch name
git push upstream main:release

# Useful settings
git config --global push.default       current     # push current branch to same-named remote branch
git config --global push.autoSetupRemote true       # auto -u on first push

# Protected branches — push to main is blocked, must go via PR
# Set this up in your hosting provider (GitHub, GitLab, Gitea).

# Pre-push hooks — run tests, lint, secret scanner before allowing push
# .git/hooks/pre-push  (or in husky / lefthook config)
#   npm test && npm run lint

Why it matters

push.autoSetupRemote = true saves you typing -u origin BRANCH on every new branch. --force-with-lease instead of --force turns destructive pushes into safe ones.

Tip: Tweak the snippet with Try it Yourself », then sit the quiz at the bottom of the page.

Example

Example
git push origin main
git push -u origin feature  # set upstream
git push --force-with-lease # safer than --force
Try it Yourself »

Exercise

Set upstream + push current branch on the first push.

git push origin feature

Test yourself

Q1. Push current branch to origin with…
Q2. Set upstream the first time with…
Q3. Safer alternative to --force is…

Discussion

Loading…