Push commits to another branch
Interview Questions practice on Codemia
Over 8,000 real interview questions from top companies, searchable by company and role.
Introduction
In Git, "push commits to another branch" can mean two different things. Sometimes you want to send your current local branch to a differently named remote branch. Other times you want specific commits to end up on another branch entirely, which is usually a cherry-pick or merge problem rather than a push syntax problem.
Push the Current HEAD to a Different Remote Branch
If your local branch contains the commits you want, and you simply want the remote branch name to differ, use the local:remote push syntax.
This means:
- take commits from local branch
my-feature - update remote branch
staging
You can also push the current checked-out branch without repeating its name:
That is useful when you are already on the correct local branch and only care about the remote target.
Create the Remote Branch If Needed
If the remote target branch does not exist yet, the same push command can create it:
After that, set upstream tracking if you plan to keep working with that branch name:
Be careful here. Pushing to a differently named branch is legal, but it can confuse teammates if the mapping is not intentional and documented.
When You Actually Need cherry-pick
If your commits live on the wrong local branch and you want them moved or copied onto another branch, git push alone is not the right tool. The clean approach is usually:
For multiple commits:
Then push the target branch normally:
This is a better mental model than trying to force Git to "push one commit to another branch". Push transfers branch references. cherry-pick changes which branch contains the commit.
Merge and Rebase Are Different Again
If the goal is to bring all work from one branch into another, a merge or rebase may be more appropriate than cherry-picking individual commits.
Choose based on intent:
- use
push local:remoteto control the remote branch name - use
cherry-pickto copy specific commits - use
mergeorrebaseto integrate branch history
Trying to solve all three with the same command is where Git starts feeling harder than it actually is.
Safer Workflow Tips
Before pushing to a different branch name, inspect what will be sent:
That shows which local commits are ahead of the remote target. It is a simple way to catch "wrong branch" mistakes before updating the remote.
If the remote branch is shared, avoid force-pushing unless you fully understand the impact on collaborators.
Common Pitfalls
- Treating
git pushas though it moves commits between branches inside your local history. It updates refs on the remote. - Pushing
HEADto the wrong remote branch name because you did not inspect the current checkout first. - Using
pushwhen the real need ischerry-pickormerge. - Forgetting to review which commits are ahead of the target branch before pushing.
- Force-pushing to a shared branch after rewriting history without coordinating with the team.
Summary
- '
git push origin local:remotesends a local branch to a chosen remote branch name.' - '
git push origin HEAD:remote-branchis a concise form when you are already on the correct branch.' - Use
cherry-pickwhen specific commits must appear on another branch. - Use
mergeorrebasewhen integrating broader branch history. - The right command depends on whether you are updating a remote ref or changing commit membership between branches.
Related reading
- Push git commits tags simultaneously
- Push local Git repo to new remote including all branches and tags
- Push local Git repo to new remote including all branches and tags
- Pushing an existing Git repository to SVN
- Pushing empty commits to remote
- Pushing to Git returning Error Code 403 fatal HTTP request failed
- Pushing to Git returning Error Code 403 fatal HTTP request failed
- Putting Git hooks into a repository
.png&w=3840&q=75)
Tackling System Design Interview Problems
A short course that equips you with the skills to approach system design interviews methodically.
Start the free courseTrack what you have practised
A free account saves your progress, solutions and study plan across every problem on Codemia.
Interview Questions practice on Codemia
Over 8,000 real interview questions from top companies, searchable by company and role.