Situational guide to Git for new engineers - git pull

Situation: You need to get better understanding of what is the relationship between local and remote branches.

Clarifications

I had a very good set of questions from Victoria Park, @VicDevPiano on Twitter, regarding workings of local and remote branches and how they relate to each other and what happens during git pull operation. So here are some clarifications.

Question: Does it matter which branch you are on when you do git pull?

Answer: Yes it does. Whichever branch you are currently on will be pulled from remote, if it exists on remote. A branch that was never pushed from local will not exist on remote.

Question: Is git pull always from remote main if you don’t give arguments?

Answer: By default git pull will always be from remote for whichever branch you are on. It is possible to change that setting if needed.

Question: What is the relationship between main and other local branches, are they like separate folders that are not linked in any way?

Answer: They are like folders and they are independent of each other. But there is a relationship in a way that if you ever want to merge your ticket1Branch into the main branch, it ideally needs to contain recent changes from main branch to avoid conflicts. A large deviation from main branch often leads to conflicts.

If you never want to merge your local branch into main. You are essentially creating a fork which can live on it’s own.

What is the point?

What is the point of making branches? It allows to create a stable version of a moment in time for a repository for engineer to work on. As you work on branch it is up to you to update it with recent changes in main branch in remote if you need to.

If you are working on isolated file that nobody changes, you don't need to worry about merge conflicts. On the other hand if you have working in codebase that other members of your team actively changing you should keep your local branch more inline with recent changes to avoid large merge conflicts.

Note: A popular approach is to work on a main branch and stash changes when you want to get most recent updates to main and create new branch only when you are ready to push. This can still lead to conflicts though because when you apply the stash you are performing a local merge. There is no real advantage there.

There is a better way though and it’s to use git worktree. It’s a more advanced topic and there is a post about it coming soon.

Details

When existing repository gets cloned using command:

git clone https://github.com/myAccount/myAwesomeProject.git

What does command do

This command will create a local copy of the repository with a link to original remote version. Both local and remote versions have a git history of all the commits and merges and branches created and pushed.

But this history quickly becomes different. In fact the moment you create a new local branch for your task it will create a difference between local and remote.

git checkout -b ticket1Branch

Until you push that branch to remote repository this branch only exists on local.

Not every branch you create locally needs to exist on remote. But if you merge that branch into your local main branch then your local git history will change and once you push it to remote. They will again match each other.

And this is why conflicts can exist. They are created due to changes to same files by different branches. Even if each branch started at the same time, the moment one of them is merged, the second branch becomes out of date/out of sync.

And that is why it’s important to get latest version of main branch to your local and update your working branches with it. Especially if you are working branch has been around for a longer period of time.

Get latest version of the branch from remote command:

git pull

What does command do

Downloads latest version of the remote branch you are currently on (and only that branch). It performs git fetch under the hood and then it performs a fast forward merge which syncs your remote and local.

Fast Forward Merge just means that local branch has old history of the remote branch and there are no local changes to worry about so there will be no conflict.

Very useful to run git pull before creating new branch as it will ensure you have the most current version of your main branch.