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

A local Git repository refers to a copy of a Git repository that is stored on your local machine. When you clone a remote Git repository, you create a local copy on your computer. This local copy contains the entire history of the project, including all branches, commits, and files.

A local Git repository allows you to work on a project offline, without needing a constant connection to the remote repository. You can make changes, create new branches, commit modifications, and perform various Git operations within your local repository.

How to discard local changes and pull from the remote?

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

To discard local changes and pull the latest version from a GitHub repository, you can follow these steps:

This will discard all the changes in the current directory and its subdirectories. Note that this is a destructive operation, so make sure you don’t have any important changes that you want to keep.

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

If you want to commit the changes instead, use the git commit -a command to commit all the changes, followed by git push to push them to your remote repository.

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 git-repo will become in sync with the latest remote version. After executing these steps, your local repository will be synchronized with the latest version from the GitHub repository.

Related How-tos: