← Back to Git Course | Chapter 4: Remote Repositories | Lesson 7 of 11

Git push Command

What is Git Push?

git push uploads the commits on your current local branch to the matching branch on a remote repository, making your work visible and available to everyone else who has access to that remote.

Example: What is Git Push?

bash
git push origin main

Pushing a Branch to Remote

The first time you push a brand-new local branch, git push -u origin <branch> (or --set-upstream) links it to a remote branch of the same name, so every future push or pull from that branch can be a plain git push with no extra arguments. After the upstream link is set once, later pushes of that same branch only need git push (or git push origin <branch> explicitly), since Git already knows which remote branch to update -- explicitly naming the branch is still useful in scripts or CI where the current branch shouldn't be assumed.

Example: Pushing a Branch to Remote

bash
git push -u origin feature-login

Pushing All Branches

git push --all sends every local branch to the remote in one command, which is convenient when you've been working across several branches locally and need to publish all of them at once.

Example: Pushing All Branches

bash
git push --all

Pushing Tags

Tags aren't included in a normal push by default -- you need git push --tags (or git push origin <tagname> for just one) to explicitly upload them, since tags are treated as separate from regular commit history.

Example: Pushing Tags

bash
git push --tags
git push origin v1.0

Force Pushing Safely

Force pushing (git push -f) overwrites the remote's history with your local version, which can silently destroy commits a teammate already pushed. git push --force-with-lease is the safer alternative: it refuses to overwrite the remote if it has commits you haven't seen yet.

Example: Force Pushing Safely

bash
git push --force-with-lease

Login to run this code

C/C++/Java/PHP execution requires a free account. Your code is saved — you'll land right back in the editor after logging in.