git-descargar-cambios-con-fetch-pull

Download Changes from the Server with git fetch and git pull

  • 5 min

When you work in a team, your colleagues will be constantly pushing code to the remote server (just like you).

To keep your local repository updated, you need to bring those changes to your computer.

To do this, Git offers us two commands:

  • git fetch. The safe way to check
  • git pull: The fast way to integrate

Both have important differences in how they work.

Featuregit fetchgit pull
Downloads data from serverYesYes
Updates the origin/main mirrorYesYes
Modifies your local main branchNO (Safe)Yes (Can cause conflicts)
Risk LevelNoneMedium

Let’s look at each one in depth 👇.

Git Fetch: The Safe Mode

When you run git fetch origin, Git connects to the server and downloads all the new information (commits, branches, files) that your colleagues have pushed and that you don’t have.

Using fetch allows you to “look before you leap”. It doesn’t modify a single file in your Working Directory. Your current code stays exactly as it was.

Tracking Branches

If it doesn’t touch your code, where does Git store that downloaded information? In special hidden branches.

You have your local main branch. But Git has an internal “mirror” called origin/main.

  • main: This is your local branch. You control it.
  • origin/main: This is the copy of what was on the server the last time you connected. It’s read-only.

When you do git fetch, Git updates origin/main by moving its pointer to the latest commit on the server, but leaves your main intact.

Inspect and Integrate Changes

Now that you have the data downloaded, you can safely inspect it before deciding whether to merge it with your code:

  • View new history: git log --oneline main..origin/main (Shows commits they have and you don’t).
  • View code changes: git diff main origin/main (Shows the exact lines that will change).

If after inspecting you decide the changes are safe and you want them in your code, you have to do the second half of the work manually.

While on your main branch, run:

git merge origin/main
Copied!

This will merge the tracking branch (what you downloaded with fetch) into your local branch.

Git Pull: The Fast and Aggressive Way

The git pull command does exactly the previous process, but all at once and without asking you.

The formula for this command is:

git pull = git fetch + git merge

When you run git pull, you are telling Git: “Download whatever is on the server and merge it with my code RIGHT NOW”.

Since git pull implies an automatic merge that modifies your files, conflicts can occur.

If you and your colleague have touched the same line, the pull will pause and you will have to resolve the conflict manually in your editor.