This post was originally posted in the newsletter GitBetter. 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
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
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
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.
git diff develop origin/develop —stat
This command will just show only the files changed.
There is another helpful trick you can use.
git log develop..origin/develop
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.
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
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.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.git log develop..origin/develop
- to know the commit logs. As mentioned earlier, the difference between the two branches is shown.git pull
-Remember that it will execute
git fetch
first and then will callmerge
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 to get tricks, tips, and advanced topics of Git.
Thank you for reading :)
Top comments (0)