i tried to show the difference between git fetch and git pull

i tried to show the difference between git fetch and git pull

Clock Icon2022.11.16

この記事は公開されてから1年以上経過しています。情報が古い可能性がありますので、ご注意ください。

Introduction

Git Fetch and Git pull are one of the most commonly used commands, but they can sometimes be misunderstood.

The Git Fetch command alerts the local repository that there are changes in the remote repository without invoking the changes in the local repository. So basically it's saying something is happening in the remote repo but I can't bring it here to the local repository for you automatically.

On the other hand, Git Pull invokes a copy of the changes made in the remote repository into the local repository.

Let's dive in to show how they are used differently.

Git Fetch

Firstly, I will create a simple read.py file with a line "first line of code" on my local machine and push it to the remote repository.

git init

git add file-name

git commit -m "commit message"

git remote add origin "your remote repo url"

git branch -M main

git push -u origin main

Now we have the file in the remote repository

Both the local and remote repositories are in sync.

Update the read.py file in the remote repository by adding a "second line of code" in the file.

The read.py file is updated remotely but what happens in the local repository?

The local repository has 1 commit but the remote repository already has 2 commits.

Now let's do git fetch and see what happens.

After using git fetch we can see from 029377a which is the initial of the second commit

To merge these changes into our local repository, we need to use git merge origin/branch-name

With these, the remote repository files are present in the local repository.

Git Pull

Let's add another line of code in our read.py file in the remote repository.

There are 3 commits in the remote repo, and 2 commits in the local repository50b4139

With git pull, let's invoke changes in the local repository

git pull origin branch-name

With the git pull command, the file was fetched and merged to the local repo from the remote repo, and both repositories have the current files.

Conclusion

The can work asgit pullgit fetch + git merge

 

Share this article

facebook logohatena logotwitter logo

© Classmethod, Inc. All rights reserved.