technophyle
technophyle

Reputation: 9118

What is the difference between git pull and git reset --hard origin/<branch>?

I find the latter to be faster than the first, so I usually do that after git fetch whenever I need to sync my local branch with the remote. What is the difference?

Upvotes: 33

Views: 15984

Answers (5)

Sajib Khan
Sajib Khan

Reputation: 24136

$ git pull                        
# takes the latest changes of origin/branch (exists both local & remote changes)

$ git reset --hard origin/branch  
# replace your local with origin's branch history (discard local changes)

Example: Say, we have two commits in local A, B and remote has two commits A, C. Now if you pull then your local contains A, B, C unlike if reset then your local will have A, C not B.

Upvotes: 7

VaTo
VaTo

Reputation: 3078

They are completely different in what they do:

git pull: Incorporates changes from a remote repository into the current branch. In its default mode, git pull is shorthand for git fetch followed by git merge FETCH_HEAD.

With git reset --hard origin/branch Git will:

  • Make your current branch (typically master) back to point at <SOME-COMMIT>.
  • Then make the files in your working tree and the index ("staging area") the same as the versions committed in <SOME-COMMIT>.

Note: it's always worth noting that git reset --hard is a potentially dangerous command, since it throws away all your uncommitted changes. For safety, you should always check that the output of git status is clean (that is, empty) before using it.

Upvotes: 0

Marina Liu
Marina Liu

Reputation: 38096

These two commands mostly used for different situations.

git pull pull (fetch & merge) changes from remote to local, especially other push commits to remote and you want these commit apply on your local branch.

git reset --hard origin/branch is force to make your local branch point to the commit where origin/branch is pointing to. It’s usually for the situation you want to abandon the local changes you made and keep the local branch as remote status.

Upvotes: 3

hurturk
hurturk

Reputation: 5454

The way you are getting a source update is more important than your performance concern. You need to utilize them for specific cases. I can provide you two examples:

  • git reset --hard ... can be used for updating code in production if you often find yourself modifying production code for debugging. That usually happens on early stage of a project deployment with lazy thoughts.
  • git pull is most accepted way of getting new commits and it usually makes sense you won't have huge local changes. If your commit periods are longer with more people on same branch, you may eventually have a gut feeling to do git pull --rebase or fetch first then compare.

Git can be explicit for very specific needs of your development style, you can discover them on the road. Performance shouldn't be concern at all once you are happy your work is safe.

Upvotes: 1

Penguin Brian
Penguin Brian

Reputation: 2131

The following commands:

git fetch
git reset --hard origin/<branch>

will discard all local changes.

Where as:

git pull

Which is exactly the same as:

git fetch
git merge origin/<branch>

will attempt to preserve local changes.

Upvotes: 54

Related Questions