How to Push to a Different Remote Branch in Git

Sometimes you need to push local changes to a remote branch with a different name. Git provides a simple way to achieve this. Here's how you can do it.

1. Understand the Default Behavior

By default, Git pushes your local branch to a remote branch with the same name. For example:

git

This pushes your local main branch to the remote main branch.

2. Push to a Different Remote Branch

To push your local branch to a remote branch with a different name, use the following syntax:

git

Example:

If your local branch is feature-xyz and you want to push it to the remote branch dev:

git

This sends the changes in feature-xyz to the dev branch on the remote.

3. Push to Create a New Remote Branch

If the remote branch does not exist yet, Git will create it. For example:

git

This creates a new branch new-branch on the remote and pushes your local feature-xyz changes to it.

4. Force Push to a Remote Branch (If Necessary)

If the remote branch has conflicting changes, you might need to force push. Use this cautiously to avoid overwriting others' work:

git push --force

Example:

git push --force

5. Verify the Push

After pushing, you can verify the changes with:

git

Or check the remote branch in the repository UI (e.g., GitHub, GitLab).

Summary

  • Use git push <remote-name> <local-branch>:<remote-branch> to push to a different remote branch.

  • Git will create the remote branch if it doesn’t exist.

  • Use --force with caution to overwrite changes on the remote.

This flexibility helps you manage your Git workflow effectively and collaborate with your team.

A single code search interface for all your repos

Search across all your repos (GitHub, GitLab, BitBucket, etc) using a single open-source, blazingly fast, and feature-rich interface.

A single code search interface for all your repos

Search across all your repos (GitHub, GitLab, BitBucket, etc) using a single open-source, blazingly fast, and feature-rich interface.