Return to Video

Reflog • GitHub & Git Foundations

  • 0:00 - 0:03
    Git tracks all of the revisions
    that you make to your software.
  • 0:04 - 0:05
    But did you know that with reflog
  • 0:05 - 0:09
    it also tracks the revisions
    to your revisions?
  • 0:09 - 0:12
    ♪ (smooth music) ♪
  • 0:15 - 0:17
    Users of Git first discover
  • 0:17 - 0:19
    that every commit
    they make in a repository
  • 0:19 - 0:21
    is a recorded snapshot in time
  • 0:21 - 0:24
    that shows how
    the code base is progressing,
  • 0:24 - 0:27
    but more advanced users of Git
    discover that the reflog
  • 0:27 - 0:29
    is keeping track of commits that are made,
  • 0:30 - 0:32
    as well as commits that are discarded.
  • 0:32 - 0:35
    This provides a rolling buffer of 30 days
  • 0:35 - 0:37
    in which you can recover
    from any mistake,
  • 0:37 - 0:40
    including detrimental ones
    with the git reset command,
  • 0:40 - 0:42
    the deletion of a branch,
  • 0:42 - 0:44
    or possibly a rebase gone awry.
  • 0:45 - 0:46
    ♪ (music) ♪
  • 0:47 - 0:50
    Git reset is specific
    to each and every branch.
  • 0:50 - 0:52
    You'll notice, if you chose to inspect,
  • 0:52 - 0:54
    that in the .git folder
  • 0:54 - 0:56
    there's a subdirectory called logs.
  • 0:56 - 0:59
    Beneath there is a file
    specific to every branch
  • 0:59 - 1:02
    that you have on your local repository.
  • 1:03 - 1:06
    Each of those logs is keeping track
    of the changes that you make,
  • 1:06 - 1:09
    both forward and backwards in direction.
  • 1:09 - 1:15
    The user interface to this
    is the git reflog command.
  • 1:16 - 1:18
    It outputs the most recent history.
  • 1:19 - 1:22
    A paging mechanism allows you
    to go through older entries
  • 1:22 - 1:26
    that have transpired on the branch
    that you're currently checked out to.
  • 1:26 - 1:29
    Once you've run git reflog
    for the first time
  • 1:29 - 1:32
    and seen the historical events
    that displays on screen,
  • 1:32 - 1:34
    you wonder what can you do with those.
  • 1:34 - 1:39
    Simply grab one of the seven
    character hashes that are displayed
  • 1:39 - 1:43
    and use that as a potential candidate
    for restoring your code base.
  • 1:44 - 1:48
    git reset --hard
    to that hash.
  • 1:48 - 1:51
    The current branch
    is now forcefully switched
  • 1:51 - 1:53
    back to that historical point.
  • 1:54 - 1:55
    ♪ (music) ♪
  • 1:55 - 1:58
    The linear history
    that git reflog provides
  • 1:58 - 2:00
    can be difficult to navigate.
  • 2:00 - 2:02
    Which one is an orphan branch?
  • 2:02 - 2:05
    Which one is a commit
    that's no longer part of this code base?
  • 2:06 - 2:09
    A little bit of command line
    hackery and creativity
  • 2:09 - 2:13
    can pipe the result of reflog
    over into a graphical user interface
  • 2:13 - 2:15
    such as gitk,
  • 2:15 - 2:19
    thus making it easy to see
    which branches are out on their own,
  • 2:19 - 2:22
    which commits are no longer
    part of this branch,
  • 2:22 - 2:24
    and which things
    you might want to recover
  • 2:24 - 2:26
    by capturing their hash
  • 2:26 - 2:29
    and using the reset --hard
    as described a minute ago.
  • 2:29 - 2:31
    ♪ (music) ♪
  • 2:31 - 2:35
    Git reflog is your motivation
    to make frequent commits.
  • 2:35 - 2:39
    Making frequent commits
    means there's history stored in reflog
  • 2:39 - 2:43
    whether or not you reset them away,
    discard a commit,
  • 2:43 - 2:45
    potentially amend it, and make a mistake.
  • 2:46 - 2:49
    Everything that's not committed,
    either in the working directory
  • 2:49 - 2:52
    or the staging area is at risk,
  • 2:52 - 2:55
    but if it's committed, you can recover it.
  • 2:55 - 2:58
    Those last 30 days
    have an insurance policy
  • 2:58 - 3:01
    and allow you to take big, bold steps
  • 3:01 - 3:03
    with the use of other Git features.
  • 3:03 - 3:05
    ♪ (music) ♪
  • 3:05 - 3:07
    Thanks for watching this episode
    of Git and GitHub Foundations
  • 3:07 - 3:09
    on the reflog.
  • 3:09 - 3:11
    Be sure to subscribe
    to our channel over here,
  • 3:11 - 3:13
    or if you have questions
    on how to use that effectively,
  • 3:13 - 3:15
    ask those down below.
  • 3:15 - 3:18
    There are related episodes
    such as the one on reset
  • 3:18 - 3:20
    that really are helpful to watch
  • 3:20 - 3:23
    in concert with what
    you've just learned on reflog.
  • 3:23 - 3:25
    ♪ (music) ♪
Title:
Reflog • GitHub & Git Foundations
Video Language:
English
Team:
GitHub
Project:
Git and GitHub Foundations
Duration:
03:29
There has been no activity on this language so far.

English subtitles

Revisions Compare revisions