Search code examples
gitgit-pullgit-fetch

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


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


Solution

  • 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').