0

I'm beginning with Git and Magit so I don't know to which one my question is specific.

Sometimes I save changes to a file but then don't want to commit them: I simply want to go back to the last commit.

I'm trying to do this from Emacs / Magit and what I do is just insane:

  1. I stash the changes (otherwise Magit complains about uncommited/unstashed changes)
  2. I checkout a commit older than the last one (if I simply try the last one Emacs / magit just leaves my changes there: it doesn't modify the file)
  3. I drop the stash
  4. I checkout the last commit

This requires lots of keypresses and is absolutely mad.

I'm surely missing something totally obvious but simply can't find what: so I do efficiently drop from Emacs / Magit the last saved changes and simply go back to the last commit?

Cedric Martin
  • 5,945
  • 4
  • 34
  • 66

2 Answers2

1

I'm not really familiar with Emacs / Magit, but I can tell you the equivalent git commands you could run from your shell.

If I understood your question correctly, I believe you're looking for a simple way to revert all changed files in your current working tree. If that not be the case, do not follow these steps ;)

  • If you just wish to discard local changes in your working tree for a file without touching the index, you could do this:

    git checkout -- <FILE>
    
  • If you want to discard all local changes without touching the index, you could run this from the root of your repo:

    git checkout -- .
    
  • If you wish to discard all changes in the index as well as the working tree, and want to move your working tree and the index to the state just after a checkout, you can use the git reset --hard command. (Be aware any uncommitted changes in your working tree and even uncommitted changes in your index/staging area will be lost).

    git reset --hard HEAD
    

    The HEAD in the above command is optional BTW.

Tuxdude
  • 47,485
  • 15
  • 109
  • 110
  • You could also go `git checkout -- file` to just get back that file. – vonbrand Mar 26 '13 at 19:30
  • @vonbrand - I already had it as the first item in the list, just didn't word it clearly ;) – Tuxdude Mar 26 '13 at 19:31
  • Is there some way to be able to run this from within magit mode? That's what the question is asking, not how to shell out. – Shai Jun 15 '15 at 21:04
1

in magit k is to drop things, so if you want to drop a change, you just have to go on the change you want to revert, and use k on it.

You could also use X to revert every change in your working tree (beware, no undo for this)

Rémi
  • 8,172
  • 2
  • 27
  • 32