what is the difference between git pull , git fetch and git rebase?
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 ontomaster
branch.git rebase master
, it would keep thefeature
branchcommits/changes
top.Say you have two commits in
master
branch (A
->C
) and two commits infeature
branch (B
->D
).Assume you are in
feature
branch (git checkout feature
). Now if youmerge master
then commit history:(previous commit) - A -- C <- master \ \ B -- D -- M <- feature
Here,
M
fornew-merge-commit-sha
.For
rebase master
, commit history: (A
->C
->B'
->D'
).
Lakmal Vithanage
I'm an Associate Tech lead at Axiata Digital Labs.
Updated on June 18, 2022Comments
-
Lakmal Vithanage almost 2 years
What are the differences between
git pull
,git fetch
andgit rebase
? I feel pull and fetch are same. -
Code-Apprentice about 7 yearsUsually you would rebase
feature
ontomaster
, not the other way around. -
Code-Apprentice about 7 yearsAnd
git merge master
will result in a much more complicated commit history than what you have shone. -
Sajib Khan about 7 yearsAgree @Code-Apprentice, here I tried to show the
simple/generic
difference. -
Code-Apprentice about 7 yearsKeeping 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 about 7 yearsAlso, the wording describing rebase is incorrect.
git rebase master
will rebase the current branch ontomaster
. -
Sajib Khan about 7 yearsYes, here I assume current branch is
feature
.git checkout feature
,git rebase master
. So, it will rebase thefeature
ontomaster
. -
Code-Apprentice about 7 yearsPlease edit your answer to say so.
-
Sajib Khan about 7 yearsI metioned it in answer already.
"Assume you are in feature branch. Now if you merge master then..."
-
Code-Apprentice about 7 yearsWhat 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 about 7 yearsI see (it was mistake), Thanks.
-
Code-Apprentice about 7 yearsYou should also fix your commit history illustrations to show a correct merge history.
-
Sajib Khan about 7 yearsFixed commit history!
-
Code-Apprentice about 7 yearsThe commit history for
git merge
still is not correct. The history will not be linear. -
Sajib Khan about 7 yearsEdited. Seems ok now?