Master LLMs with our FREE course in collaboration with Activeloop & Intel Disruptor Initiative. Join now!


All About Git Branches… And Git pull vs fetch

All About Git Branches… And Git pull vs fetch

Last Updated on December 25, 2022 by Editorial Team

Author(s): Muttineni Sai Rohith

Originally published on Towards AI the World’s Leading AI and Technology News and Media Company. If you are building an AI-related product or service, we invite you to consider becoming an AI sponsor. At Towards AI, we help scale AI and technology startups. Let us help you unleash your technology to the masses.

All About Git Branches… And Git pull vs. fetch

With Branches in Git, we can create a copy of the file we would like to work on without messing up with the original file. Later we can merge them to the original copy.

This is the second addition to the series of articles about Git — Complete Git Tutorial for Beginners with Examples. The link is available below —

Complete Git Tutorial for Beginners with Examples

So far, we have created the main branch, pushed our first commit to the branch, and then 2nd commit. Our repo looks like this —

Branch Flow

Now let's create a test branch and push our new changes to that branch. We can create a new branch named “test” using the below command –

git checkout -b test

In the above command, checkout makes us switch to a branch, and -b helps us in creating the branch.

Now Let’s add some changes, then add them to our local repo commit and push them to the remote repo with the test branch.

def add(a, b):
return a+b+10

We can add, commit and push changes to the remote repo using the following commands —

git add .
git commit -m “Updating the add function with offset 10”
git push origin test

Instead of git add . and git commit -m “message” we can use the following command —

git commit -am "Updating the add function with offset 10"
git push origin test

Now our repo looks like this –

Branch Flow

Now Let’s make another change by changing the offset to 20 and commit and push to the test branch again.

def add(a, b):
return a+b+20

Let’s add, commit and push into the remote Repo

git commit -a -m "changing the offset to 20"
git push origin test

Now as we have some modified code in the test branch, I feel my code is ready, and let’s merge our code with the main branch.

This can be done using this command –

git checkout main
git merge test

Here we used checkout to switch the branch, we are not using -b as it is used to create new. And then merge the code using the git merge command.

Now Below is what our repo looks like-

Now that we have made many changes to the main branch, let's see the commit history using the command –

git log

We can press “q” to come out of the log.

To make our test branch track the main branch, we can use the below command –

git branch - set-upstream-to=origin/main test

Pulling a git repo branch

Now let’s make some changes in the main branch and commit this.

def add(a, b):
return a+b
def sub(a, b):
return a-b
def mul(a, b):
return a * b
a = int(input())
b = int(input())
print(add(a, b))
print(sub(a, b))
print(mul(a, b))

Now let's commit this and push this to the main branch.

git add .
git commit -m "adding multiplication code"
git push origin main

Now in the main branch, we have made our changes.

Now let’s suppose we need to add another change in the main branch, but as it is in production we have to make sure the testing doesn’t cause any problems. So let’s implement that change in the test branch and then create a pull request to merge that in the main branch.

But before that, let’s check the code in the test branch. So for that, let’s switch back to the test branch using the command –

git checkout test

Here we can see the code is not updated. And our test branch Lags one commit behind the main branch.

To update the code from the main branch we need to give this command –

git pull origin main


git pull

Pull command helps in pulling the latest changes from our own branch or from the remote branch.

Whenever we are working in the branches, it is a good practice to pull our changes regularly.

Git fetch vs. Git pull

Git pull will check for any lagging code commits, and if our local repo lags behind any commit, then using Git pull, we can retrieve the updated changes and get them merged. At the same time, git fetch will only retrieve the updated changes in the lagging commits. And then, we should use git merge to merge them in our repo.

As we can see, after a commit is made, there is no update in the local repo using git status, but when we used git fetch, we got info that our local repo is lagging by 1 commit behind the main repo. So then, we should use the git merge command to merge the changes.

But all this can be implemented using one command –

git pull

It will fetch the changes and then merge the changes in our local repo from the remote/origin repo.

git pull = git fetch + git merge

So that’s all about git branches in this article… But there is more to cover on Git rebase, Stash, revert and reset. Those will be covered in the below article —

Git Rebase, Merge, and Stash

Happy Learning… Stay tuned…

All About Git Branches… And Git pull vs fetch was originally published in Towards AI on Medium, where people are continuing the conversation by highlighting and responding to this story.

Join thousands of data leaders on the AI newsletter. It’s free, we don’t spam, and we never share your email address. Keep up to date with the latest work in AI. From research to projects and ideas. If you are building an AI startup, an AI-related product, or a service, we invite you to consider becoming a sponsor.

Published via Towards AI

Feedback ↓