Enroll in Selenium Training

In the last tutorial, we learned about the Git push. Git push command pushes the changes made by the user on the local repository to the remote repository. Pushing the changes to the remote repository enables the whole team or the organization associated with the project to see those changes and work accordingly. But, this was a story from the end of the user making the changes. Let's switch to another side and see the story of the user who sees the changes. When a user makes some changes on the project (push), the process of incorporating those changes into their local systems to be in parallel with the stable code is called pulling. This is on what this tutorial is based:

  • The Importance of Git Fetch Command.
  • The Importance of Git Merge.
  • Git Pull and How to use it in Git.

Along with it, it will also contain a demo of using the commands in Git Bash.

What is Git Fetch in Git?

Git fetch commands helps the user download commits, refs and files from the remote repository to the local repository. These commits are done by the teammates and people associated with the project in the organization or maybe the user itself directly on the remote repository. In other words, executing git fetch command will help you see the updates from all the teammates on the repository. You might be thinking, what if you don't want to keep the changes? Well, Git fetch does not hurt your working repository at all. Git fetch is a great way to stand at a place from where you can see the changes and decide if you want to keep them or discard them. Keeping the changes is called merging in Git and it is an explicit operation. So until you press git merge, you won't be adding anything. The next section is dedicated to merging in Git.

Git Fetch and Git Pull

What is Git Merge in Git?

Git merge command is the positive conclusion of your decision to incorporate the changes you saw using Git fetch command. Let me straighten it out. Once the user likes the changes made by themself on the remote repository or made by their teammates, they can merge these changes to the repository (remote and local). These changes which could have been from multiple branches are merged into a single branch. Although it is the responsibility of the user who is merging the changes to ensure their head pointer points to the same branch in which they want to merge the changes. Not adhering to this will create unnecessary inconsistencies in the repository. Let's perform some practical on git fetch and merge commands.

Using Git Fetch and Git Merge in Git

Follow these steps to perform fetching and merging operations in Git:

  1. Open your GitHub account and navigate to ToolsQA repository.

  2. Open the Readme.md file.

readme_md

  1. Press the edit icon to edit the file.

edit readme

  1. Edit the file description in a meaningful way.

readme_edit_new

  1. Scroll down and commit the changes using meaningful description and press commit changes.

commit_readme_file

  1. The changes will be reflected in the README.md file.

readme_change_reflected

Now that we have some changes to the remote repository, we must fetch those in our local working copy of the repository.

  1. Open Git bash and navigate to your working directory.

  2. Check for a clean working repository (no committed changes).

  3. Execute the following command in Git Bash:

git fetch

Git Fetch Command

The last two lines are as follows:

  • https://<repo_url> : The URL of the repository.
  • e7b37f6..47b2bf6 : The first hash is the hash of last merged commit into the local repository while 47b2bf6 is the new commit/change hash code.

So it is evident that we have one commit on the remote repo that has not yet been merged into the local repository. So let's do it.

  1. Execute the following command to merge the changes into your local working repository.

git merge

git merge command

Note: If you have performed the above functions on your own repository, you have to push the changes and tell the remote server that you have pulled the changes by using git push command. Since this is a forked repository, we are not required to do so here.

Git, being a version control system, ensures that the histories of commits of these separate branches remain separate even after the merge operation has been done. Git merge is performed after the changes have been fetched i.e. Git fetch has already been performed. But, there is a way to bypass this two-step process and convert it to a single step. This is called pulling in Git and is performed using the git pull command.

What is Git Pull Command in Git?

A git pull command is the combined command of git fetch and git merge. Executing a git pull command will merge the changes without notifying or displaying what changes are being merged. This is as risky as it sounds. Risky in a way that git pull will merge even those changes that are not required or those which you do not want to merge. It is ironic that in general terms, everyone uses the term "pull my changes", whereas, it is more like, "fetch and merge my changes". Git pull is not a recommended way of merging the changes since it raises "merge-conflict" in Git.

A merge-conflict arises when two users are trying to modify the same line on the same file. For example, one user tries to delete the line while another modified it.

Git Fetch and Git Pull

Git fetch is a safer version of git pull and should be used instead. Although if the user is confident enough, it is recommended they use git pull command only on a clean working directory (no committed changes).

Using Git pull command is no different than using Git merge command. Just keep in mind that git pull is the combined command for git fetch and git merge. This means we don't need to execute git fetch and git merge and the changes will be incorporated directly.

git pull command

Notice the 2 sections I have marked in the image above.

The first section has the same output as the git fetch command in the previous section whereas the second section has the same output as the git merge command. This basically proves git pull is an amalgam of git fetch and git merge command and should be used carefully.

This brings us to the end of this tutorial. Till now we have covered all the major commands and their usage in Git. Everything constitutes of these commands in Git. I hope you are practicing them regularly. In the next section, we will talk about branches in Git and visualize them to understand why they are the most important aspect of Git.

Branch in Git
Branch in Git
Next Article
Harish Rajora
I am a computer science engineer. I love to keep growing as the technological world grows. I feel there is no powerful tool than a computer to change the world in any way. Apart from my field of study, I like reading books a lot and developing new stuff.
Reviewers
Lakshay Sharma's Photo
Lakshay Sharma

Similar Articles

Feedback