How to discard local changes and pull latest from GitHub repository
GitGithubGit Problem Overview
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?
Git Solutions
Solution 1 - Git
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.
Solution 2 - Git
Run the below commands
git log
From this you will get your last push commit hash key
git reset --hard <your commit hash key>
Solution 3 - Git
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 .
Solution 4 - Git
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.
Solution 5 - Git
Just adding to what other people said I would do the following. If you already staged changes, meaning you already did "git add" at some point, I would do:
git reset HEAD <file_name>
If you haven't stated files for changes, as someone mentioned before just do:
git checkout .
Solution 6 - Git
To push over old repo.
git push -u origin master --force
I think the --force
would work for a pull as well.