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?
Try Git checkout --<file> to discard uncommitted changes to a file. Git reset --hard is for when you want to discard all uncommitted changes. Use Git reset --hard <commit id> to point the repo to a previous commit.
The for loop will delete all tracked files which are changed in the local repo, so git pull will work without any problems. The nicest thing about this is that only the tracked files will be overwritten by the files in the repo, all other files will be left untouched.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With