what is the difference between git pull , git fetch and git rebase?

10,173

Fetch: update local with remote changes but not merge with any local branch.

Pull: update local and merge the changes with current-branch.

  • git fetch: Get the latest changes from origin (no merge)

  • git pull = git fetch + git merge

  • If you rebase feature branch onto master branch. git rebase master, it would keep the feature branch commits/changes top.

    Say you have two commits in master branch (A -> C) and two commits in feature branch (B -> D).

    Assume you are in feature branch (git checkout feature). Now if you merge master then commit history:

    (previous commit) - A -- C       <- master
                      \        \
                        B -- D -- M   <- feature
    

    Here, M for new-merge-commit-sha.

    For rebase master, commit history: (A -> C -> B' -> D').

Share:
10,173
Lakmal Vithanage
Author by

Lakmal Vithanage

I'm an Associate Tech lead at Axiata Digital Labs.

Updated on June 18, 2022

Comments

  • Lakmal Vithanage
    Lakmal Vithanage almost 2 years

    What are the differences between git pull , git fetch and git rebase? I feel pull and fetch are same.

  • Code-Apprentice
    Code-Apprentice about 7 years
    Usually you would rebase feature onto master, not the other way around.
  • Code-Apprentice
    Code-Apprentice about 7 years
    And git merge master will result in a much more complicated commit history than what you have shone.
  • Sajib Khan
    Sajib Khan about 7 years
    Agree @Code-Apprentice, here I tried to show the simple/generic difference.
  • Code-Apprentice
    Code-Apprentice about 7 years
    Keeping it simple is fine as long as it is correct. As you have it here, the commit history is incorrect in your paragraph about git merge.
  • Code-Apprentice
    Code-Apprentice about 7 years
    Also, the wording describing rebase is incorrect. git rebase master will rebase the current branch onto master.
  • Sajib Khan
    Sajib Khan about 7 years
    Yes, here I assume current branch is feature. git checkout feature, git rebase master. So, it will rebase the feature onto master.
  • Code-Apprentice
    Code-Apprentice about 7 years
    Please edit your answer to say so.
  • Sajib Khan
    Sajib Khan about 7 years
    I metioned it in answer already. "Assume you are in feature branch. Now if you merge master then..."
  • Code-Apprentice
    Code-Apprentice about 7 years
    What does that quote have to do with rebase? I'm talking about "If you rebase master branch into feature branch. git rebase master..." which is incorrect. You do not rebase master branch into feature branch, rather you rebase feature branch onto master branch.
  • Sajib Khan
    Sajib Khan about 7 years
    I see (it was mistake), Thanks.
  • Code-Apprentice
    Code-Apprentice about 7 years
    You should also fix your commit history illustrations to show a correct merge history.
  • Sajib Khan
    Sajib Khan about 7 years
    Fixed commit history!
  • Code-Apprentice
    Code-Apprentice about 7 years
    The commit history for git merge still is not correct. The history will not be linear.
  • Sajib Khan
    Sajib Khan about 7 years
    Edited. Seems ok now?