How to work with Git and remotes (Git)

Reginald LynchAugust 8th 2020, 5:00

At some point, it is very likely that you have cloned somebody's repository. This means that you have an associated remote. The remote is usually called origin because it is where the source originated from.

While working with Git and remotes, you will get some benefits from Git. Follow these steps:

Step 1 : We will start by checking out a local branch that tracks a remote branch:

git checkout -b remoteBugFix --track origin/stable-3.2 

The previous command creates and checks out the remoteBugFix branch that will track the origin/stable-3.2 branch. Therefore, for instance, executing git status will automatically show how different your branch is from origin/stable-3.2, and it will also show whether your branch's HEAD can be fast forwarded to the HEAD of the remote branch or not.

Step 2 : To provide an example of how the previous step works, we need to do some manual work that will simulate this situation. First, we find a commit:

The command will list the last 10 commits on the stable-3.2 branch from the remote origin. The --oneline option will show the abbreviated commit hash and the commit subject.

Step 3 : We will be using the following commit:

git reset --hard 2e0d178

Step 4 : This will reset the remoteBugFix branch to the 2e0d178 commit hash. We are now ready to continue using the free benefits of Git when we have a remote tracking branch.

Step 5 : we will try a few commands that assist you when you have a remote tracking branch. Start by executing git status:

git status

As you can see from the message, you can use git pull to update your local branch. The message says it can be fast-forwarded. It simply means that Git can advance the HEAD without merging

Step 6 : The git pull command is just a git fetch command and then a git merge command with the remote tracking branch.

git merge origin/stable-3.2 

From the output, you can see it is a fast-forward merge, as Git predicted in the output of git status.

You can also add a remote to an existing branch, which is very handy when you realize that you actually wanted a remote tracking branch but forgot to add the tracking information while creating the branch:

Step 7 : Start by creating a local branch at the 2e0d17 commit:

git checkout -b remoteBugFix2 2e0d17 

Step 8 : The remoteBugFix2 branch is just a local branch at the moment with no tracking information; to set the tracking branch, we need to use --set-upstream-to or -u as a flag to the git branch command:

git branch --set-upstream-to origin/stable-3.2 

Step 9 : As you can see from the Git output, we are now tracking the stable-3.2 branch from the origin:

git status

Step 10 : You can see from the Git output that you are nine commits ahead, and you can use git pull to update the branch. Remember that a git pull command is just a git fetch command, followed by a git merge command with the upstream branch, which we also call the remote tracking branch:

git pull 

Step 11 : From the output, you can see that the branch has been fast forwarded to the f839d383e commit hash, which is equivalent to origin/stable-3.2. You can verify this with git log: