# git push

Until you run git push, your commits exist only on your own disk. The command sends them to a remote repository and moves the branch pointer there — which is also why it is the command that most often refuses, when someone else got their work in first.

## What goes to the server

Git sends commits — the ones the server does not have yet — and the new position
of the branch pointer. Uncommitted edits, files in your working directory and the
contents of the index go nowhere: first [git commit](https://skillok.org/topics/git-commit-command),
then push.

The server accepts a change only if it continues the history it already has.
That kind of continuation is called a fast-forward. If the server branch has
moved ahead or your history was rewritten, Git stops and touches nothing — this is
the main safeguard against losing other people's work.

## Syntax and options

```bash
git push <remote> <branch>
```

Most often the remote is called `origin`, and the branch is the current one. When
a branch is linked to a server branch (its upstream), plain `git push` is enough.

| Option | What for |
| --- | --- |
| `-u`, `--set-upstream` | push and link the branch to the server branch — needed once |
| `--force-with-lease` | overwrite the server branch, but only if it has no commits you have not seen |
| `-f`, `--force` | overwrite unconditionally; other people's work can disappear |
| `--tags` | push tags, which an ordinary push does not send |
| `--delete <branch>` | delete a branch on the server |
| `-n`, `--dry-run` | show what would happen without changing anything |

## Example: the first push of a new branch

```bash
git switch -c feature/login
git add login.py
git commit -m "Add login form"
git push -u origin feature/login
```

```
Enumerating objects: 8, done.
Counting objects: 100% (8/8), done.
Compressing objects: 100% (4/4), done.
Writing objects: 100% (5/5), 512 bytes | 512.00 KiB/s, done.
Total 5 (delta 2), reused 0 (delta 0)
To https://github.com/example/notes.git
 * [new branch]      feature/login -> feature/login
branch 'feature/login' set up to track 'origin/feature/login'.
```

Without `-u`, Git reminds you itself:

```
fatal: The current branch feature/login has no upstream branch.
To push the current branch and set the remote as upstream, use

    git push --set-upstream origin feature/login
```

From then on, `git push` with no arguments is enough for this branch.

## The non-fast-forward rejection

The most common picture: while you were working, someone else's commit landed on
the server branch.

```
To https://github.com/example/notes.git
 ! [rejected]        main -> main (fetch first)
error: failed to push some refs to 'https://github.com/example/notes.git'
hint: Updates were rejected because the remote contains work that you do not
hint: have locally.
```

The note may say `fetch first` or `non-fast-forward` — the second appears when your
history has diverged from the server's, for example after `--amend` or a rebase.
The fix is the same: first get their work, then send yours.

```bash
git pull --rebase
git push
```

The rebase moves your commits on top of theirs, and the push becomes an ordinary
fast-forward. Details are in the article on [git pull](https://skillok.org/topics/git-pull-command).
Never answer this rejection with `--force`: it wipes from the server exactly the
commits that caused the rejection.

## When you really do need to rewrite

There are legitimate cases: you fixed the message of your last commit on a
personal branch nobody else is looking at. Then instead of `--force` use its
careful relative:

```bash
git push --force-with-lease origin feature/login
```

The option checks that the server branch is still where you last saw it. If
someone else's commit has appeared, the push is rejected with `stale info`, and
you investigate instead of overwriting. On a shared branch like `main`, do not
rewrite history at all — the reasons are spelled out in
[git rebase](https://skillok.org/topics/git-rebase-command).

## How to take back what you pushed

The commit is on the server and others have seen it — there is only one safe way:

```bash
git revert 9d31c07
git push
```

A new commit appears that cancels the effect of the old one. History stays
continuous, and nobody's copy breaks.

A branch created by mistake is removed entirely:

```bash
git push origin --delete feature/login
```

And a tag pushed by accident goes with `git push origin --delete v1.4`. Remember
that anything pushed to a shared server may already have been downloaded, and a
secret that ended up in a commit counts as compromised whatever you do with the
history: change the password or token, do not try to hide it. The other commands
for talking to a server are in the [cheat sheet](https://skillok.org/topics/git-commands-cheat-sheet).

## Step-by-step plan

1. **Make sure everything is committed** — git status: uncommitted edits do not go to the server.
2. **Get other people's work** — git pull --rebase so your commits sit on top of the server's.
3. **Push with tracking** — The first time git push -u origin branch, after that just git push.
4. **Read the server's reply** — A new branch line or a hash range means accepted; rejected means read the hint.
5. **Fix with a revert, not with force** — git revert for shared branches, --force-with-lease only for your own.

## Check yourself

1. First push of a new branch: which command both pushes it and links it to the server branch?
   - git push -u origin feature
   - git push --tags
   - git push origin --delete feature
2. A push was rejected as non-fast-forward. What do you do first?
   - Get the other commits: git pull --rebase
   - Repeat the push with --force
   - Delete the branch on the server and create it again
3. Why is --force-with-lease better than plain --force?
   - It refuses to overwrite the branch if commits you have not seen appeared on the server
   - It is noticeably faster on large repositories
   - It keeps a backup of the branch on the server

## Sources

- [Pro Git book](https://git-scm.com/book/en/v2) — The section “Working with Remotes”
- [git push reference](https://git-scm.com/docs/git-push) — Push options and what the rejections mean
- [GitHub Docs: Removing sensitive data](https://docs.github.com/en/authentication/keeping-your-account-and-data-secure/removing-sensitive-data-from-a-repository) — What to do if a secret was pushed

---

https://skillok.org/articles/en/git-push-command
