Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to discard local changes and pull latest from GitHub repository

Tags:

git

github

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?

like image 618
Coding Duchess Avatar asked Aug 04 '16 20:08

Coding Duchess


People also ask

How do I remove changes from GitHub?

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.

Will git pull delete local changes?

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.


1 Answers

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.

like image 69
Cody Avatar answered Sep 24 '22 06:09

Cody