How to discard local changes and pull latest from GitHub repository?

This is useful if you sand-box some code in your test or development environment. After making many local changes, you want to discard all local changes and pull the latest from the remote master – GitHub repository.

Without discarding local changes if you try to pull, git will show this error:

Git pull error: Your local changes to the following files would be overwritten by merge
error: Your local changes to the following files would be overwritten by the merge

Discard local changes and pull from the GitHub master again

Commands to discard local changes and pull from the GitHub source. git reset –hard will git discard all local changes

# Reset your local changes (discard every deviation from the last commit)
git reset --hard

Now, try using git pull from the remote branch (where ever you are, master branch or any specific branch). We will use the git pull.

git pull

With this, git will throw away all local changes (discard or reset them to the last pull), and with the git pull, your local gitrepo will become in sync with the latest remote version.

Related How tos: