How to Use Git Fetch?
Step 1: Let us create a file called demo.txt with “Hello Geeks” content inside it initialize the directory to a git repository and push the changes to a remote repository.
- git init
- git add <Filename>
- git commit -m <Commit Message>
- git remote add origin <Link to your remote repository>
- git push origin <branch name>
Now, we have my demo.txt in the remote repository.
The local and the remote repositories are now in sync and have the same content at both places. Let’s now update our demo.txt in the remote repository.
Step 2: We will update our demo.txt remotely.
Now since we have updated our demo.txt remotely, let’s bring the changes to our local repository. Our local repository has only 1 commit while the remote repository now has 2 commits (observe the second commit starting from 4c4fcb8).
Step 3: Let’s use the git fetch command to see in the local repository whether we have a change in the remote repository or not. Before that let’s use the git log command to see our previous commits.
We can see that after using git fetch we get the information that there is some commit done in the remote repository. (notice the 4c4fcb8 which is the initials of our 2nd commit in a remote repository).
Step 4: To merge these changes into our local repository, we need to use the git merge origin/<branch name> command.
Let us have a look at our commits in the local repository using the git log command.
And we got our remote repository commit in our local repository. This is how git fetch works. Let us now have a look at the git pull command.
Difference Between Git Fetch and Git Pull
Understanding the difference between git fetch
and git pull
is important for effective version control in Git. These commands are important for managing your repository and collaborating with team members. In this article, Let us look at Git Fetch and Git Pull separately with the help of an example.