Git by Example: Push Changes
Learn how to upload your local commits to a remote repository. This example covers pushing branches, setting upstream tracking, and understanding the push process.
Code
# 1. Push a branch for the first time (set upstream)
git push -u origin feature-login
# Output:
# Enumerating objects: 5, done.
# ...
# Branch 'feature-login' set up to track remote branch 'feature-login' from 'origin'.
# 2. Push subsequent changes (upstream already set)
git push
# 3. Push a specific branch to a specific remote
git push origin main
# 4. Push all tags
git push --tagsExplanation
The git push command is used to upload your local repository content to a remote repository. It transfers commits from your local branch to a corresponding branch on the remote server (like GitHub, GitLab, or Bitbucket). This is how you share your work with others and back up your code.
When you push a new branch for the first time, you typically use the -u (or --set-upstream) flag. This tells Git to establish a link between your local branch and the new remote branch. Once this link is set, you can simply run git push in the future without specifying the remote or branch name, and Git will know where to send the changes.
Git protects you from overwriting others' work. If the remote branch has changes that you don't have locally (meaning your history has diverged), Git will reject your push. You will typically need to git pull those changes first to merge them into your local branch before you can push again.
- Uploads local commits to remote
- Use
-uto set upstream tracking - Enables collaboration and backup
- Rejects pushes if remote is ahead (requires pull)

