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?
5 Answers
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.
$ 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
.
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.
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 dogit 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.
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.
git pull
fetches from remote repositories,git reset
deals with the local tree. How are they even comparable?git pull
does the merge.git fetch
withgit reset --hard
only checkouts it.git pull
(which isgit fetch
followed bygit merge
) as: move me to a new address, taking all my possessions. Think ofgit fetch
followed bygit 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...git stash
- I don't have any possessions.. :-)