From: Oleg Broytman Date: Mon, 29 Jun 2015 19:09:41 +0000 (+0300) Subject: Explain git reflog X-Git-Url: https://git.phdru.name/?a=commitdiff_plain;h=57df7facf669882b7c8402ba53e48aa605654e9b;p=git-wiki.git Explain git reflog --- diff --git a/pep-git.txt b/pep-git.txt index 402dac8..a8be119 100644 --- a/pep-git.txt +++ b/pep-git.txt @@ -440,6 +440,33 @@ about unstaging and other undo tricks. git reflog: reference log ------------------------- +Removing commits with ``git reset`` or moving the head of a branch +sounds dangerous and it is. But there is a way to undo: another +reset back to the original commit. Git doesn't remove commits +immediately; unreferenced commits (in git terminology they are called +"dangling commits") stay in the database for some time (default is two +weeks) so you can reset back to it or create a new branch pointing to +the original commit. + +For every move of a branch's head - with ``git commit``, ``git +checkout``, ``git fetch``, ``git pull``, ``git rebase``, ``git reset`` +and so on - git stores a reference log (reflog for short). For every +move git stores where the head was. Command ``git reflog`` can be used +to view (and manipulate) the log. + +In addition to the moves of the head of every branch git stores the +moves of the HEAD - a symbolic reference that (usually) names the +current branch. HEAD is changed with ``git checkout $BRANCH``. + +By default ``git reflog`` shows the moves of the HEAD, i.e. the +command is equivalent to ``git reflog HEAD``. To show the moves of the +head of a branch use the command ``git reflog $BRANCH``. + +So to undo a ``git reset`` lookup the original commit in ``git +reflog``, verify it with ``git show`` or ``git log`` and run ``git +reset $COMMIT_ID``. Git stores the move of the branch's head in +reflog, so you can undo that undo later again. + git revert: revert a commit ---------------------------