git pushCommand and Its Rich Set of Options
a Neat Shortcut in
git pushto Set the
refspecsParameter Once and Use Many Times
- When Would We Like to Git Push to Another Branch
git push has a rich set of options to let you use the full power of Git. One of these is its
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.
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
Let us see how these parameters help us
git push to another branch.
git push [remote_repo] [refspecs]
[remote_repo] refers to the remote repository in our local system. Most times, this name is
[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 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
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
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
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.
git pushcommand with its
refspecsoptions saves the day for you in such cases.
Finally, before we sign off, let’s dig deeper into
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
In Git, we refer (pun intended) to these references as
refspecs are stored in special directories inside your repository.
refs/headsdirectory stores the references to objects in your local repository.
refs/remoteshave references to your remote repository Git objects.
- git add, git commit, and git push in One Command
- Commit and Push a Single File to the Remote
- Difference Between Git Push Origin and Git Push Origin Master
- Git Push Origin Head
- Git Push Hangs
- Make an Initial Push to a Remote Repository With Git