In this article, we will see what information git fetch retrieves in different cases and what it does not.
Does git fetch pull commits?
git fetch is not like git pull. It would only retrieve the latest information about the repository branches to local git. It does not pull any commits. git pull is a combination of git fetch and git merge and pulls the commits.
Does git fetch pull all remote repositories?
When you issue git fetch in a branch, it will not pull information about all the repositories. It would pull information only from the remote repository that corresponds to the current branch. You can use git fetch explicitly on specific repositories – git fetch repositoryName. Here, repositoryName represents the name of the remote repository. You can issue this command on any remote repository added in the local git.
Let us see with a hands-on example.
The initial setup of local git is as shown. There is a branch with the name main corresponding to repository repoone. There are two remote repositories repoone and repotwo, added.
~/repoone$ git branch * main ~/codeversion$ git remote -v origin firstname.lastname@example.org:codeversionmaster/repoone.git (fetch) origin email@example.com:codeversionmaster/repoone.git (push) repotwo firstname.lastname@example.org:codeversionmaster/repotwo.git (fetch) repotwo email@example.com:codeversionmaster/repotwo.git (push)
We can also see that repotwo has commits in branch main. We still do not have any respective local branch in the local git.
Now, let us issue git fetch and see if it gets information from all the repositories. As you can see, it retrieved information only from the origin repository, which is repoone.
~/codeversion$ git fetch remote: Enumerating objects: 5, done. remote: Counting objects: 100% (5/5), done. remote: Compressing objects: 100% (3/3), done. remote: Total 3 (delta 1), reused 0 (delta 0), pack-reused 0 Unpacking objects: 100% (3/3), 705 bytes | 705.00 KiB/s, done. From github.com:codeversionmaster/repoone bead66e..27658cf main -> origin/main
Does git fetch pull all branches?
git fetch would retrieve information about all the branches in the remote repository. The remote repository is the repository to which the current branch corresponds. It would pull information for the branches not even present in local git. Note that git fetch would pull only information and not actual commits.
Let us see with a hands-on example.
The initial setup is as shown.
git branch -a shows that we are in a branch with the name main. There are two branches main and maincopy, in remote repository origin (repoone).
git log –oneline shows that the local branch main has seven commits.
git remote -v shows that the local git has added two remote repositories.
~/codeversion$ git branch -a * main remotes/origin/HEAD -> origin/main remotes/origin/main remotes/origin/maincopy ~/codeversion$ git log --oneline bead66e (HEAD -> main) Another change to new file 3f54558 This is new file 4632c0d (origin/maincopy) Adding empty directory 908d6ac Third commit in main 42296d0 Second commit in main 453424e This is first commit 2673111 Initial commit ~/codeversion$ git remote -v origin firstname.lastname@example.org:codeversionmaster/repoone.git (fetch) origin email@example.com:codeversionmaster/repoone.git (push) repotwo firstname.lastname@example.org:codeversionmaster/repotwo.git (fetch) repotwo email@example.com:codeversionmaster/repotwo.git (push)
We can see in online GitHub that main branch in repoone repository got three new commits compared to the local branch.
We can see in online GitHub that maincopy branch from repoone has commits. We do not have this branch in local git.
Now, when we issue git fetch, you can see that it fetches information of all the branches – main and maincopy.
~/codeversion$ git branch * main paperspace@pst1blf1w:~/codeversion$ git fetch remote: Enumerating objects: 9, done. remote: Counting objects: 100% (9/9), done. remote: Compressing objects: 100% (5/5), done. remote: Total 6 (delta 3), reused 0 (delta 0), pack-reused 0 Unpacking objects: 100% (6/6), 1.32 KiB | 1.32 MiB/s, done. From github.com:codeversionmaster/repoone 9870df7..d0978f9 main -> origin/main 4632c0d..cb1a2da maincopy -> origin/maincopy
git status would show that the local branch is three commits behind the online repository.
~/codeversion$ git status On branch main Your branch is behind 'origin/main' by 3 commits, and can be fast-forwarded. (use "git pull" to update your local branch) nothing to commit, working tree clean
Note that we can always do git fetch origin, explicitly giving the repository name to retrieve all the information from the repository “origin” alone.