Using git push to send commits to a remote repository is a fundamental part of modern development workflows. This action transfers your local branch changes to a shared location, enabling collaboration and backup. Understanding the precise syntax and behavior of this command helps teams avoid common pitfalls like overwriting work or creating confusing histories.
Basic Syntax and Target Specification
The core command follows a simple structure where you specify the remote name and the destination branch. If you are working on a feature branch locally, you instruct Git to send those commits upstream. The standard format requires clarity on where the code should land on the remote server.
Syntax: git push
usually refers to origin , the default name for your central repository.
is the name of the branch you want to update on the remote.
For example, running git push origin feature/login-page takes your local feature/login-page branch and attempts to update the remote branch of the same name. This straightforward approach works well when your local branch name matches the remote target, which is a common convention in many teams.
Tracking Relationships and Simplified Commands
After an initial push, Git establishes a tracking relationship between your local branch and the remote branch. This linkage allows you to simplify future commands significantly. You no longer need to specify the remote and target every single time you push.
Once tracking is set, you can use a streamlined version of the command. The system remembers the upstream branch, so you can execute a shorter instruction to update the code. This reduces typing and potential errors in your terminal sessions.
Setting Upstream and Subsequent Pushes
First push: git push -u origin feature/login-page
Subsequent pushes: git push
The -u flag (or --set-upstream ) creates the link between the branches. After this is done, running git push without arguments will push to the designated remote branch, making your workflow more efficient.
Handling Divergent Histories and Forced Updates
Collaboration sometimes leads to situations where both you and a teammate have pushed different commits to the same branch. In this scenario, your local history is behind the remote, and a standard push will be rejected. Git requires you to reconcile these differences before updating the remote branch.
If you need to overwrite the remote history—perhaps to fix a sensitive commit or squash messy work—you must use a force push. This operation replaces the remote history with your local history, which can be dangerous if other developers rely on the old commits.
Force Push Variants
git push --force : Overwrites the remote branch without checking for conflicts.
git push --force-with-lease : Safer option that fails if someone else has pushed new commits you don’t have locally.
Using --force-with-lease is generally the safer practice, as it protects against accidentally destroying work that others have contributed to the shared branch.
Pushing Multiple and New Branches
When you are working on several features simultaneously, you might need to push multiple branches to the remote repository. You can handle this efficiently without pushing every local branch. Git provides flags to control this behavior precisely.