Skip to content

Instantly share code, notes, and snippets.

@nishantrpai
Last active July 15, 2023 07:12
Show Gist options
  • Save nishantrpai/28163e22b19a4f311a66d0126de9d8b6 to your computer and use it in GitHub Desktop.
Save nishantrpai/28163e22b19a4f311a66d0126de9d8b6 to your computer and use it in GitHub Desktop.
If you want to replay git commits this script can help you. Useful for documentation purposes.

Replay all git commits

The intention of writing this script is to help lookback at work and share like https://twitter.com/PaiNishant/status/1680105589513543682

How to use:

  1. Save script
  2. Open the terminal
  3. Go to repo
  4. Run /path/replay.sh abcd123

if you do /path/replay.sh without params it'll run from the beginning

#!/bin/bash

# Usage: replay.sh [start_commit]

START_COMMIT=$1
i=0
head=$(git rev-parse HEAD)

if [[ -n $START_COMMIT ]]; then
    # Checkout the starting commit
    git checkout "$START_COMMIT"
fi

# Take 5 seconds to prepare
echo "Preparing to replay commits in 5 seconds"
sleep 5

for commit in $(git log --reverse --pretty=format:%H)
do
    if [[ -n $START_COMMIT && $commit == $START_COMMIT ]]; then
        START_COMMIT=""
    fi

    if [[ -z $START_COMMIT ]]; then
        echo "Replaying commit $commit"
        git checkout "$commit"
        i=$((i+1))
        echo "Commit $i of $(git rev-list --count HEAD)"
        # If any data takes 5 seconds to load
        sleep 10
    fi
done

# Go back to the HEAD of the current branch
git checkout "$head"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment