0

I did a git undo which was not at all what I wanted. Any now I wonder how to undo the undo AKA redo.

I case you wonder what I wanted to do: I wanted to abandon the local changed (they did not work as expected) and revert to the last committed state.

Update for question 1

A git reset HEAD . leaves me with:

Auf Branch master
Ihr Branch ist vor 'origin/master' um 2 Commits.
  (benutzen Sie "git push", um lokale Commits zu publizieren)
Änderungen, die nicht zum Commit vorgemerkt sind:
  (benutzen Sie "git add/rm <Datei>...", um die Änderungen zum Commit vorzumerken)
  (benutzen Sie "git checkout -- <Datei>...", um die Änderungen im Arbeitsverzeichnis zu verwerfen)

    gelöscht:       Pictures/KETTLER_Logo.svg.png
    gelöscht:       Pictures/Withing_Logo.png
    gelöscht:       app/src/main/java/com/krischik/fit_import/IMainActivity.java
    gelöscht:       app/src/main/java/com/krischik/fit_import/IMainFragment.java
    geändert:       app/src/main/java/com/krischik/fit_import/MainActivity.java
    geändert:       app/src/main/java/com/krischik/fit_import/MainFragment.java
    geändert:       app/src/main/kotlin/com.krischik/fit_import/GoogleFit.kt
    geändert:       app/src/main/res/layout/main_fragment.xml
    gelöscht:       app/src/main/res/menu/main_menu.xml
    geändert:       app/src/main/res/values/dimens.xml
    geändert:       app/src/main/res/values/strings.xml
    geändert:       app/src/main/res/values/styles.xml
    geändert:       lib/src/main/kotlin/com.krischik/fit_import/Ketfit.kt
    geändert:       lib/src/main/kotlin/com.krischik/fit_import/Withings.kt
    geändert:       lib/src/test/kotlin/com.krischik/fit_import/Ketfit_Test.kt
    geändert:       lib/src/test/kotlin/com.krischik/fit_import/Withings_Test.kt
    geändert:       src/main/scripts/Create-IC-Launcher.scala

Unversionierte Dateien:
  (benutzen Sie "git add <Datei>...", um die Änderungen zum Commit vorzumerken)

    app/src/main/res/anim-v21/
    app/src/main/res/anim/
    app/src/main/res/drawable-xxxhdpi/
    app/src/main/res/values-de/
    app/src/main/res/values-v21/

keine Änderungen zum Commit vorgemerkt (benutzen Sie "git add" und/oder "git commit -a")

Obviously reverting to the last committed state should have an clean git status

Update for possible duplication

I don't know how anybody could have had the idea that there was a possible duplicate because the operations in question have nothing in common:

  • git reset --hard HEAD changes the work copy to reflect the repository.
  • git undo changes the repository to undo the last commit.

So while git reset is part of the answer it has nothing to do with the problem.

Martin
  • 11,577
  • 16
  • 80
  • 110
  • See update to question. With the `git reset HEAD .` I am left with lots of add removes and changes. – Martin Jan 11 '16 at 17:32
  • `git checkout -- .` will reset all files to the `HEAD` version. – Holloway Jan 11 '16 at 17:33
  • To remove unversioned files, use [`git clean`](http://git-scm.com/docs/git-clean) – Holloway Jan 11 '16 at 17:35
  • @wRAR `info git-undo` or `git help undo` will tell you ;-) — a very dangerous command as I just found out. – Martin Jan 11 '16 at 17:39
  • Possible duplicate of [git reset --hard HEAD leaves untracked files behind](http://stackoverflow.com/questions/4327708/git-reset-hard-head-leaves-untracked-files-behind) – Andrew C Jan 11 '16 at 17:49
  • @Martin I don't have such command, that's why I've asked – wRAR Jan 11 '16 at 17:54
  • @wRAR I have **git version 2.6.4** and command undos the commit itself. And it is a bit more then just moving HEAD. – Martin Jan 11 '16 at 18:29
  • Found it, it's a part of http://github.com/visionmedia/git-extras – wRAR Jan 12 '16 at 04:30

1 Answers1

2

You have several options:

The most easy one is to use the git reflog and to checkout the latest "good" commit that you was on.

You can also use any of those to get back to any desired commit.

First all what is HEAD?

HEAD is a simply a reference to the current commit (latest) in the current branch.
There can only be 1 HEAD at any given time.

If you are not on the latest commit - meaning that HEAD is point to a prior commit in history its called detached HEAD.

enter image description here

Few options:

git checkout

git checkout <commit_id>

git reflog

You can always use the reflog as well

git reflog
git checkout HEAD@{...}

This will get you back to your desired commit

enter image description here


git reset HEAD --hard <commit_id>

"Move" your head back to the desired commit.

# This will destroy any local modifications.
# Don't do it if you have uncommitted work you want to keep.
git reset --hard 0d1d7fc32

# Alternatively, if there's work to keep:
git stash
git reset --hard 0d1d7fc32
git stash pop
# This saves the modifications, then reapplies that patch after resetting.
# You could get merge conflicts, if you've modified things which were
# changed since the commit you reset to.
  • Note: (Since Git 2.7)
    you can also use the git rebase --no-autostash as well.

git checkout

git checkout -b <new branch> <commit_id>
git checkout HEAD~X // x is the number of commits t go back

This will checkout new branch pointing to the desired commit


Here is a general schema of what can be done.

enter image description here

Community
  • 1
  • 1
CodeWizard
  • 128,036
  • 21
  • 144
  • 167
  • Perfect answer. Found the right commit with `reflog` did the `checkout` and the `reset` run unit tests and everything is OK again – Martin Jan 11 '16 at 18:25