3

I have done some local changes in code stored in Git repo. I don't need these changes anymore, so I would like to discard them and get a clean copy from github.

When I did a Git pull it gave me a merge conflict error.

So, I did

git reset --hard

but that did not help. I also tried

git stash

but still when I try pull from github, it does not allow me to do so.

Can someone please suggest what would be the best approach to get the latest code from github. Should I just delete the local files and then do a git pull?

swati
  • 1,157
  • 2
  • 16
  • 25
  • It would be great if you post what errors are you getting. What exactly is "merge conflict error" and what github "does not allow you to do". – Paul Sep 01 '16 at 16:09

4 Answers4

2

I would do:

git reset --hard {remote_name_here}/{branch_name_here}

  • 1
    After doing git reset --hard master, when I did a git pull, it still complained about conflicts and asked me to merge – swati Sep 02 '16 at 19:07
  • That's why I specified using the remote; if you add the remote to the command (probably `origin`) it will use the version pulled from that remote. – AgentAntelope Jul 17 '17 at 15:36
1

Try:

git checkout -f

For a great discussion of the differences between git reset and git checkout, see:

https://git-scm.com/book/en/v2/Git-Tools-Reset-Demystified#_git_reset

Gene Olson
  • 890
  • 6
  • 4
  • I did a git checkout -f and then did git pull but it still complained of Conflicts. I would like my local copy to be overwritten with whatever is there in the remote repo. Don't want to do merging. – swati Sep 02 '16 at 19:00
  • The command I recommended won't get rid of your local changes. To do that you can wipe out the entire git directory and clone it again. Alternatively look at this thread: http://stackoverflow.com/questions/1628088/reset-local-repository-branch-to-be-just-like-remote-repository-head – Gene Olson Sep 03 '16 at 03:50
  • It deleted all my files (that were not in project), it is not that I've expected. – Eugene Zakharenko May 15 '21 at 06:11
0

If you just want to get the latest code pushed on remote, you can do that.

rm -rf ./gitProject

git clone yourRemoteRepo
Kevin Grosgojat
  • 1,386
  • 8
  • 13
  • 2
    I am skeptical about doing a delete of the local project, unless there is no better way. Doing a delete means those files which are not part of repo (and mentioned in .gitignore) will also go away. All I want is my local copy to be overwritten with whatever is there in the remote repo. – swati Sep 02 '16 at 19:02
0

If you want to leave out all your local changes and get only those from github you can just delete your .git folder and reinitialize it, like that :

rm -rf .git
git init
git remote add origin http://yourGithubUrl.com
git pull
  • Wanted to understand why this has been downvoted? Are there any potential challenges that I could run into if I do this? – swati Sep 02 '16 at 19:03
  • Well, i can't see the point either. What you will have with this, is a fresh copy of your project. If you have any doubt, you can duplicate your project folder and test in the new one without any fear – Jérémy Giuseppi Sep 02 '16 at 20:11