日本語

How to Compare git fetch and git pull Output

​​Git​command line

git fetch downloads new commits from the remote into remote-tracking branches such as origin/main, but it does not change your local main or your working files. git pull does the same download and then merges the result into your local main. When working on a team and you want to check changes someone else pushed before taking them in, use git fetch. Here, I ran git fetch and git pull in two clones in the same state and put the output from before and after side by side.

Note

The test environment is Ubuntu 24.04 with Git 2.43.0. I set the author name and email to example values (Neko <neko@example.com>) and fixed the commit dates with environment variables so the output stays the same each time. The remote is a repository I created in the same test environment, and the local clones point to it with the relative path ../remote.git. In the From line of the output, the .git suffix is omitted, so it shows ../remote. I did not test GitHub or other hosting services, or Windows and macOS. I also did not test the case where both the local and remote have different commits.

The test setup

I cloned a repository that contained only index.html twice, at the same point in time. Then, from a third clone, I pushed a commit that added about.html to main, and I pushed a new branch, dark-mode. I ran git fetch in one of the two clones and git pull in the other.

Neither clone knows about the remote changes yet. In both, git status reports that the branch is the same as origin/main (up to date).

console
$ git status
On branch main
Your branch is up to date with 'origin/main'.

nothing to commit, working tree clean

$ git branch -a
* main
  remotes/origin/HEAD -> origin/main
  remotes/origin/main

$ git log --oneline --decorate --graph --all
* ea95d47 (HEAD -> main, origin/main, origin/HEAD) 最初のコミット

$ ls
index.html

Your branch is up to date with 'origin/main'. is based on the origin/main you have locally. The remote already has new commits, but they do not appear here until you fetch them.

A diagram with three states stacked vertically: before, after git fetch, and after git pull. After fetch, only origin/main moves to fa1a6fe, while main stays at ea95d47, and the only working file is index.html. After pull, main also catches up to fa1a6fe and about.html appears (the labels in this image are in Japanese)
How the commits that main and origin/main point to, and the working files, change with fetch and with pull

The output of git fetch and the state afterward

The output of git fetch is as follows.

console
$ git fetch
From ../remote
   ea95d47..fa1a6fe  main       -> origin/main
 * [new branch]      dark-mode  -> origin/dark-mode

Each line corresponds to one branch. main -> origin/main means the remote main was fetched into the local origin/main, and ea95d47..fa1a6fe shows the commit that origin/main pointed to before and after. The * at the start marks a newly fetched branch. According to the output section of the git fetch reference, the before..after part can be passed to git log as is.

After the fetch, the output of git status changes.

console
$ git status
On branch main
Your branch is behind 'origin/main' by 1 commit, and can be fast-forwarded.
  (use "git pull" to update your local branch)

nothing to commit, working tree clean

This tells you that your local main is one commit behind origin/main and can catch up with a fast-forward. Looking at the branch list, the history, and the files, only the remote-tracking branches moved.

console
$ git branch -a
* main
  remotes/origin/HEAD -> origin/main
  remotes/origin/dark-mode
  remotes/origin/main

$ git log --oneline --decorate --graph --all
* 33ee204 (origin/dark-mode) ダークモードの色を足す
* fa1a6fe (origin/main, origin/HEAD) 紹介ページを足す
* ea95d47 (HEAD -> main) 最初のコミット

$ ls
index.html

main is still at ea95d47, and about.html does not exist yet. The new branch shows up only as remotes/origin/dark-mode. A local branch named dark-mode has not been created.

As described in the description section of the git fetch reference, the names of the fetched branches and the commits they point to are also written to .git/FETCH_HEAD.

console
$ cat .git/FETCH_HEAD
fa1a6fe885ed6be0e01720b70055a06b0516c3af		branch 'main' of ../remote
33ee20484daaf8b9f333133f06c69b7eeaacd57b	not-for-merge	branch 'dark-mode' of ../remote

Check the fetched changes before taking them in

After git fetch, you can look at what changed on the remote without touching your local files. main..origin/main refers to "commits reachable from origin/main but not from main", as described in the range notation section of the reference.

console
$ git log --oneline main..origin/main
fa1a6fe 紹介ページを足す

$ git diff --stat main origin/main
 about.html | 1 +
 1 file changed, 1 insertion(+)

$ git log --oneline -p -1 origin/main
fa1a6fe 紹介ページを足す
diff --git a/about.html b/about.html
new file mode 100644
index 0000000..0d799f0
--- /dev/null
+++ b/about.html
@@ -0,0 +1 @@
+<h1>このブログについて</h1>

If the contents look fine, take them in with git merge origin/main.

console
$ git merge origin/main
Updating ea95d47..fa1a6fe
Fast-forward
 about.html | 1 +
 1 file changed, 1 insertion(+)
 create mode 100644 about.html

$ git status
On branch main
Your branch is up to date with 'origin/main'.

nothing to commit, working tree clean

$ ls
about.html
index.html

Only now does main move forward, and about.html appears in the working folder.

The output of git pull is fetch and merge combined

In the other clone (which started in the same state), I ran git pull.

console
$ git pull
From ../remote
   ea95d47..fa1a6fe  main       -> origin/main
 * [new branch]      dark-mode  -> origin/dark-mode
Updating ea95d47..fa1a6fe
Fast-forward
 about.html | 1 +
 1 file changed, 1 insertion(+)
 create mode 100644 about.html

The first three lines match the output of git fetch, and everything from Updating onward matches the output of git merge origin/main. The git pull reference also says that git pull runs git fetch and then calls git rebase or git merge, depending on the settings and options.

The history and files afterward were the same as in the clone where I ran git fetch followed by git merge.

console
$ git log --oneline --decorate --graph --all
* 33ee204 (origin/dark-mode) ダークモードの色を足す
* fa1a6fe (HEAD -> main, origin/main, origin/HEAD) 紹介ページを足す
* ea95d47 最初のコミット

$ ls
about.html
index.html

With git pull, too, dark-mode was only fetched as origin/dark-mode, and no local branch was created. Only the branch you are currently on (here, main) gets updated.

The only difference was the name of the operation recorded in git reflog. In the clone where I ran git fetch and then git merge origin/main, it is:

console
$ git reflog -1
fa1a6fe HEAD@{0}: merge origin/main: Fast-forward

In the clone where I ran git pull, it is:

console
$ git reflog -1
fa1a6fe HEAD@{0}: pull: Fast-forward

What you see when there is nothing new

If you run git fetch again after fetching but before taking the changes in, it prints nothing and exits with code 0. If you run git pull again after taking the changes in, it prints Already up to date.

console
$ git fetch; echo "exit=$?"
exit=0
console
$ git pull
Already up to date.

The output of git fetch is empty because no branch changed. As described in the output section of the reference, branches that were not updated are shown only when you use --verbose.

In this case, my local main had no commits that were not yet pushed, so both git pull and git merge finished with a fast-forward. If both the local and remote have different commits, git pull in Git 2.43.0 stops and asks you to specify how to integrate them (merge or rebase). For how to fix that case, see the article "How to fix the git pull divergent branches error" on this blog, which shows the actual output.

​Posted by 86