[git] How to discard local changes and pull latest from GitHub repository

I have a directory on my machine where I store all projects from GitHub. I opened one of them and made changes locally on my machine. Project got messed up and now I want to discard all the changes I made and pull the latest version from the repository. I am relatively new to GitHub, using Git Shell.

Would git pull command be sufficient? Do I need to do anything extra to discard the changes made locally? Can anyone help?

This question is related to git github

The answer is


git reset is what you want, but I'm going to add a couple extra things you might find useful that the other answers didn't mention.

git reset --hard HEAD resets your changes back to the last commit that your local repo has tracked. If you made a commit, did not push it to GitHub, and want to throw that away too, see @absiddiqueLive's answer.

git clean -df will discard any new files or directories that you may have added, in case you want to throw those away. If you haven't added any, you don't have to run this.

git pull (or if you are using git shell with the GitHub client) git sync will get the new changes from GitHub.

Edit from way in the future: I updated my git shell the other week and noticed that the git sync command is no longer defined by default. For the record, typing git sync was equivalent to git pull && git push in bash. I find it still helpful so it is in my bashrc.


Run the below commands

git log

From this you will get your last push commit hash key

git reset --hard <your commit hash key>

If you already committed the changes than you would have to revert changes.

If you didn't commit yet, just do a clean checkout git checkout .


In addition to the above answers, there is always the scorched earth method.

rm -R <folder>

in Windows shell the command is:

rd /s <folder>

Then you can just checkout the project again:

git clone -v <repository URL> 

This will definitely remove any local changes and pull the latest from the remote repository. Be careful with rm -R as it will delete your good data if you put the wrong path. For instance, definitely do not do:

rm -R /

edit: To fix spelling and add emphasis.


To push over old repo. git push -u origin master --force

I think the --force would work for a pull as well.


Questions with git tag:

Does the target directory for a git clone have to match the repo name? Git fatal: protocol 'https' is not supported Git is not working after macOS Update (xcrun: error: invalid active developer path (/Library/Developer/CommandLineTools) git clone: Authentication failed for <URL> destination path already exists and is not an empty directory SSL_connect: SSL_ERROR_SYSCALL in connection to github.com:443 GitLab remote: HTTP Basic: Access denied and fatal Authentication How can I switch to another branch in git? VS 2017 Git Local Commit DB.lock error on every commit How to remove an unpushed outgoing commit in Visual Studio? How to know the git username and email saved during configuration? How to add a new project to Github using VS Code git clone error: RPC failed; curl 56 OpenSSL SSL_read: SSL_ERROR_SYSCALL, errno 10054 fatal: ambiguous argument 'origin': unknown revision or path not in the working tree HTTP Basic: Access denied fatal: Authentication failed npm notice created a lockfile as package-lock.json. You should commit this file Do I commit the package-lock.json file created by npm 5? Abort a Git Merge key_load_public: invalid format git - remote add origin vs remote set-url origin Visual Studio 2017 - Git failed with a fatal error Get git branch name in Jenkins Pipeline/Jenkinsfile Changing the git user inside Visual Studio Code How to compare different branches in Visual Studio Code Git checkout - switching back to HEAD Clear git local cache Deleting a local branch with Git Rebuild Docker container on file changes Cloning specific branch How to add chmod permissions to file in Git? Git copy changes from one branch to another Git merge with force overwrite Project vs Repository in GitHub How to add a file to the last commit in git? Getting permission denied (public key) on gitlab Delete commit on gitlab gpg failed to sign the data fatal: failed to write commit object [Git 2.10.0] Remove a modified file from pull request Updates were rejected because the tip of your current branch is behind its remote counterpart Can't push to the heroku How to discard local changes and pull latest from GitHub repository In Visual Studio Code How do I merge between two local branches? error: RPC failed; curl transfer closed with outstanding read data remaining Change drive in git bash for windows Checkout Jenkins Pipeline Git SCM with credentials? How to fix git error: RPC failed; curl 56 GnuTLS Trying to pull files from my Github repository: "refusing to merge unrelated histories" Visual Studio Code how to resolve merge conflicts with git? merge one local branch into another local branch Can't push to remote branch, cannot be resolved to branch

Questions with github tag:

Does the target directory for a git clone have to match the repo name? Issue in installing php7.2-mcrypt How can I switch to another branch in git? How to draw checkbox or tick mark in GitHub Markdown table? How to add a new project to Github using VS Code git clone error: RPC failed; curl 56 OpenSSL SSL_read: SSL_ERROR_SYSCALL, errno 10054 How to add empty spaces into MD markdown readme on GitHub? key_load_public: invalid format git - remote add origin vs remote set-url origin Cloning specific branch How to upload folders on GitHub Git merge with force overwrite Project vs Repository in GitHub Install specific branch from github using Npm gpg failed to sign the data fatal: failed to write commit object [Git 2.10.0] Remove a modified file from pull request What do raw.githubusercontent.com URLs represent? How to discard local changes and pull latest from GitHub repository Trying to pull files from my Github repository: "refusing to merge unrelated histories" Git - remote: Repository not found git status (nothing to commit, working directory clean), however with changes commited When does Git refresh the list of remote branches? How to remove commits from a pull request github: server certificate verification failed Visual Studio Code always asking for git credentials What I can do to resolve "1 commit behind master"? How to overcome "'aclocal-1.15' is missing on your system" warning? How do I connect to my existing Git repository using Visual Studio Code? How to download a folder from github? Git: How to remove proxy Git push: "fatal 'origin' does not appear to be a git repository - fatal Could not read from remote repository." Jenkins - how to build a specific branch There is no tracking information for the current branch How to rename a directory/folder on GitHub website? Git pull till a particular commit Git Clone from GitHub over https with two-factor authentication error: pathspec 'test-branch' did not match any file(s) known to git How can I install a package with go get? How can I clone a private GitLab repository? Git : fatal: Could not read from remote repository. Please make sure you have the correct access rights and the repository exists Copy a git repo without history GitHub: invalid username or password How to unmerge a Git merge? How to use private Github repo as npm dependency ! [rejected] master -> master (fetch first) How to remove a branch locally? How can I check out a GitHub pull request with git? How to resolve "git pull,fatal: unable to access 'https://github.com...\': Empty reply from server" Can you get the number of lines of code from a GitHub repository? Git: Installing Git in PATH with GitHub client for Windows