What are the differences between git pull
, git fetch
and git rebase
? I feel pull and fetch are same.
Asked
Active
Viewed 1.1k times
6

Lakmal Vithanage
- 2,767
- 7
- 42
- 58
-
2http://stackoverflow.com/questions/292357/difference-between-git-pull-and-git-fetch – jophab Feb 12 '17 at 06:07
-
1I suggest you read [Pro Git](https://git-scm.com/book/en/v2). The first three chapters cover 95% of what you need on a daily basis, including the answer to your question. – Code-Apprentice Feb 12 '17 at 06:09
-
`git pull --rebase` is equivalent to the latter two – Pockets Feb 12 '17 at 06:40
1 Answers
5
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'
).

Sajib Khan
- 22,878
- 9
- 63
- 73
-
1Usually you would rebase `feature` onto `master`, not the other way around. – Code-Apprentice Feb 12 '17 at 06:06
-
1And `git merge master` will result in a much more complicated commit history than what you have shone. – Code-Apprentice Feb 12 '17 at 06:08
-
Agree @Code-Apprentice, here I tried to show the `simple/generic` difference. – Sajib Khan Feb 12 '17 at 06:53
-
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 Feb 12 '17 at 06:57
-
Also, the wording describing rebase is incorrect. `git rebase master` will rebase the current branch onto `master`. – Code-Apprentice Feb 12 '17 at 06:58
-
Yes, here I assume current branch is `feature`. `git checkout feature`, `git rebase master`. So, it will rebase the `feature` onto `master`. – Sajib Khan Feb 12 '17 at 07:07
-
-
I metioned it in answer already. `"Assume you are in feature branch. Now if you merge master then..."` – Sajib Khan Feb 12 '17 at 07:10
-
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. – Code-Apprentice Feb 12 '17 at 07:11
-
-
You should also fix your commit history illustrations to show a correct merge history. – Code-Apprentice Feb 12 '17 at 07:15
-
-
The commit history for `git merge` still is not correct. The history will not be linear. – Code-Apprentice Feb 12 '17 at 07:41
-