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Β β
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Β –
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
or
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Β β
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