Git & GitHub Course
Git & GitHub
/
Intermediate

git fetch

Definition

Downloads commits, files, and refs from a remote repository into your local repo, but does NOT merge those changes into your working directory.

Explain Like I'm New

The safe way to see what your team has been working on. It downloads all their new code from GitHub into your local `.git` hidden folder, but it explicitly DOES NOT touch the code you are currently looking at in VS Code. You can review their code safely before merging it.

Real World Example

Checking if anyone pushed changes to the `main` branch over the weekend without actually risking breaking the code you are currently writing.

Common Use Cases

  • •Safe syncing
  • •Reviewing teammate code

Crucial Command Flags

Flag / OptionDescription
--allFetch all remotes.
--pruneRemove any remote-tracking references that no longer exist on the remote.
--tagsFetch all tags from the remote.

Terminal Output

bash / terminal
# Download all new data from the 'origin' server $ git fetch origin # Look at what your coworkers pushed to the 'main' branch on the server $ git log origin/main # Compare your local 'main' branch with the server's 'main' branch $ git diff main origin/main # If you like their changes, you manually merge them in! $ git merge origin/main

Interview Questions

basic

  • Does `git fetch` overwrite the files in your Working Directory?

intermediate

  • How do you view the changes you just fetched?

Flash Cards

Question

Overwrite files?

Click to reveal answer
Answer

No. Never. That is the entire point of `fetch`. It is completely safe.

Question

View changes?

Click to reveal answer
Answer

You can use `git log origin/main` to see the newly downloaded commits, or `git diff main origin/main` to see the exact code differences.