241

I'm a noob in Git, and trying to learn the difference between git pull vs git rebase. Can someone provide an example when to use which option since I feel that both serve the same purpose.

Tim
  • 41,901
  • 18
  • 127
  • 145
user4943236
  • 5,914
  • 11
  • 27
  • 40
  • Yes I did. The difference between pull and rebase is that pull does a merge. So the answer to your question is at the link i pasted. – Thibault D. Mar 22 '16 at 07:58
  • 1
    Possible duplicate of [What's the difference between 'git merge' and 'git rebase'?](https://stackoverflow.com/questions/16666089/whats-the-difference-between-git-merge-and-git-rebase) – SiGe Mar 09 '18 at 08:49
  • 1
    Does this answer your question? [git pull VS git fetch Vs git rebase](https://stackoverflow.com/questions/3357122/git-pull-vs-git-fetch-vs-git-rebase) – Channa Jul 15 '20 at 18:44

3 Answers3

255

git pull and git rebase are not interchangeable, but they are closely connected.

git pull fetches the latest changes of the current branch from a remote and applies those changes to your local copy of the branch. Generally this is done by merging, i.e. the local changes are merged into the remote changes. So git pull is similar to git fetch & git merge.

Rebasing is an alternative to merging. Instead of creating a new commit that combines the two branches, it moves the commits of one of the branches on top of the other.

You can pull using rebase instead of merge (git pull --rebase). The local changes you made will be rebased on top of the remote changes, instead of being merged with the remote changes.

Atlassian has some excellent documentation on merging vs. rebasing.

Daniel Lerps
  • 5,256
  • 3
  • 23
  • 33
Peter
  • 2,932
  • 1
  • 12
  • 9
  • 7
    So what's actual diffrence between putting feature on top of master and merging feature with master , in the end both are mixed aren't they ? – Omar Essam El-Din May 24 '21 at 13:02
  • 7
    The content of the resulting commit is indeed indistinguishable. However, the commit tree looks a bit different. The reason I prefer rebasing over merging is that if you have conflicts, you resolve them commit-wise instead of all during the single merge commit. Also, is results in a cleaner more linear commit tree, which makes it easier to walk through. – Peter May 25 '21 at 15:06
  • 1
    When you will check the commit history, if you have used Git Pull, it will show you an extra merge commit, while in git rebase, that wont be there. Git Rebase is a cleaner approach from the context of git history. – Mohit Sep 14 '21 at 04:27
  • So the only different is history related only...? if I wont these merged changes to be in a separate commit on "rewrite/recommit" the commits – I.sh. Feb 06 '23 at 15:20
87

git-pull - Fetch from and integrate with another repository or a local branch GIT PULL

Basically you are pulling remote branch to your local, example:

git pull origin master

Will pull master branch into your local repository

git-rebase - Forward-port local commits to the updated upstream head GIT REBASE

This one is putting your local changes on top of changes done remotely by other users. For example:

  • You have committed some changes on your local branch for example called SOME-FEATURE
  • Your friend in the meantime was working on other features and he merged his branch into master

Now you want to see his and your changes on your local branch. So then you checkout master branch:

git checkout master

then you can pull:

git pull origin master

and then you go to your branch:

git checkout SOME-FEATURE

and you can do rebase master to get lastest changes from it and put your branch commits on top:

git rebase master

I hope now it's a bit more clear for you.

Melebius
  • 6,183
  • 4
  • 39
  • 52
Tomasz
  • 4,847
  • 2
  • 32
  • 41
58

In a nutshell :

Git Merge:

Merges your local changes and remote changes, and that will create another commit history record

Git Rebase:

Put your changes above all new remote changes, and rewrite commit history, so your commit history will be much cleaner than git merge. Rebase is a destructive operation. That means, if you do not apply it correctly, you could lose committed work and/or break the consistency of other developer's repositories.

vsync
  • 118,978
  • 58
  • 307
  • 400
Saurin Vala
  • 1,898
  • 1
  • 17
  • 23