Search This Blog

Tuesday, August 9, 2016

What is the Difference between git fetch and git pull ?



The main difference between git fetch and git pull lies in how they retrieve and update your local repository with changes from a remote repository:


1. git fetch

  • What it does:

    • Downloads the latest changes (commits, branches, tags, etc.) from the remote repository.
    • Does not integrate (merge) these changes into your local branches.
  • Purpose:
    Use git fetch when you want to check the latest updates on the remote without affecting your working directory or local branches.

  • Workflow:
    After fetching, you can inspect the fetched updates and decide how to incorporate them (e.g., merge or rebase).

  • Command:

    git fetch
  • Example Output:

    * [new branch] feature/xyz -> origin/feature/xyz
  • Usage Scenario:
    Ideal when you want to review remote changes before merging them.


2. git pull

  • What it does:

    • Combines git fetch and git merge.
    • Fetches changes from the remote repository and immediately integrates them into your current branch.
  • Purpose:
    Use git pull when you want to update your local branch to match the remote branch.

  • Command:

    git pull
  • Workflow:
    Automatically merges fetched changes into your working branch. If there are conflicts, you must resolve them before completing the merge.

  • Usage Scenario:
    Useful for quickly synchronizing your branch with the remote.


Key Differences

Aspectgit fetchgit pull
Downloads ChangesYesYes
Merges ChangesNoYes
Safe for InspectionYes, lets you review changes before merging.No, directly integrates fetched changes.
Conflict HandlingNo conflicts, as no merge is performed.May result in merge conflicts.

When to Use Each?

  • Use git fetch:

    • To preview changes on the remote without altering your local branch.
    • Before running git pull, to check if there are updates.
  • Use git pull:

    • To update your local branch quickly and directly when you're ready to accept the changes.

Example Combined Workflow

  1. Fetch updates from the remote:
    git fetch origin
  2. Inspect changes:
    git log origin/main
  3. Merge updates when ready:
    git merge origin/main

Cheers 
Kapil 

No comments:

Post a Comment

Thanks for your comment, will revert as soon as we read it.

Popular Posts