In some cases, you may choose to link your local branches to existing remote branches that you just pulled or cloned from the main repository. Set upstream branch for an existing remote branch
$ git checkout -b branch2īranch 'branch2' set up to track remote branch 'branch2' from 'origin'.
Let’s create a new branch and use our alias in order to push our code and create the upstream branch easily. Using a bash aliasĪlternatively, you can use a bash alias if you don’t want to modify your existing git commands.ĭefine a new bash alias using the “ alias” command and define a name for it. $ git pushdīranch 'branch' set up to track remote branch 'branch' from 'origin'. When you are done adding and committing fiels to your repository, set the upstream branch using your newly defined alias.
In order to create a new git alias, use the “ git config” command and define a new alias named “pushd” $ git config -global alias.pushd "push -u origin HEAD" In order to avoid having to define the upstream everytime you create a new branch, define an alias for the command we just wrote.įor aliases, you have two choices, you can either create a git alias or a bash alias. In fact, pushing to HEAD is equivalent to pushing to a remote branch having the same name as your current branch. Set upstream branch using an aliasĪnother way to set the upstream branch is to define an alias for your “git push” command. We have successfully set the upstream branch for our newly created branch. * branch 808b598 Initial commit master 808b598 Initial commit Let’s have a look at the tracking branches again with the branch command. $ git push -u origin branchīranch 'branch' set up to track remote branch 'branch' from 'origin'. We can set the upstream branch using the “git push” command. Master 808b598 Initial commitĪs you can see, compared to master, the branch “branch” has no tracking branches yet (and no upstream branches as a consequence) You can check tracking branches by running the “ git branch” command with the “ -vv” option. $ git push -set-upstream Īs an example, let’s say that you created a branch named “ branch” using the checkout command. $ git push -u Īlternatively, you can use the “ –set-upstream” option that is equivalent to the “-u” option. The easiest way to set the upstream branch is to use the “ git push” command with the “-u” option for upstream branch.
Set upstream branch for an existing remote branch.