Search code examples
gitrevision-history

How can I efficiently navigate a Git repo to follow project development?


I am trying to understand a relatively new but non-trivial tool being developed in a git repository.

There is not much documentation in the code but the repository has picked up <100 commits so far, and I can see myself understanding what is going on better if I do something like:

  1. checkout the very first commit; look at some code
  2. checkout say ~5-6 commits later; look at how it changed
  3. rinse-and-repeat #2 till I'm up-to-date

.. instead of just looking at all the code at the HEAD of master right now, which is a lot more complex.

The problem with my idea is that once I git checkout $commit_1, I get into a detached head state, so to go "up" to any newer commit I have to git checkout master again, and then work my way down to the commit I want. Is there a more convenient way to do this? i.e. check out an old commit, and then ask git to show me newer commits and move to one of them.


Solution

  • No need to check out master again all the time, or scribble down loads of SHA-1's on a piece of paper. At any stage, you can run

    git log --oneline --decorate --graph master
    

    For convenience, you may want to define the following alias, as many Git users do:

    git config alias.lg "log --oneline --decorate --graph"
    

    Then the command above simply becomes

    git lg master
    

    This will output the log of the entire ancestry (albeit in a concise form) of your master branch, no matter "where you are" in the commit graph.

    It's a nice way to get your bearings and identify which commit you want to check out or inspect next, via its corresponding short SHA-1 hash. Note that using the --decorate flag shows you where you are (HEAD).

    Example

    Here is an example of me navigating one of my own Git repositories:

    $ git branch
    * master
    $ git tag
    v0.1
    v0.2
    v0.3
    $ git checkout v0.3
    Note: checking out 'v0.3'.
    
    You are in 'detached HEAD' state. You can look around, make experimental
    changes and commit them, and you can discard any commits you make in this
    state without impacting any branches by performing another checkout.
    
    If you want to create a new branch to retain commits you create, you may
    do so (now or later) by using -b with the checkout command again. Example:
    
      git checkout -b new_branch_name
    
    HEAD is now at cfc71e4... mention compatibility with subset of Octave syntax
    $ git log --oneline --decorate --graph master
    * 73ec762 (origin/master, origin/HEAD, master) improve brace style
    * ffc8d67 remove superfluous word; delete trailing whitespace
    * 3f8b8db remove obsolete comment about mcode
    * f9f9fd0 remove .DS_Store file that was accidentally added
    * cc855ab clarify description of mlonlyheader option
    * 7553ccf change contact email
    * 4d860e9 correct remarks on shell-escape and backtick
    * 9a2ef02 corrected typo in Tips & tricks
    * f0badb5 minor improvements in documentation
    * cfc71e4 (HEAD, tag: v0.3) mention compatibility with subset of Octave syntax
    * 7db2c88 Preventive bugfix: replace \toks@ by \toks@mlpr
    * 01fdc43 delete OS-specific files from .gitignore
    ...