Easy-to-follow video tutorials help you learn software, creative, and business skills.Become a member
Back in the chapter on branches, we talked about how you could have one local branch that stays rather closely in sync with the progress of another branch, and this process is called tracking. And as an example, I showed you how to text_edits could track master. The idea here is that we want to keep the two pretty closely in sync. So we're regularly going to pull updates from master and merge them into text edits. That way it never gets too far out of touch and even though it happens less frequently, whenever we are done with our work in text_edits, and we are ready to merge it back in, master is probably the branch that we want to merge it back into.
Now tracking is really common with remote branches, and it works in a similar way, plus with a little bit of configuration, we get to save ourselves some typing by letting the master branch know what remote branch should it be using when it's doing its fetch, and when it's doing it's push. We won't have to specify it each and every time. It will have a default setting by knowing its tracking branch. Now when we did our first Git push, we did it with the -u option, and I told you at that time, that that was about creating the tracking branch, and then we'd talk about it later. Well, that time is now.
If we don't do git push with the -u option, it does not track any remote branch. All it does is push our code up there, and that's it. It doesn't keep any kind of reference that this is the branch there we're going to be working with in the future. The -u option says push it up there, and also make a note of the fact because we're going to be coming back and working with this branch frequently. When we did a git clone to create our lynda_ version, it does track the remote branch, and we can see that tracking here, git/config. You can see that our branch master is set up to track the refs/heads/master that's on origin.
That's what it's going to track. That's how you know that it's a tracking branch. By contrast, let's switch back over. We'll go backwards a directory and then let's go into our explore_california folder, we can see the same thing is true here. If we do cat .git/config, but let's create one that doesn't track. Let's create one that doesn't have the -u option to see the difference. Let's create a branch off of master that we can then use. Let's do git branch, and let's call this branch non _tracking, that will make it clear what's up.
And then now, we know how to push that to origin, and we're going to push non_tracking. Wait one minute, it wants my credentials, okay, pushes it up there, and now let's take a look again at that git/config, and you will see that it does not have a listing here, right, push it up there but it said, all right! Here you go remote repository, I am done with you. There is not sort of connection in the future between our non_tracking branch and what's going on at GitHub.
The -u option or cloning it, created that tracking branch. Now I'm going to leave this non_tracking branch but I just want to let you know that if you ever end up with a branch that's not tracking, and you want to make it tracking, then you have three choices. Either you can come in and add these, so they are similar, or you can use the git config commands. It's git config branch and then whatever the branch name is. In this case, it would be non_tracking remote and then the name of the remote, origin, and then you would do the same thing for the option for merge refs/heads/master.
So that would give you those same configurations in the dot config file, or if you're using Git 1.7, which most of you should be unless you got an old version of Git for some reason, you could use git branch --set-upstream, which is also what the u option is, that -u that was used, when we did a push. That's the shortcut for set-upstream, and then we can say non_tracking origin, non-tracking, and that will tell it what its up-stream server is.
It will tell it what the remote is that it ought to track. So again, I'm going to do any of those. I am just going to leave it so that our non_tracking branch stays non_tracking, so that if we ever want to see the difference, we can switch over there and try it out.
Get unlimited access to all courses for just $25/month.Become a member