1

I sometimes screw up my local git repo so badly that I need to just wipe it out and start again from the latest commit on remote. Is there a correct way to do this with git, or should I just delete the local repo and pull again? I tried the answer to this post, but my local repo was still detached from HEAD.

Here's one example to prevent anyone from asking why I want to do this instead of fixing my mistake. I somehow ended up with a detached HEAD, and other SO posts indicate it's a PITA to fix. My latest commit on remote is fine, I've only changed one file, so I saved my changes and just want to start fresh instead of trying to fix the detached HEAD.

vaindil
  • 7,536
  • 21
  • 68
  • 127
  • after few commits or only with some local change? – rajuGT Nov 17 '15 at 21:16
  • @rajuGT I don't need to save anything local. It can all be wiped out and I'll just pull the latest commit info from `remote`. – vaindil Nov 17 '15 at 21:17
  • 1
    Detached HEAD isn't that hard to fix...`git checkout ` and you're pretty well sorted, provided you haven't *really* gone off the reservation and made commits in detached HEAD mode... – Makoto Nov 17 '15 at 21:26
  • @Makoto I made no commits, but checking out was what detached HEAD in the first place. – vaindil Nov 17 '15 at 21:27
  • If you checked out a SHA or a tag, sure, that's exactly what happens. If you check out a branch, Git knows where it needs to go in the hierarchy so there's no detaching of HEAD. – Makoto Nov 17 '15 at 21:30
  • @Makoto is right, a "detached HEAD" is really quite trivial and normal. It's sometimes portrayed as scary and difficult, I think mainly to make sure people don't do a bunch of work in that mode and then not know how to retain the work. But ultimately all it means is that you're not on any branch. – torek Nov 17 '15 at 22:45

1 Answers1

1

Assuming that by "the latest commit on remote" you mean on origin/master, you can easily reset to that state with the following command:

git reset --hard origin/master

Or you can simply reset all local changes since the last local commit by doing:

git reset --hard

Additionally, you may want to experiment with the git clean command to clear away any untracked local files. git clean won't undo your changes, you need git reset --hard for that.

mkrufky
  • 3,268
  • 2
  • 17
  • 37