How to Push to Remote in Git: Simple Commands and Examples
To push your local commits to a remote repository in Git, use the command
git push <remote-name> <branch-name>. Typically, remote-name is origin and branch-name is main or master. This uploads your changes so others can access them.Syntax
The basic syntax to push changes to a remote Git repository is:
git push <remote-name> <branch-name>: Sends your local branch commits to the remote repository.remote-name: The name of the remote repository, usuallyorigin.branch-name: The branch you want to push, likemainormaster.
bash
git push <remote-name> <branch-name>Example
This example shows how to push your local main branch to the remote named origin. It assumes you have already committed your changes.
bash
git push origin mainOutput
Enumerating objects: 5, done.
Counting objects: 100% (5/5), done.
Delta compression using up to 8 threads
Compressing objects: 100% (3/3), done.
Writing objects: 100% (3/3), 300 bytes | 300.00 KiB/s, done.
Total 3 (delta 1), reused 0 (delta 0), pack-reused 0
To https://github.com/user/repo.git
abc1234..def5678 main -> main
Common Pitfalls
Some common mistakes when pushing to remote include:
- Trying to push without committing changes first.
- Using the wrong branch name or remote name.
- Not having permission to push to the remote repository.
- Conflicts if the remote branch has changes you don't have locally.
Always pull changes first if others have updated the remote branch.
bash
git push origin main # Wrong branch example # git push origin master # if your branch is actually 'main' # Correct way git push origin main
Quick Reference
| Command | Description |
|---|---|
| git push origin main | Push local 'main' branch to remote 'origin' |
| git push origin feature-branch | Push local 'feature-branch' to remote 'origin' |
| git push -u origin main | Push and set upstream tracking for 'main' branch |
| git push --force origin main | Force push to remote (use with caution) |
Key Takeaways
Use
git push <remote> <branch> to upload local commits to a remote repository.The default remote name is usually
origin and the main branch is often main.Commit your changes locally before pushing to avoid errors.
Pull remote changes first to prevent conflicts before pushing.
Use
git push -u to set upstream tracking for easier future pushes.