Enroll in Selenium Training

In the last tutorial, we learned about the Git push command. Git push command pushes the changes made by the users on their local repository to the remote repository. Additionally, pushing the changes to the remote repository enables the whole team to collaborate and share their work. But, this was one side of the story, where users have pushed their local change to the remote repository. On the other hand, all other team members need to sync these changes from the remote repository to their local repository. Therefore, for this, the team members need to fetch the changes and merge them into their local repository. Focussing on the same, in this tutorial, we will cover Git Fetch and Git Merge, which will brief how users can sync changes from the remote repository to their local repository:

  • Git Fetch
    • What is Git Fetch Command?
    • How to use Git Fetch Command?
    • Options in Git Fetch Command
  • Git Merge
    • What is Git Merge Command?
    • How to use Git Merge Command?
    • Fast-Forward Merge In Git
    • Options In Git Merge

What is Git Fetch Command?

Git fetch command helps the user download commits, refs, and files from the remote repository to the local repository.  In other words, executing this command will help you see all the updates on the remote repository. You might be thinking, what if you don't want to keep the changes? Well, this command does not hurt your working repository at all.

git fetch only

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.

How To Use Git Fetch Command?

Before using the command, let's make some changes to our remote repository so that we can fetch them through the local repository.

Follow these steps in your GitHub account:

  1. Firstly, open your GitHub account and navigate to the ToolsQA repository.

  2. Secondly, open the Readme.md file.

readme md

  1. Thirdly, press the edit icon to edit the file.

edit readme

  1. Fourthly, edit the file description in a meaningful way.

readme edit new

  1. After that, scroll down and commit the changes using meaningful description and press commit changes.

commit readme file

  1. The changes will reflect 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. Finally, execute the command in Git Bash: git fetch

git fetch output

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 from the remote repository.

The user can also check the commits and recent activity through the git log command. You can refer to what is Git Log command and Git Diff command in Git.

If your screen shows a similar output, as shown in the image above, you have successfully fetched the changes from your remote repository.

Options in Git Fetch Command

Like any other command in Git, Git fetch command also contains some of the options for the quick and efficient use of the command. Let's discuss them below:

All Option

The 'all option' fetches all the remote refs, files, etc.

git fetch --all

git fetch all

Dry Run Option

The dry run option will show the user how the command will execute without making any changes.

git fetch --dry-run

git fetch dry run

What is Git Merge Command?

Git merge command is the positive conclusion of your decision to incorporate the changes you saw using the Git fetch command. Let me straighten it out. Once the user is ready to accept the changes from the remote repository, they can merge these changes to the local repository. As the name suggests, you are confirming to "merge" these changes.

The image below can help describe the meaning of Git Merge straightforwardly.

git fetch

The extraction of the changes done on the remote happens via fetch command, and then if the user approves, they are merged with the local repository.

How to use Git Merge Command?

To merge the changes that we fetched in the above section, execute the following command:

git merge

git merge

If you see the same output, you have successfully merged the changes into your local repository. In the above image, the third line shows "Fast-forward" written. It is because this is a fast-forward merge done by git. Let's see what it is.

Fast-Forward Merge In Git

A fast-forward merge in Git means there is a linear path from the branch that diverts from the branch to which you are merging. A linear path means that there have been no commits to the master branch since the feature branch diverted to the point it is being merged.


The above image shows that the branch got diverted, made three commits, and in that time, there were no commits in the master branch (the green dots). After the three commits, I will merge the feature branch into the master branch, which will result in a fast forward commit.

Options In Git Merge

--no-ff Option in Git Merge

The --no-ff option prevents git from merging the changes in a fast-forward way.

git merge --no-ff

1 no fast forward

--no-commit Option In Git Merge

The no-commit option will not auto-commit the git merge and will request the user's intervention for the completion.

git merge --no-commit

1 no commit git merge

There are many more options in the git merge that you can explore and try out on the local repository. Git, being a version control system, ensures that the histories of commits of these separate branches remain separate even after the merge operation finishes. Git merge happens after the changes fetch, i.e., the performance of the Git fetch already takes place. But, there is a way to bypass this two-step process and convert it to a single step. It is known as pulling in Git, and the Git pull command performs it.

In the next tutorials, we will learn how to use Git pull command to pull the changes from the remote and will use these concepts while merging two branches into a single branch.

Common Questions On Git Fetch And Git Merge

Is git fetch and git fetch -all the same?

Git Fetch and Git Fetch -All are similar in operation. The difference comes when the user uses git fetch <branch_name> to fetch the changes from a particular branch.

What differentiates git fetch and git pull?

Git fetch command and git pull command are both different in operation. Git fetch fetches the changes while git pull merges them after fetching. So in a way, git fetch is a part of git pull as it first fetches the changes and then performs git merge.

Can I undo the changes done by the git merge?

Yes, Git merge changes can reverse by the following actions.

  • Identify the commit to which you want to go back through git reflog command.
  • Perform hard reset along with the commit hash by git reset --hard <hash>.

How to merge branches whose histories are unrelated?

More often, an error pops up, which says, "refusing to merge unrelated histories" while merging through git rebase command. It was previously allowed as a default option, but its removal happened since Git 2.9. Now, the user has to command Git to merge the branches with unrelated histories explicitly. Therefore, the option to be used with rebase is --allow-unrelated-histories.

Verify Committed Changes on GitHub
Verify Committed Changes on GitHub
Previous 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.
Lakshay Sharma's Photo
Lakshay Sharma

Similar Articles