Creating a command which creates a markdown list with (merged) changes for a Git branch
Tags: git, markdown, bash, terminal
Published: 2023-02-14 15:29:00

---
title: Creating a command which creates a markdown list with  (merged) changes for a Git branch
published: true
description: This tutorial will show you how to create a Bash script that lists the merged changes for a Git branch and formats them as a Markdown list. You will learn how to create the script, make it executable, and run it from anywhere in the terminal. Sample use and output are included to help you understand the process.
tags: Git, Markdown, Bash, Terminal
cover_image: https://dev-to-uploads.s3.amazonaws.com/uploads/articles/vbl1ib3uwvgc1k1izlqa.jpg
# Use a ratio of 100:42 for best results.
published_at: 2023-02-14 15:29 +0000
---

### 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'
```

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
```

### 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
```

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>
```

Replace <branch-name> 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
```
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
```

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'
```

3. Save the file with the name `list-commits.sh`.
4. Make the script executable by running the following command in the terminal:

```
chmod +x list-commits.sh
```

5. 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
```

6. Test the script by running the following command in the terminal:

```
list-commits <branch-name>
```

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
```
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](https://chat.openai.com/chat) by a prompt given by [@netsi1964](https://twitter.com/netsi1964). We hope you found this tutorial helpful!