When to use git branch --track (meaning to run "look upstream")?

Until recently, I did not know about the --track switch for git branch . I read the documentation and tried this command, but it makes no sense to me.

--track

When creating a new branch, configure the branch.<name>.remote and branch.<name>.merge to mark the starting point of the branch as "upstream" from the new branch. This configuration will tell git to show the relationship between the two branches in git status and git branch -v . In addition, it directs git pull with no arguments to pull from the upstream when a new branch is checked.

This behavior is the default when the starting point is remote branch tracking. Set the branch.autoSetupMerge configuration variable to false if you want git checkout and git branch always behave as if --no-track. Set this parameter to always if you want it to be when the starting point is a local or remote branch.

I see that people relate to this switch when they want to make a branch branch upstream

What does it mean? Is it me or this switch description confusing. When I use the term upstream , I refer to another remote repo (fork), which I can make changes to.

What happens when I start tracking the remote branch? How does this manifest locally?

+5
source share
2 answers

A fixed branch of a branch of a branch or a remote branch being tracked is just a branch with which you will interact by default when using the git pull and git push commands.

When you pull a branch into yours, you can do it explicitly:

 git pull origin the_branch 

It will select the remote origin , then origin/the_branch into your current branch.

If you always use the same branch for pulling, specifying the branch upstream, you can simply run git pull :

 git branch --set-upstream-to origin/the_branch git pull 

By default, when you start a new branch from a remote one, git will add it as a branch up:

 git checkout -b origin/the_branch # Is equivalent to git branch --track the_branch origin/the_branch git checkout the_branch 

When pressed, it is almost the same.
The push.default configuration will define the default branch to be clicked using git push without parameters.

With an upstream value, it will simply enter the upstream branch.
With a default value of simple it will do the same , but only if the branch names of the local and upstream are the same .
I will let you see the document to check other configuration options.


You can see the current branch branches of all branches with the -vv switch:

 $ git branch -vv * my_branch 33f2d4c [origin/mybranch] a useful commit master 3ed8e99 [origin/master] Merge the_branch dbbb8c0 [origin/the_branch] commit on the branch 

The inbound branch of the branch can also be transferred using the @{upstream} link:

 $ git rev-parse --symbolic-full-name --abbrev-ref @{upstream} origin/the_branch 

A push branch as equivalent to @{push} (it will be the same as @{upstream} in 99% of cases):

 $ git rev-parse --symbolic-full-name --abbrev-ref @{push} origin/the_branch 

The difference between @{upstream} and @{push} applies to cases where you use a triangular workflow: you extract read-only from the upstream project (usually this is a remote upstream call) and click on the recordable repository.
This refers to the forking workflow used on GitHub.
I made a (french) blog post about this, here is the auto-translate version .

+5
source

You set up a local branch to track the remote when you want to bind what you do locally to what happens remotely.

That is, git will know where to look for changes when you do git pull or git fetch . If someone else pushed some commits to the remote branch and you make git status , he will tell you what you earned at the remote control. Or, if you made some commits on your local computer, it will tell you that you made some actions before the remote.

Example output git status :

 On branch develop Your branch is behind 'origin/develop' by 19 commits, and can be fast-forwarded. (use "git pull" to update your local branch) 

As you can see, this is useful when working with a large number of people in one branch, since you can always be aware of what they are doing and keep your work up to date.

0
source

Source: https://habr.com/ru/post/1269938/


All Articles