How to Git Push to Another Branch With a Different Name

Ankit Raj Goyal Feb 02, 2024
  1. the git push Command and Its Rich Set of Options
  2. a Neat Shortcut in git push to Set the refspecs Parameter Once and Use Many Times
  3. When Would We Like to Git Push to Another Branch
  4. Git Refspecs
  5. Resources
How to Git Push to Another Branch With a Different Name

The git push has a rich set of options to let you use the full power of Git. One of these is its source:destination refspecs parameters.

We use these to git push to a specific branch with a name of our choice. Toward the end, we will see a few use cases where we make immense gains in our workflow if we git push a new branch and not into a branch with the same name as our local.

the git push Command and Its Rich Set of Options

Git does not restrict us to a simple push into a remote with the bare git push command. Instead, it provides us with several powerful options to let us achieve our desired outcome.

Two of these are the [remote_repe] and [src:dst] refspecs parameters.

Let us see how these parameters help us git push to another branch.

git push [remote_repo] [refspecs]

The [remote_repo] refers to the remote repository in our local system. Most times, this name is origin.

The [refspecs] is the interesting parameter key to git push to a specific branch. We will look in detail at refspecs toward the end of this post.

It has the form: src:dst, where src refers to the local branch we want to push. The dst is the reference (or name) to the remote branch we want to push into.

It defaults to the same name as the src parameter, but we can choose to git push to a specific branch by explicitly providing the dst value.

We first set up a local repository and link it to a remote repository. Then, we also create a new branch on our local repository.

local repo with unpushed branch

remote without feature branch

Our remote repository does not have the feature_branch present on our local.

We now push the feature_branch to another branch with a new name.

git push origin feature_branch:teamX_featureY

git push to specific branch

We now see the feature_branch has been pushed into a new branch with a different name on our remote.

git push new branch to remote

Note that you will have to pass in these arguments each time you push; else, the dst parameter will default to the name of the branch on your local repository. However, Git provides us with a neat shortcut to save us the effort of typing these repeatedly.

a Neat Shortcut in git push to Set the refspecs Parameter Once and Use Many Times

In Git, a branch’s upstream name is the branch you always push it to. You can set this value with the command below.

git branch --set-upstream-to <remote_branch>

However, you can also run this command with git push by simply passing the -u flag.

git push -u origin local:different_remote

If you do this, your local branch gets pushed into the different_remote branch the next time you push. So you do not need to name it every time explicitly.

push with upstream flag

We see this new upstream branch in our remote repository also.

remote with new upstream branch

You must change the push.default value in your config file with the following command.

git config push.default upstream

If you now push second_feature without mentioning the dst parameter, Git automatically pushes it into a different_remote.

git push origin second_feature

git config edit remote pushdefault

When Would We Like to Git Push to Another Branch

A few use cases when we would want to git push a new branch are:

  1. Suppose you develop a cool feature or module, and you want to push it into more than one project that you work on. You could love even a simple generic feature like a Halloween-themed menu you designed and would want it in many of your apps.

    You would need to push it into each project with a different name.

  2. Sometimes, there is a mismatch between the naming schemes in the central (remote) repository and your local setup. This is especially true when you work on multiple projects involving large teams.

    The git push command with its refspecs options saves the day for you in such cases.

Finally, before we sign off, let’s dig deeper into refspecs.

Git Refspecs

Git internally stores references to all the objects in your repository. This makes it easy to quickly access the various Git objects without always using cryptic SHA hashes.

In Git, we refer (pun intended) to these references as refspecs.

These refspecs are stored in special directories inside your repository.

  1. The refs/heads directory stores the references to objects in your local repository.

    local refs heads

  2. The refs/remotes have references to your remote repository Git objects.

    remote refs remotes

Resources

  1. https://www.freecodecamp.org/news/git-push-to-remote-branch-how-to-push-a-local-branch-to-origin/
  2. https://github.com/jiffyclub/blog-posts/commit/4bf63cdafc9a3eb9602646ced972913ef73386bd
  3. https://davidwalsh.name/git-push-different-name
  4. https://stackoverflow.com/questions/36139275/git-pushing-to-remote-branch
  5. https://git-scm.com/docs/git-push

Related Article - Git Push