How can I pull from remote Git repository and override the changes in my local repository?

Git

Git Problem Overview


I need to throw away all the changes in my local repository and pull all the code from the remote repository. What is the Git command to do this?

Git Solutions


Solution 1 - Git

Provided that the remote repository is origin, and that you're interested in master:

git fetch origin
git reset --hard origin/master

This tells it to fetch the commits from the remote repository, and position your working copy to the tip of its master branch.

All your local commits not common to the remote will be gone.

Solution 2 - Git

As an addendum, if you want to reapply your changes on top of the remote, you can also try:

git pull --rebase origin master

If you then want to undo some of your changes (but perhaps not all of them) you can use:

git reset SHA_HASH

Then do some adjustment and recommit.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionBarkaView Question on Stackoverflow
Solution 1 - GitCharlesBView Answer on Stackoverflow
Solution 2 - Gitsebastian-cView Answer on Stackoverflow