31

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?

15
  • 1
    git pull fetches from remote repositories, git reset deals with the local tree. How are they even comparable?
    – zerkms
    Mar 27, 2017 at 3:05
  • 2
    if your local branch differs from remote, git pull does the merge. git fetch with git reset --hard only checkouts it.
    – ymonad
    Mar 27, 2017 at 3:05
  • 7
    As a very loose analogy, think of git pull (which is git fetch followed by git merge) as: move me to a new address, taking all my possessions. Think of git fetch followed by git reset --hard as: burn all my possessions, then set me up at the new address. Since the houses come furnished, if you have no possessions of your own, the result is the same. But if you do, well...
    – torek
    Mar 27, 2017 at 3:40
  • git stash - I don't have any possessions.. :-)
    – hurturk
    Mar 27, 2017 at 3:47
  • @hurturk or, just burn all my possessions. Jun 26, 2017 at 20:36

5 Answers 5

50

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.

6
$ 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.

2

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.

0

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.

0

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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.