How to use git to reproduce the steps of a tutorial

Viewed 67

I have a project with a serie de commits. This project is a tutorial, and every commit represents a little step in the tutorial. It is possible to start with the first commit and "play" the next commits, one by one, in order to explain the tutorial to an audience?

If I do git log, then I get the following output:

   $ git log
    commit a867b4af366350be2e7c21b8de9cc6504678a61b`
    Author: Me <me@me.com>
    Date:   Thu Nov 4 18:59:41 2010 -0400
    
    Third step of the tutorial, blah blah blah...
    
    commit 25eee4caef46ae64aa08e8ab3f988bc917ee1ce4
    Author: Me <me@me.com>
    Date:   Thu Nov 4 05:13:39 2010 -0400
    
    Second step of the tutorial, more blah blah blah...
    
    commit 0766c053c0ea2035e90f504928f8df3c9363b8bd
    Author: Me <me@me.com>
    Date:   Thu Nov 4 00:55:06 2010 -0400
    
    First step of the tutorial,  blah blah...

Edit: I know that I can use checkout to go to the each commit. Once I am on the first commit, there is an easy command (apart of checkout 25eee4c) to advance without need to know the hash of the next commit?

1 Answers

If you want to interact with the content of the repository at each stage, you could do something like this:

old_head=$(git name-rev --name-only HEAD)
for commit in $(git rev-list --reverse HEAD); do
    git checkout -q $commit
    git show --quiet
    PS1="${commit:0:7}> " bash --norc || break
done
git checkout -q $old_head

Here, we use git rev-list --reverse HEAD to get the list of commits in reverse order (earliest first). Then for each commit we (a) checkout the commit, (b) display the log message, and (c) start a shell with the commit id as the prompt.

Exit the shell (type exit or CTRL-D) to continue to the next commit, or exit 1 to abort the process.

Related