small medium large xlarge

24 Aug 2008, 13:40
Guido Ostkamp (3 posts)


I would like to know whether undoing Git actions is covered in full detail in the book. I mean the following actions:

git add git rm git mv git commit (without having pushed) git commit (with having pushed)

I had a hard time with the ‘git revert’ stuff as it doesn’t seem to work as a beginner would expect or you may need other or even a sequence of commands to undo a change.

In my view, this topic strongly belongs to “Everyday” Git actions.

Thanks for your information.



25 Aug 2008, 22:36
Travis Swicegood (56 posts)

Hey Guido;

Was loading this page when I say an email come in. I hope you don’t mind me just responding here - that way everyone can see it.

Yes, the book does deal with undoing changing. In good Git form, it covers a few different methods of undoing and removing changes, including (off of the top of my head):

  • git revert
  • git reset
  • git rebase

I hope that answers your question.


26 Aug 2008, 19:41
Guido Ostkamp (3 posts)

Hi Travis,

thanks for your reply. I bought the beta to have a closer look at this.

While the writeup regarding the dangers of rewriting history is ok I feel that the everyday topics are not yet covered - well, the book is still beta and plenty of time left to fix this.

I give you some examples:

Most people will never do any partial staging as Git allows. They want just to add new files or modify, rename or remove existing files as a whole in one single step. And they want to undo just that single step (before committing) without throwing away all other staged changes affecting differnt files (and completely going back to an older repository revision).

I give you some example cases:

Change existing file:

$ edit somefile $ git add somefile $ git status # On branch master # Changes to be committed: # (use “git reset HEAD ..." to unstage) # # modified: somefile #

To undo this you need two steps:

$ git reset HEAD somefile $ git checkout somefile

The second step only, if you not only want to unstage but want to completely get rid of the changes, of course.

If you remove a file:

$ git rm somefile $ git status # On branch master # Changes to be committed: # (use “git reset HEAD ..." to unstage) # # deleted: somefile #

$ git reset HEAD somefile $ git checkout somefile

Here the second step is required, otherwise the file would not be recovered.

For moving it even gets more complicated:

$ git mv file1 file2 $ git status # On branch master # Changes to be committed: # (use “git reset HEAD ..." to unstage) # # renamed: file1 -> file2 #

$ git reset file2 $ git reset HEAD file1 $ git checkout file1 $ rm file2

… and I don’t know how complicated it will get to recover a move of an entire directory with multiple files.

I omit the adding of a new file example which also needs to be covered.

Regarding undoing it should be mentioned that “git checkout” undoes local changes that are not yet staged - without a warning!

$ edit somefile $ git checkout somefile

Oops, all changes lost!

I suggest to add such examples to the book including what happened and why.

Best regards


26 Aug 2008, 20:23
Guido Ostkamp (3 posts)

Hi again Travis,

just wanted to inform you that I’ve created an erratum entry for this topic as well.

24 Oct 2008, 15:06
David James (2 posts)

Guido, thanks for sharing that.

I only use git checkout to switch branches. Looking at the man page just now made me realize that it can be used to update “the named paths in the working tree from the index file” as well – which indeed can destroy unstaged changes.

30 Sep 2009, 11:30
Co Lega (1 post)

Good addition, thanks. The examples really clarified.

You must be logged in to comment