All About Git Branches… And Git pull vs. fetch
Last Updated on July 25, 2023 by Editorial Team
Author(s): Muttineni Sai Rohith
Originally published on Towards AI.
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
Git is a version control system that lets us track the changes we make to our files over time.
muttinenisairohith.medium.com
So far, we have created the main branch, pushed our first commit to the branch, and then 2nd commit. Our repo looks like this β
To clone a specific branch, use this command in git bash
git clone -b <branchname> <remote-repo-url>
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 β
Git Rebase, Merge, and Stash
Git rebase is an advanced feature in git which helps us when we are dealing with multiple branches.
muttinenisairohith.medium.com
Happy Learningβ¦ Stay tunedβ¦
Join thousands of data leaders on the AI newsletter. Join over 80,000 subscribers and keep up to date with the latest developments 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