Name: Towards AI Legal Name: Towards AI, Inc. Description: Towards AI is the world's leading artificial intelligence (AI) and technology publication. Read by thought-leaders and decision-makers around the world. Phone Number: +1-650-246-9381 Email: [email protected]
228 Park Avenue South New York, NY 10003 United States
Website: Publisher: https://towardsai.net/#publisher Diversity Policy: https://towardsai.net/about Ethics Policy: https://towardsai.net/about Masthead: https://towardsai.net/about
Name: Towards AI Legal Name: Towards AI, Inc. Description: Towards AI is the world's leading artificial intelligence (AI) and technology publication. Founders: Roberto Iriondo, , Job Title: Co-founder and Advisor Works for: Towards AI, Inc. Follow Roberto: X, LinkedIn, GitHub, Google Scholar, Towards AI Profile, Medium, ML@CMU, FreeCodeCamp, Crunchbase, Bloomberg, Roberto Iriondo, Generative AI Lab, Generative AI Lab Denis Piffaretti, Job Title: Co-founder Works for: Towards AI, Inc. Louie Peters, Job Title: Co-founder Works for: Towards AI, Inc. Louis-François Bouchard, Job Title: Co-founder Works for: Towards AI, Inc. Cover:
Towards AI Cover
Logo:
Towards AI Logo
Areas Served: Worldwide Alternate Name: Towards AI, Inc. Alternate Name: Towards AI Co. Alternate Name: towards ai Alternate Name: towardsai Alternate Name: towards.ai Alternate Name: tai Alternate Name: toward ai Alternate Name: toward.ai Alternate Name: Towards AI, Inc. Alternate Name: towardsai.net Alternate Name: pub.towardsai.net
5 stars – based on 497 reviews

Frequently Used, Contextual References

TODO: Remember to copy unique IDs whenever it needs used. i.e., URL: 304b2e42315e

Resources

Unlock the full potential of AI with Building LLMs for Productionβ€”our 470+ page guide to mastering LLMs with practical projects and expert insights!

Publication

All About Git Branches… And Git pull vs fetch
Latest

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
Output

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

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 ↓