git push is the command used to upload your local repository content (your commits) to a remote repository (like GitHub).
Commits made using git commit are completely localized. They exist only on the hard drive of your laptop. If your laptop gets stolen or the hard drive crashes, you lose your work. Pushing uploads those commits to the cloud (GitHub) so they are securely backed up. Furthermore, if you are working on a team, pushing is how you share your code with the rest of the developers.
For the very first push of a branch:
git push -u origin <branch_name>For all future pushes on that branch:
git push| Part | Meaning |
|---|---|
git push |
Uploads local commits to a remote server |
-u |
Stands for "--set-upstream". It creates a permanent link between your local branch and the remote branch. |
origin |
The nickname of the remote destination (GitHub) |
<branch_name> |
The branch you are pushing (e.g., main or feature-login) |
When you run git push, Git calculates the difference between your local branch and the remote branch on GitHub. It then securely packages the new commits, compresses them, and sends them over HTTPS or SSH. Once GitHub receives them, the remote branch is updated to match your local branch.
The most common error you will face is:
error: failed to push some refs to 'https://github.com/...'
hint: Updates were rejected because the remote contains work that you do not have locally.
Why this happens: Someone else (or you, from a different computer) pushed code to GitHub while you were working. GitHub refuses your push because accepting it would overwrite the other person's code.
The Fix: You must merge their code into yours before you can push.
- Run
git pull origin main(This downloads their code and merges it with yours). - Fix any merge conflicts if they exist.
- Run
git pushagain.
- Pushing without committing: You can only push commits. If you just made changes in your code editor and didn't
addandcommitthem,git pushwill say "Everything up-to-date", but your new code won't be on GitHub. - Pushing secrets: If you accidentally commit an API key or password and run
git push, it is now public on the internet. Hackers scan GitHub 24/7 for leaked keys. Never push.envfiles!
- Uploads your saved local commits to GitHub.
- Use
git push -u origin mainthe very first time you push a branch. - Use
git pushafter that. - Always commit before pushing!
- If a push is rejected, you must
git pullfirst.