How to Git Push to Another Branch With a Different Name
-
the
git push
Command and Its Rich Set of Options -
a Neat Shortcut in
git push
to Set therefspecs
Parameter Once and Use Many Times - When Would We Like to Git Push to Another Branch
-
Git
Refspecs
- Resources
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.
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
We now see the feature_branch
has been pushed into a new branch with a different name on our 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.
We see this new upstream branch in our remote repository also.
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
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:
-
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.
-
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 itsrefspecs
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.
-
The
refs/heads
directory stores the references to objects in your local repository. -
The
refs/remotes
have references to your remote repository Git objects.
Resources
- https://www.freecodecamp.org/news/git-push-to-remote-branch-how-to-push-a-local-branch-to-origin/
- https://github.com/jiffyclub/blog-posts/commit/4bf63cdafc9a3eb9602646ced972913ef73386bd
- https://davidwalsh.name/git-push-different-name
- https://stackoverflow.com/questions/36139275/git-pushing-to-remote-branch
- https://git-scm.com/docs/git-push