git push: send commits to the server

Updated July 31, 2026

git push copies commits from your local branch to the remote repository and moves the branch there forward. Until you push, all the work lives only on your disk, and losing the machine means losing the history.

Push does not touch anyone else's working directory. Your colleagues see your commits once they run git pull or git fetch themselves. The exchange always happens on your command: the server never reaches into your project.

Sending a new branch out for the first time looks like this:

$ git push -u origin main
Enumerating objects: 3, done.
Writing objects: 100% (3/3), 226 bytes | 226.00 KiB/s, done.
To github.com:username/my-project.git
 * [new branch]      main -> main
branch 'main' set up to track 'origin/main'.

origin is the name of the remote, main is the name of the branch. The -u flag (spelled out, --set-upstream) remembers the pairing: your local main is now tied to origin/main. After that a bare git push is enough, and git status starts telling you how many commits you are ahead of the server.

Without -u on a new branch, git does not guess where to put it:

$ git push
fatal: The current branch feature-search has no upstream branch.
To push the current branch and set the remote as upstream, use

    git push --set-upstream origin feature-search

Updates were rejected

The most common refusal:

$ git push
To github.com:username/my-project.git
 ! [rejected]        main -> main (fetch first)
error: failed to push some refs to 'github.com:username/my-project.git'
hint: Updates were rejected because the remote contains work that you do not
hint: have locally. This is usually caused by another repository pushing to
hint: the same ref. If you want to integrate the remote changes, use
hint: 'git pull' before pushing again.

There are commits on the server that you do not have, and push refuses to overwrite them. The cure is always the same order: git pull, deal with the merge, then git push again. This is routine, not damage. --force is no help here: it replaces the server branch with your version and throws someone else's work away.

Deleting a branch on the server

A local git branch -d leaves the server copy alone. You remove it with a separate command:

$ git push origin --delete feature-search
To github.com:username/my-project.git
 - [deleted]         feature-search

Common mistakes

Push sends only what is committed. Edits sitting in the working directory or in the index do not travel, so it is worth a look at git status before pushing.

Push does not send tags either. Branches go out, but v1.0 stays with you until you run git push origin v1.0. See git tag for the details.

Where this is in the book

Chapter 6. GitHub: the cloud and working together.

To practise it by hand, open the Sandbox: a real terminal with git and step checks.