1

Using Mercurial's hg revert -aC, or with long option names, hg revert --all --no-backup I can reset my working copy to head.

I.E., which ever branch I'm on, it'll update the working copy to be the latest commit on that branch that the local repo contains.

In particular:

  • If I have commits in my local repository, it definitely won't discard those commits.
  • It will remove untracked files that are not ignored files.
  • It will leave ignored files alone.

I find this to be one of the most essential basic commands for working with a repo. I'll often make some exploratory changes to a few files and then want to abandon this to do begin real work from a pristine set.

How do I do that this git?


This is logged as a duplicate of How do I use 'git reset --hard HEAD' to revert to a previous commit?. That question (and questions it links to) has answers that are a useful resource to answer this question. However, this question is specifically asking how a mercurial user should achieve part of their work flow using git instead.

Community
  • 1
  • 1
Benjohn
  • 13,228
  • 9
  • 65
  • 127
  • 1
    Possible duplicate of [How do I use 'git reset --hard HEAD' to revert to a previous commit?](http://stackoverflow.com/questions/9529078/how-do-i-use-git-reset-hard-head-to-revert-to-a-previous-commit) – Tim Oct 20 '15 at 09:53
  • @TimCastelijns Thanks – I'll take a look. I don't _think_ this is a duplicate: I'm very much wanting to ask the question from the point of view of a mercurial user looking for the git equivalent, rather than as a git user querying the exact usage of a command. I suspect that there almost certainly is a good description of how to achieve this specific effect in an existing SO answer though. A good answer for this question might just be a précis and a link to an existing answer. – Benjohn Oct 20 '15 at 10:31
  • Hi @TimCastelijns Thanks, I believe my question is different from the linked one – though a link from there takes me to [an answer](http://stackoverflow.com/a/12049323/2547229) that would work well for this question, so I think I can self answer. I don't think that I can edit this question to make it more clear that this isn't a duplicate though :-) If you have suggestions, I'll definitely consider them! – Benjohn Oct 20 '15 at 10:40
  • 1
    @TimCastelijns I added a footnote to clarify why the question isn't a duplicate. – Benjohn Oct 20 '15 at 11:05

1 Answers1

2

To reset your working copy to head:

git reset --hard HEAD

To remove untracked files, but not ignored files:

git clean -f

WARNING: These commands will permanently revert all uncommitted changes and remove all untracked files!

However, used in this way, they will preserve any local commits that you have not yet pushed.

sergej
  • 17,147
  • 6
  • 52
  • 89
  • Thanks! Could you clarify two things: 1. Will either of these (ever) remove commits I have made that are currently only local. 2. Will either of these remove *ignored* files? I definitely want to keep ignored files. – Benjohn Oct 20 '15 at 14:35
  • 1
    @Benjohn 1. No. Local commits will not be removed. 2. No. *Ignored* files will not be removed. You can use `git clean -n` to see what will be done. – sergej Oct 20 '15 at 16:48
  • I'll edit to include that information, if that's okay with you? – Benjohn Oct 21 '15 at 08:21