git push
Update remote refs along with associated objects
The 'git push' command uploads local repository content to a remote repository. It transfers commits and branch updates (and tags, when you ask for them with --tags or --follow-tags) from your local repo to a remote, making your changes available to others. Push is how you share your work - after committing locally, you push to synchronize with team members. By default (push.default=simple), git push only updates the current branch's upstream branch, which must exist and have the same name, and only if it is a fast-forward update (meaning the remote branch is an ancestor of your local branch).
Synopsis
git push [--all | --mirror | --tags] [--follow-tags] [--atomic] [-n | --dry-run] [-f | --force] [-d | --delete] [--prune] [-q | --quiet] [-v | --verbose] [-u | --set-upstream] [-o <string>] [--force-with-lease[=<refname>[:<expect>]] [--force-if-includes]] [--no-verify] [<repository> [<refspec>...]]Options
Choosing the Right Option
Force Push Safety Ladder
When you need to overwrite remote history, choose the safest option that works for your situation
| Command | Risk Level | Checks Remote | When It Fails | Use Case |
|---|---|---|---|---|
git push | Safe | ✓ Fast-forward only | If remote has new commits | Normal daily workflow |
git push --force-with-lease --force-if-includes | Safest force | ✓ Remote tip in local history | If you haven't integrated remote changes | After rebase when others may have pushed |
git push --force-with-lease | Safer force | ✓ Remote unchanged since fetch | If remote changed since last fetch | After rebase on personal branch |
git push --force | Dangerous | ✗ Overwrites blindly | Never (unless rejected by server) | Only when you're certain, or solo branch |
What Gets Pushed
Control the scope of what you're sending to the remote
| Command | Pushes | Tags Included | Typical Use |
|---|---|---|---|
git push origin main | Single branch | ✗ | Push specific branch |
git push -u origin branch | Single branch + sets tracking | ✗ | First push of new branch |
git push --all | All branches | ✗ | Sync all local branches |
git push --tags | Tags only (no branches) | ✓ All tags | Push version tags |
git push --follow-tags | Current branch | ✓ Reachable annotated only | Push with relevant release tags |
git push --mirror | Everything | ✓ All | Exact remote clone (deletes remote-only refs) |
Output & Feedback
Control verbosity and output format based on your needs
| Command | Output Level | Shows Progress | Machine Parseable | Best For |
|---|---|---|---|---|
git push | Standard | In terminal only | ✗ | Normal interactive use |
git push --verbose | Detailed | ✓ | ✗ | Debugging push issues |
git push --quiet | Errors only | ✗ | ✗ | Scripts, CI pipelines |
git push --progress | Standard + forced progress | ✓ Always | ✗ | Scripts that need progress in logs |
git push --porcelain | Structured | ✗ | ✓ Tab-separated | Parsing push results in scripts |
Safety & Verification
Pre-push checks and verification options
| Command | Runs Hooks | Verifies | Creates Commit | When to Use |
|---|---|---|---|---|
git push | ✓ pre-push | Remote accepts | N/A | Normal workflow |
git push --dry-run | ✓ pre-push | What would happen | ✗ Preview only | Before important pushes |
git push --atomic | ✓ | All refs or none | All-or-nothing | Pushing multiple branches together |
git push --no-verify | ✗ Skips local pre-push (server hooks still run) | Remote accepts | N/A | Emergency when hooks block you |
git push --signed | ✓ | GPG signature | Cryptographically signed | High-security repos, proving identity |
Remote & Branch Management
Options for managing remote refs and branch relationships
| Command | Action | Affects Remote | Use Case |
|---|---|---|---|
git push -u origin branch | Push + set upstream | Adds branch | First push, sets up tracking |
git push origin --delete branch | Delete remote branch | Removes branch | Clean up merged branches |
git push --prune | Delete remote branches you deleted locally (needs --all or a wildcard refspec) | Removes branches | Sync deletions to remote |
git push --repo=<url> | Push to specific remote | Target remote | Push to alternate remote URL |
Workflow Examples
git remote add origin https://github.com/user/repo.gitAdd the remote repository URL
git push -u origin mainPush main branch and set it to track origin/main
git statusVerify you're on the correct branch and have commits to push
git pushPush to the tracked upstream branch
git checkout featureSwitch to your feature branch
git rebase mainRebase onto main (rewrites commit history)
git push --force-with-leaseSafely force push - fails if someone else pushed
git checkout -b feature/new-loginCreate and switch to new branch
git add . && git commit -m "Add new login"Make your changes and commit
git push -u origin feature/new-loginPush and set upstream tracking
git push origin --delete feature/old-branchDelete the remote branch
git branch -d feature/old-branchDelete the local branch too
git push --all originPush all branches under refs/heads/
git push --tags originOptionally also push all tags
Troubleshooting
! [rejected] main -> main (fetch first) / (non-fast-forward)
Cause: The remote branch has commits that your local branch doesn't have. Someone else pushed changes, or you rebased/amended commits that were already pushed.
Pull the remote changes first with 'git pull --rebase' or 'git pull --no-rebase', resolve any conflicts, then push again. Only force (preferably '--force-with-lease') if you're certain no one else is using the branch.
git pull --rebase && git pushgit@github.com: Permission denied (publickey)
Cause: SSH authentication failed: no key is loaded, or your public key isn't added to your account. (Missing write access shows a different 'Permission to ... denied' error.)
Check that your SSH key is added to your GitHub/GitLab account. Verify with 'ssh -T git@github.com'. For HTTPS, ensure you're using the correct credentials.
ssh -T git@github.comfatal: No configured push destination
Cause: The repository has no remote configured at all. (If a remote exists but the branch has no upstream, you get 'The current branch ... has no upstream branch' instead.)
Add a remote with 'git remote add origin <url>', then push and set the upstream with 'git push -u origin <branch>'.
git push -u origin mainYour branch is behind 'origin/main'
Cause: The remote branch has newer commits than your local branch. You need to integrate those changes before pushing.
Fetch and merge or rebase the remote changes into your local branch before pushing.
git pull origin main && git pushrefusing to update checked out branch
Cause: You're trying to push to a non-bare repository where that branch is currently checked out. This could cause the working directory to become inconsistent.
Push to a bare repository, or have the receiver check out a different branch first. This usually happens with personal server setups, not with GitHub/GitLab.
git config receive.denyCurrentBranch updateInsteaderror: failed to push some refs to '<remote-url>'
Cause: Generic error that can have multiple causes: non-fast-forward updates, hook failures, or permission issues. Check the detailed error message above this line.
Read the full error output. Usually it's a non-fast-forward issue (pull first) or a pre-push hook failure (fix the issues the hook reports).
git pull --rebase && git pushpre-push hook failed
Cause: A pre-push hook (running tests, linting, etc.) found issues and blocked the push.
Fix the issues reported by the hook. Only bypass with '--no-verify' if absolutely necessary and you understand the consequences.
git push --no-verifyremote: Repository not found
Cause: The repository URL is incorrect, the repository was deleted, renamed, or you don't have access to it.
Verify the repository URL with 'git remote -v'. Check that the repository exists and you have access. Update the URL if needed.
git remote set-url origin https://github.com/user/correct-repo.gitTips & Pitfalls
Best Practices
- ✓Always pull before push: Run 'git pull' (or 'git fetch' followed by a merge or rebase) before pushing to ensure you have the latest remote changes and avoid rejected pushes.
- ✓Use --force-with-lease instead of --force: If you must force push, --force-with-lease is safer because it fails if someone else pushed to the remote since your last fetch (add --force-if-includes for extra safety).
- ✓Set upstream on first push: Use 'git push -u origin branch-name' when pushing a new branch to set up tracking. This lets you use plain 'git push' afterward.
- ✓Never force push to shared branches: Force pushing to main, master, or develop can destroy other people's work. Use revert commits instead of rewriting history.
- ✓Use branch protection rules: Configure your repository to prevent force pushes to important branches and require pull request reviews.
- ✓Push frequently to backup your work: Regular pushes to feature branches serve as backups and let others see your progress.
- ✓Verify before pushing: Use 'git push --dry-run' to see what would be pushed without actually pushing. Great for double-checking.
- ✓Clean up remote branches: Delete merged feature branches with 'git push origin --delete branch-name' to keep the remote repository tidy.
- ✓Use atomic pushes for related changes: When pushing multiple branches that depend on each other, use '--atomic' to ensure all succeed or none do.
- ✓Sign important pushes: For security-critical repositories, use '--signed' to GPG-sign your push requests for verification (the server must support signed pushes, otherwise the push fails).
Common Mistakes
- ✗Pushing without pulling first: If the remote has new commits, your push will be rejected. Always 'git pull' (or fetch, then merge/rebase) first to incorporate remote changes.
- ✗Using --force on shared branches: Force pushing rewrites history and can destroy other people's work. Use --force-with-lease instead, or better yet, avoid force pushing to shared branches entirely.
- ✗Forgetting to set upstream with -u: On first push of a new branch, use 'git push -u origin branch-name' to set up tracking. Otherwise you'll need to specify the remote and branch every time.
- ✗Pushing to the wrong branch: Double-check your current branch with 'git branch' before pushing. Use explicit syntax like 'git push origin feature:feature' to be safe.
- ✗Not understanding rejected pushes: 'non-fast-forward' errors mean the remote has commits you don't have locally. Pull and merge/rebase before pushing, don't just force push.
FAQ
'git push' uploads your local commits to a remote repository, making your changes available to others. 'git pull' downloads commits from a remote and merges them into your local branch. Push sends data out; pull brings data in.
You can't truly 'undo' a push since it's shared history. For safe reversal, use 'git revert HEAD' to create a new commit that undoes the changes, then push again. Avoid using 'git reset' + force push on shared branches as it rewrites history others may have pulled.
The -u flag sets up a tracking relationship between your local branch and the remote branch. After using 'git push -u origin feature', you can simply use 'git push' and 'git pull' without specifying the remote and branch each time.
Force push (--force) should only be used on branches where you're the only contributor, like personal feature branches after rebasing. Never force push to shared branches like main or develop. If you must, use --force-with-lease for safety.
Use the refspec syntax: 'git push origin local-branch:remote-branch'. For example, 'git push origin feature:experiment' pushes your local 'feature' branch to a remote branch called 'experiment'.
Usually because the remote has commits you don't have locally (non-fast-forward). Run 'git pull' to fetch and merge those changes, resolve any conflicts, then push again. Don't force push unless you're certain it's safe.
Use 'git push --all origin' to push all local branches to the remote. Tags need a separate command because '--all' and '--tags' cannot be combined: 'git push --tags origin'.
'--force' blindly overwrites the remote branch regardless of its state. '--force-with-lease' only overwrites if the remote hasn't changed since you last fetched - it fails if someone else pushed, protecting their work. Caveat: a fetch (even an IDE's background fetch) refreshes that check, so add --force-if-includes to also require that you've integrated what you fetched.
Use 'git push origin --delete branch-name' or the shorthand 'git push origin :branch-name'. Both remove the branch from the remote repository.
Plain 'git push' only works if the current branch has an upstream branch configured. For new branches, you need 'git push -u origin branch-name' first. Check with 'git branch -vv' to see tracking relationships.