![]() (b) if there are 4 commits on the remote upstream branch which you haven't pulled yet, then your local branch is "4 commits behind" its upstream branch. # Your branch and 'origin/development' have diverged, Git tells you about this right in the output for "git status": $ git status This information helps tremendously in staying up-to-date. Inspecting tracking branches configuration.Why are upstream branches so useful in Git?.Set tracking branches for existing local branches.Set tracking branches for new local branches.Set upstream branch for an existing remote branch.# and have 2 and 3 different commits each, respectively. The easiest way to set the upstream branch is to use the “ git push” command with the “-u” option for upstream branch. $ git push -u Īlternatively, you can use the “ –set-upstream” option that is equivalent to the “-u” option. $ git push -set-upstream Īs an example, let’s say that you created a branch named “ branch” using the checkout 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. We can set the upstream branch using the “git push” command. $ git push -u origin branchīranch 'branch' set up to track remote branch 'branch' from 'origin'. * branch 808b598 Initial commit master 808b598 Initial commit Let’s have a look at the tracking branches again with the branch command. We have successfully set the upstream branch for our newly created branch. ![]() Set upstream branch using an aliasĪnother way to set the upstream branch is to define an alias for your “git push” command. ![]() In fact, pushing to HEAD is equivalent to pushing to a remote branch having the same name as your current branch. 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. ![]() When you are done adding and committing fiels to your repository, set the upstream branch using your newly defined alias. $ git pushdīranch 'branch' set up to track remote branch 'branch' from 'origin'. 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. Let’s create a new branch and use our alias in order to push our code and create the upstream branch easily. Set upstream branch for an existing remote branch $ git checkout -b branch2īranch 'branch2' set up to track remote branch 'branch2' from 'origin'. 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. Let’s say for example that you pulled the “dev” branch located on the “origin” remote.Īs a consequence, the tracking branch is named “origin/dev”. Set tracking branches for new local branches In order to switch to the local “dev” branch, and to set the “origin/dev” as the tracking branch (or upstream branch), use the “–track” option. $ git checkout -track origin/devīranch 'dev' set up to track remote branch 'dev' from 'origin'. To verify that you linked dev to the tracking branch “origin/dev” (which upstream branch is the remote dev), use the “ git branch” command.
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |