DEV Community

Cover image for Creating a command which creates a markdown list with (merged) changes for a Git branch
netsi1964 🙏🏻
netsi1964 🙏🏻

Posted on

Creating a command which creates a markdown list with (merged) changes for a Git branch

TLDR

You can create a Bash script that lists the merged changes for a Git branch and formats them as a Markdown list using the git log command and the sed command. Move the script to a directory that's included in your system's PATH environment variable, make it executable, and run it from anywhere in the terminal. Sample use and output are included in this tutorial.

Step-by-Step Guide

Step 1: Create the Bash script

Create a new file and name it list-merged-changes.sh. You can use any text editor of your choice.

Add the following code to the file:

#!/bin/bash

branch=$1

if [ -z "$branch" ]; then
  echo "Branch name is missing."
  exit 1
fi

git log --merges --pretty=format:'* %s' "$branch" | sed 's/_/\\_/g'
Enter fullscreen mode Exit fullscreen mode

This script, uses the --date=short option with the git log command to include the date in the format YYYY-MM-DD for each Git event.

The [%ad] format string is used to specify the date format to be included in the output. This format string will output the date in the format [YYYY-MM-DD].

The sed command is used to escape any underscores in the commit messages.

When you run the script with a branch name as a parameter, it will now print a list of the merged changes for the specified branch, with the date included for each Git event. Here's an example of what the output might look like:

Step 2: Make the script executable

Use the following command to make the script executable:

chmod +x list-merged-changes.sh
Enter fullscreen mode Exit fullscreen mode

Step 3: Move the script to a directory in your system's PATH

Use the following command to move the script to a directory that's included in your system's PATH environment variable:

sudo mv list-merged-changes.sh /usr/local/bin/list-merged-changes
Enter fullscreen mode Exit fullscreen mode

This command moves the script to the /usr/local/bin directory and renames it to list-merged-changes. This directory is typically included in the system's PATH environment variable, so you can run the script from anywhere in the terminal.

Step 4: Test the script

To test the script, run the following command from any directory in the terminal:

list-merged-changes <branch-name>
Enter fullscreen mode Exit fullscreen mode

Replace with the name of the branch that you want to list the merged changes for.

If the script runs successfully, you should see a markdown list of the merged changes for the specified branch.

Sample Use and Output

Let's say you want to list the merged changes for the main branch of a Git repository. You can use the following command:

list-merged-changes main
Enter fullscreen mode Exit fullscreen mode

The output will look something like this:

* [2022-03-15] Merge pull request #123 from feature-branch
* [2022-03-14] Merge branch 'hotfix-branch'
* [2022-03-13] Merge pull request #456 from bugfix-branch
Enter fullscreen mode Exit fullscreen mode

You can then copy and paste this output into a Markdown file or document.

If You Want All Commits

If you want to list all the commits in a specified branch, you can create a new Bash script to automate the process.

Here's how you can create the list-commits.sh script:

  1. Open a text editor and create a new file.
  2. Copy the following code and paste it into the file:
#!/bin/bash

branch=$1

if [ -z "$branch" ]; then
  echo "Branch name is missing."
  exit 1
fi

echo "## Commits in $branch"
echo

git log --date=short --pretty=format:'* [%ad] %s' "$branch" | sed 's/_/\\_/g'
Enter fullscreen mode Exit fullscreen mode
  1. Save the file with the name list-commits.sh.
  2. Make the script executable by running the following command in the terminal:
chmod +x list-commits.sh
Enter fullscreen mode Exit fullscreen mode
  1. Move the script to a directory in your system's PATH by running the following command:
sudo mv list-commits.sh /usr/local/bin/list-commits
Enter fullscreen mode Exit fullscreen mode
  1. Test the script by running the following command in the terminal:
list-commits <branch-name>
Enter fullscreen mode Exit fullscreen mode

Replace <branch-name> with the name of the branch that you want to list the commits for.

When you run the script with a branch name as a parameter, it will print a list of all the commits for the specified branch, with the commit date and message included in the output. Here's an example of what the output might look like:

## Commits in main

* [2022-03-15] Add new feature
* [2022-03-14] Fix bug
* [2022-03-13] Update documentation
* [2022-03-12] Initial commit
Enter fullscreen mode Exit fullscreen mode

You can then copy and paste this output into a Markdown file or document.

With this script, you can automate the process of listing all the commits in a specified branch, which can save you time and effort.

Disclaimer

This post created by ChatGPT by a prompt given by @netsi1964. We hope you found this tutorial helpful!

Top comments (0)