git push — Uploading Local Commits to Remotes
Uploading commits with git push: upstream tracking (-u), safe force pushes (--force-with-lease), and resolving non-fast-forward rejections.
git push — Uploading Local Commits to Remotes
The git push command uploads local branch commits to the corresponding remote repository on GitHub or a Git server.
1. Syntax and Upstream Tracking (-u)
When pushing a newly created branch for the very first time, use the -u (set-upstream) flag to link your local branch to the remote branch:
# 1. First push of a new feature branch:
git push -u origin feat/user-auth
# 2. Subsequent pushes on this branch (upstream is saved):
git push2. Safe Force Pushing (--force-with-lease)
If you used git rebase or git commit --amend on your local feature branch, Git will reject normal git push because commit SHA hashes diverged.
Always Use --force-with-lease:
--force-with-lease checks if anyone else has updated the remote branch before overriding it. If new commits were pushed by a teammate, Git will abort and protect their work:
git push --force-with-lease origin feat/user-authRelated Topics
git pull — Fetching and Merging Remote Updates
How git pull works: git fetch + git merge combination, rebase pulls (git pull --rebase), fast-forward only, and preventing unwanted merge commits.
git reflog — The Developer Safety Net
How to rescue lost commits and branches with git reflog: reference logs, recovering from accidental git reset --hard, and recovering deleted branches.