Pushing your code is like publishing your article, until now, it only existed in your drawer. Once pushed, it is visible, accessible, and part of the official project. This is the moment when your work becomes "real" for the team.
The basic push
The simplest commands:
# Push the current branch to origin
git push
# Push a specific branch
git push origin feature-login
# Push main to origin
git push origin mainIf everything goes well, Git displays something like:
Enumerating objects: 5, done.
Counting objects: 100% (5/5), done.
Writing objects: 100% (3/3), 312 bytes | 312.00 KiB/s, done.
To https://github.com/user/repo.git
a1b2c3d..e4f5g6h main -> mainThe first push: configure tracking
When you push a branch for the first time, Git does not know where it should go:
# Push AND configure tracking (upstream)
git push -u origin feature-login
# Or the long version
git push --set-upstream origin feature-loginAfter that, you can simply run git push on this branch, Git knows where to send.
What is tracking (upstream)?
Tracking creates a link between your local branch and a remote branch:
| With upstream configured | Benefit |
|---|---|
git push | Automatically knows where to push |
git pull | Automatically knows where to pull from |
git status | Tells you if you are ahead or behind the remote |
The danger of force pushWhat is force push?A destructive Git push that overwrites the remote branch history with your local version, which can break teammates' work if they already pulled the original.
Force push is the nuclear weapon of Git. AI tools sometimes suggest it casually, but it can destroy your teammates' work.
# DANGER: can overwrite others' work
git push --forcegit push --force as the first solution. This is almost always wrong on shared branches. The correct approach is to pull first, resolve conflicts, and then push. Force push rewrites remote history and can permanently destroy commits your teammates have already pulled.The safer alternative: force-with-lease
# Fails if someone else pushed in the meantime
git push --force-with-leaseThis is like saying: "Force, but only if no one modified the remote since my last fetch." If someone pushed, Git refuses and warns you.
When NOT to force push:
- On
mainor any shared branch - If others have already pulled the branch
- When you are not sure what you are doing
When force-with-lease is acceptable:
- On a personal feature branch where you are the only contributor
- After a local rebaseWhat is rebase?A Git operation that replays your branch's commits on top of another branch's latest commit, creating a linear history instead of a merge commit. that you need to sync
- If you just amended a recent commitWhat is commit?A permanent snapshot of your staged changes saved in Git's history, identified by a unique hash and accompanied by a message describing what changed. and no one has seen it yet
Common errors and fixes
"rejected: non-fast-forward"
git push origin main
# ! [rejected] main -> main (non-fast-forward)Meaning: The remote has commits you don't have locally.
Solution:
# Retrieve and merge first
git pull origin main
# Resolve conflicts if necessary
git push origin main"Permission denied"
# fatal: unable to access
# The requested URL returned error: 403Possible causes:
- You cloned the original repo instead of your forkWhat is fork?A personal copy of someone else's repository on a platform like GitHub, letting you make changes freely and propose them back via pull request.
- Your SSH key or tokenWhat is token?The smallest unit of text an LLM processes - roughly three-quarters of a word. API pricing is based on how many tokens you use. expired
- You were not added as a collaborator
Delete a remote branch
When a feature branch is done and merged, clean it up:
# Delete the remote branch
git push origin --delete old-branchThis only deletes the remote branch. Your local branch still exists until you run git branch -d old-branch.
The complete workflow: from commitWhat is commit?A permanent snapshot of your staged changes saved in Git's history, identified by a unique hash and accompanied by a message describing what changed. to push
# 1. Work on your branch
git checkout -b feature-payment
# 2. Make changes and commit
git add .
git commit -m "Add Stripe payment system"
# 3. Push to remote (first time with -u)
git push -u origin feature-payment
# 4. Continue working...
git commit -m "Fix validation bug"
git push # No need to specify, upstream is configured
# 5. Create a Pull Request on GitHub/GitLab
# 6. After merge, clean up
git checkout main
git pull origin main
git branch -d feature-payment
git push origin --delete feature-paymentQuick reference
| Command | Description |
|---|---|
git push | Push current branch to its upstream |
git push origin branch | Push a specific branch |
git push -u origin branch | Push and configure upstream |
git push --all | Push all local branches |
git push --tags | Push all tags |
git push origin --delete branch | Delete a remote branch |
git push --force-with-lease | Safe force push (personal branches only) |