Useful tricks about git fetch and git pull
Tags: git, programming, showdev, tutorial
Published: 2020-05-07 13:06:41

---
title: Useful tricks about git fetch and git pull
published: true
date: 2020-05-05 09:00:36 UTC
tags: git, programming, showdev, tutorial
canonical_url: https://gitbetter.substack.com/p/how-to-use-git-fetch-and-git-pull
---
### *This post was originally posted in the newsletter [GitBetter](https://gitbetter.substack.com/). If you are interested in leveling up your game in Git, you can subscribe to it*

If you are using Git for a while, then you should be probably using **`git fetch`** and **`git pull`**. In this post, we will be covering how `git fetch` and `git pull` works and will be sharing some tricks that you can start using right away.

### How git fetch works
[![](https://cdn.substack.com/image/fetch/w_1456,c_limit,f_auto,q_auto:good,fl_lossy/https%3A%2F%2Fbucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2F0254f549-30fc-4062-8865-7ea3e3380aa2_700x394.gif)](https://cdn.substack.com/image/fetch/c_limit,f_auto,q_auto:good/https%3A%2F%2Fbucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2F0254f549-30fc-4062-8865-7ea3e3380aa2_700x394.gif)

`git fetch` downloads all the latest commits from all branches and also all the new branches from the remote and saves it in the local repo **but it won’t merge it to the local branch you are working on**. It means that `git fetch` will never change your working state. It’s harmless. You can fetch multiple times and it won’t change or update any content in your working branch.

Then, why it’s useful. `git fetch` helps you to see the updated content in the remote. Assume that, you are working in your local repo. In the meanwhile, there are changes in the remote repo. You have to know, the changes in the remote repo without much impact. This is where git fetch comes in.

**To say it in simple words, it fetches all the changes from the remote and stores it in your local but doesn’t merge those changes locally**. Simple.

```
git fetch
```
[![](https://cdn.substack.com/image/fetch/w_1456,c_limit,f_auto,q_auto:good/https%3A%2F%2Fbucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2F53d4d7a3-f478-4819-a494-ad7ffdca0cfa_810x92.png)](https://cdn.substack.com/image/fetch/c_limit,f_auto,q_auto:good/https%3A%2F%2Fbucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2F53d4d7a3-f478-4819-a494-ad7ffdca0cfa_810x92.png)

In the above image, you can see that two new branches are created `origin/develop` and `origin/master.` These branches will have the updated changes from the remote. But these changes are not affected to `master` and `develop` branches.

After fetching, you can view the changes made in the remote. For example, to view the latest changes made in the `master.`

```
git checkout origin/master
```
[![](https://cdn.substack.com/image/fetch/w_1456,c_limit,f_auto,q_auto:good/https%3A%2F%2Fbucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2F0b537cd5-7adf-499b-9c79-0a74c7c666db_807x239.png)](https://cdn.substack.com/image/fetch/c_limit,f_auto,q_auto:good/https%3A%2F%2Fbucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2F0b537cd5-7adf-499b-9c79-0a74c7c666db_807x239.png)

With the above command, you are basically checking out to the **local branch that git has created to store all the latest changes from the remote**.

To checkout to some other branch, you can replace the `master` with your branch name.

You can use the command `git log` to view the updated commits from the remote.

One of the main use-cases of `git fetch` is to see the changes before merging the current branch. So that you are always sure, what changes are already made and it will easy to resolve conflicts beforehand.

```
git diff develop origin/develop
```

The above command will show you the diff between the local changes and the remote changes of the branch master.

In other words, this will show the difference between these two branches so that all the changes from `origin/develop `are shown but changes from `develop `aren’t shown. So that you can know the new updates.

It doesn’t matter in which branch you are calling. You can call this command in any branch you like. But remember to `git fetch` in prior.

[![](https://cdn.substack.com/image/fetch/w_1456,c_limit,f_auto,q_auto:good/https%3A%2F%2Fbucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2Fe212161e-8399-4928-8288-601a44371039_808x156.png)](https://cdn.substack.com/image/fetch/c_limit,f_auto,q_auto:good/https%3A%2F%2Fbucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2Fe212161e-8399-4928-8288-601a44371039_808x156.png)

```
git diff develop origin/develop —stat
```

This command will just show only the files changed.

[![](https://cdn.substack.com/image/fetch/w_1456,c_limit,f_auto,q_auto:good/https%3A%2F%2Fbucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2F46ba2f46-24cf-4f7d-81ac-7b74be7522b0_804x82.png)](https://cdn.substack.com/image/fetch/c_limit,f_auto,q_auto:good/https%3A%2F%2Fbucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2F46ba2f46-24cf-4f7d-81ac-7b74be7522b0_804x82.png)

There is another helpful trick you can use.

```
git log develop..origin/develop
```
[![](https://cdn.substack.com/image/fetch/w_1456,c_limit,f_auto,q_auto:good/https%3A%2F%2Fbucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2F9a1237b5-c227-4eac-a07e-b17806adf7a6_808x123.png)](https://cdn.substack.com/image/fetch/c_limit,f_auto,q_auto:good/https%3A%2F%2Fbucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2F9a1237b5-c227-4eac-a07e-b17806adf7a6_808x123.png)

This command will help you show all the commits from `origin/develop` but that are not present in the `develop` branch. In this way, you can know that how many new commits are added to the remote `develop` branch that is not present in the local branch.

Try to run

```
git log origin/develop..develop
```

Now you are trying to see all commits from `develop` (local) but commits that are not present in `origin/develop` (remote).

With all these things in mind, don’t forget to `git fetch` in prior.

### How git pull works

I think most of us know how git pull works. But I will try to add some words to it.

```
git pull
```

The above command will first execute `git fetch` first. Then it will execute the `git merge` command in the working directory. It means that, if you are in the master branch then the remote changes in master will be merged to the local master branch.

[![](https://cdn.substack.com/image/fetch/w_1456,c_limit,f_auto,q_auto:good/https%3A%2F%2Fbucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2F298ec44a-154d-43e4-9271-b35b17ef011b_808x224.png)](https://cdn.substack.com/image/fetch/c_limit,f_auto,q_auto:good/https%3A%2F%2Fbucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2F298ec44a-154d-43e4-9271-b35b17ef011b_808x224.png)

In the above image, you can see new origin branches are created and then the new changes from the remote are merged to the branch `develop`.

```
git pull origin develop
```

This command will only fetch the remote changes of develop branch and not other branches. And it also merges the remote changes to the branch.

By default, git pull will execute `git merge`. If you want to rebase it, you can try

```
git pull —rebase
```

If you are not confident enough with rebase, don’t worry I will explain the rebasing concept in another post.

#### Key points to takeaway

1. Use `git fetch` to download all the remote changes to local without affecting your flow. And to compare remote changes with the local changes before merging or rebasing. This will help a lot.

2. `git diff <branch_name> origin/<branch_name> `- to know the remote changes. Remember that all the changes from the second branch are shown and the changes from the first branch are omitted. So that you can see the difference between the two branches.

3. `git log develop..origin/develop `- to know the commit logs. As mentioned earlier, the difference between the two branches is shown.

4. `git pull `-` `Remember that it will execute `git fetch` first and then will call `merge` command. So try to use it often.

That’s it for today.

If you have any queries, you can just leave a comment. :)

#### *If you have come this much, then I think you are much interested in Git. You can subscribe to [GitBetter](https://gitbetter.substack.com/) to get tricks, tips, and advanced topics of Git.*

Thank you for reading :)