Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the difference between git pull and git fetch + git rebase?

Another question says that git pull is like a git fetch + git merge.

But what is the difference between git pull and git fetch + git rebase?

like image 330
michael Avatar asked Jul 28 '10 20:07

michael


People also ask

What is the difference between git pull and git pull rebase?

Now instead of performing this git fetch followed by git merge, you can directly use git pull . The git rebase is sort of an alternative to merge functionality. Instead of creating a new commit that combines the two branches, the git rebase moves the commits of one of the branches on top of the other.

What's the difference between git pull and git fetch?

git fetch is the command that tells your local git to retrieve the latest meta-data info from the original (yet doesn't do any file transferring. It's more like just checking to see if there are any changes available). git pull on the other hand does that AND brings (copy) those changes from the remote repository.

Should I use git fetch or git pull?

When comparing Git pull vs fetch, Git fetch is a safer alternative because it pulls in all the commits from your remote but doesn't make any changes to your local files. On the other hand, Git pull is faster as you're performing multiple actions in one – a better bang for your buck.

What is the difference between git fetch and git pull Mcq?

Git fetch fetches the required information only to the local repository. Git pull fetches the required information not only to the local repository but also to the workspace that you are currently working in.


2 Answers

It should be pretty obvious from your question that you're actually just asking about the difference between git merge and git rebase.

So let's suppose you're in the common case - you've done some work on your master branch, and you pull from origin's, which also has done some work. After the fetch, things look like this:

- o - o - o - H - A - B - C (master)                \                 P - Q - R (origin/master) 

If you merge at this point (the default behavior of git pull), assuming there aren't any conflicts, you end up with this:

- o - o - o - H - A - B - C - X (master)                \             /                 P - Q - R --- (origin/master) 

If on the other hand you did the appropriate rebase, you'd end up with this:

- o - o - o - H - P - Q - R - A' - B' - C' (master)                           |                           (origin/master) 

The content of your work tree should end up the same in both cases; you've just created a different history leading up to it. The rebase rewrites your history, making it look as if you had committed on top of origin's new master branch (R), instead of where you originally committed (H). You should never use the rebase approach if someone else has already pulled from your master branch.

Finally, note that you can actually set up git pull for a given branch to use rebase instead of merge by setting the config parameter branch.<name>.rebase to true. You can also do this for a single pull using git pull --rebase.

like image 167
Cascabel Avatar answered Sep 19 '22 08:09

Cascabel


TLDR:

git pull is like running git fetch then git merge
git pull --rebase is like git fetch then git rebase

In reply to your first statement,

git pull is like a git fetch + git merge.

"In its default mode, git pull is shorthand for git fetch followed by git merge FETCH_HEAD" More precisely, git pull runs git fetch with the given parameters and then calls git merge to merge the retrieved branch heads into the current branch"

(Ref: https://git-scm.com/docs/git-pull)


For your second statement/question:

'But what is the difference between git pull VS git fetch + git rebase'

Again, from same source:
git pull --rebase

"With --rebase, it runs git rebase instead of git merge."


Now, if you wanted to ask

'the difference between merge and rebase'

that is answered here too:
https://git-scm.com/book/en/v2/Git-Branching-Rebasing
(the difference between altering the way version history is recorded)

like image 32
gawkface Avatar answered Sep 20 '22 08:09

gawkface