Git Push
In short
Git push is a Git command that uploads your local commits to a remote repository, updating the matching branch there so others can see and use your work.
What is git push?
git push sends commits from your local repository to a remote repository, such as one on a code hosting platform or a company server. Until you push, your commits exist only on your own machine; after pushing, teammates can pull them, CI pipelines can test them, and you can open a pull request. A typical command is git push origin main, which means 'send my main branch to the remote named origin'.
The first time you push a new branch, git push -u origin <branch> also sets it as the branch's upstream, so later a plain git push or git pull knows where to go. By default, Git only accepts a push that is a fast-forward, meaning the remote branch's latest commit is already part of your history. If someone else pushed first, Git rejects your push, and you need to pull their changes, resolve any conflicts, and push again.
Pushing is like publishing a draft to a shared folder: until you do, your edits are private, and afterward everyone can build on them. Hosting platforms can add rules on top of this, such as protected branches that refuse direct pushes to main and require a reviewed pull request instead, and server-side Git hooks can reject pushes that break team policies.
Push is often confused with commit. git commit saves a snapshot locally and git push shares commits that already exist, so you always commit before you push. Force pushing with git push --force overwrites the remote branch with your version even if that deletes other people's commits, so it should never be used on shared branches; after rebasing your own branch, prefer git push --force-with-lease, which refuses to overwrite commits you haven't seen.
Key takeaways
git pushuploads local commits to a branch on a remote repository.git push -u origin <branch>publishes a new branch and sets its upstream for later pushes and pulls.- Git rejects pushes that are not fast-forwards; pull first, then push again.
--forcecan erase other people's work, while--force-with-leaserefuses to overwrite commits you haven't fetched.- Commits stay local until pushed, and tags must be pushed explicitly.
Example
# Publish a new branch and set its upstream
git switch -c feature/search
git commit -am "Add search endpoint"
git push -u origin feature/search
# Later pushes on the same branch need no arguments
git push
# Rejected because the remote has new commits? Sync, then push again
git pull --rebase
git push
# After rebasing your own branch, overwrite it safely
git push --force-with-leaseReaders ask
Why was my git push rejected?
Usually because the remote branch has commits you don't have yet, so your push would not be a fast-forward. Run git pull or git pull --rebase, resolve any conflicts, and push again; other causes include protected branches and missing write permission.
What is the difference between git push --force and --force-with-lease?
--force replaces the remote branch with yours no matter what is on it. --force-with-lease first checks that the remote branch is still where you last saw it and refuses if someone has pushed new commits in the meantime, so you can't wipe out their work by accident.
How do I delete a remote branch?
Run git push origin --delete <branch>. This removes the branch from the remote only, so delete your local copy separately with git branch -d <branch>.
See also
- Git PullVersion Control, p. 17Git pull is a Git command that downloads new commits from a remote repository and immediately integrates them into your current local branch.
- Git RemoteVersion Control, p. 19A Git remote is a named shortcut, such as origin, to another copy of your repository, usually on a server, that you push commits to and pull commits from.
- CommitVersion Control, p. 5A commit is a saved snapshot of a project's files in Git, recorded with a unique ID, an author, a timestamp, and a message describing what changed.
- BranchVersion Control, p. 2A branch in Git is an independent line of development that lets you work on a feature or fix in isolation, without affecting the main code until you merge it.
- Pull RequestVersion Control, p. 32A pull request is a proposal to merge changes from one branch into another, giving teammates a place to review, discuss, and test the code before it is merged.
- RebaseVersion Control, p. 33A rebase in Git replays a branch's commits on top of another commit, rewriting history to produce a cleaner, linear sequence of changes without merge commits.
Spotted a mistake or something missing on this page?Suggest an edit