git
version control
branching
commit
software development

Push commits to another branch

Interview Questions practice on Codemia

Over 8,000 real interview questions from top companies, searchable by company and role.

Browse interview questions

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.

bash
git push origin my-feature:staging

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:

bash
git push origin HEAD:staging

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:

bash
git push origin HEAD:release-candidate

After that, set upstream tracking if you plan to keep working with that branch name:

bash
git branch --set-upstream-to=origin/release-candidate

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:

bash
git switch target-branch
git cherry-pick <commit-sha>

For multiple commits:

bash
git switch target-branch
git cherry-pick <older-sha>^..<newer-sha>

Then push the target branch normally:

bash
git push origin target-branch

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.

bash
git switch target-branch
git merge source-branch
git push origin target-branch

Choose based on intent:

  • use push local:remote to control the remote branch name
  • use cherry-pick to copy specific commits
  • use merge or rebase to 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:

bash
git log --oneline origin/staging..HEAD

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 push as though it moves commits between branches inside your local history. It updates refs on the remote.
  • Pushing HEAD to the wrong remote branch name because you did not inspect the current checkout first.
  • Using push when the real need is cherry-pick or merge.
  • 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:remote sends a local branch to a chosen remote branch name.'
  • 'git push origin HEAD:remote-branch is a concise form when you are already on the correct branch.'
  • Use cherry-pick when specific commits must appear on another branch.
  • Use merge or rebase when integrating broader branch history.
  • The right command depends on whether you are updating a remote ref or changing commit membership between branches.

Related reading
Free course
Beginner
7 lessons
2 hours
Tackling System Design Interview Problems

A short course that equips you with the skills to approach system design interviews methodically.

Start the free course
Track 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.

Browse interview questions

All Rights Reserved.