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

Git push Command

git push is like handing in your homework to the class folder online so everyone else can see and use what you did.
Syntax
bash
git push remote_name branch_name
git push -u remote_name branch_name
git push --all remote_name

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
Related Topics
{# common_mistakes/chapter_summary/browser_support: on Hindi pages the view already swaps in the hi_ translation fields (or blanks these out if untranslated), so this renders correctly for both languages without a lang_code check here. #}
Common Mistakes
  1. Pushing a new branch with plain git push and getting no upstream branch, when the first push needs -u origin <branch>.
  2. Getting rejected (non-fast-forward) and forcing it with --force, overwriting your teammates' commits instead of running git pull first.
  3. Forgetting that git push does not send tags, when you need git push --tags.

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.