git pull Vs git fetch

git pull Vs git fetch

All branches are stored in .git/refs

All local branches are stored in .git/refs/heads

All remote branches are stored in .git/refs/remotes

The git fetch command downloads commits, files, and refs from a remote repository into your local repo. Fetching is what you do when you want to see what everybody else has been working on.

So when you do git fetch all the files, commits, and refs are downloaded in

this directory .git/refs/remotes

You can switch to these branches to see the changes.

Also, you can merge them if you want.

git pull just downloads these changes and also merges them to the current branch.

Example

If you want see the work of remote branch dev/jd/feature/auth, you just need to do

git fetch origin dev/jd/feature/auth

to see the changes or work progress do,

git checkout dev/jd/feature/auth

But, If you also want to fetch and merge them in the current branch do,

git pull origin dev/jd/feature/auth

If you do

git fetch origin branch_name

it will fetch the branch, now you can switch to this branch you want and see the changes. Your local master or other local branches won't be affected.

But

git pull origin branch_name

will fetch the branch and will also merge to the current branch.

Did you find this article valuable?

Support Sujeet Agrahari by becoming a sponsor. Any amount is appreciated!