A step-by-step guide to revert a pushed commit using GitHub Actions bot

Hansel
Bina Nusantara IT Division
4 min readMar 30, 2023

GitHub has provided a feature that enables us to automate tasks as a CI/CD platform called GitHub Actions. In this article, I’m going to show how we can set it up for simple tasks, such as reverting a pushed commit.

Photo by Praveen Thirumurugan on Unsplash

Setting up GitHub Actions

To set up GitHub Actions in your project, we would need to create a YAML file inside the folder ‘.github/workflows’, or we could set up the workflow from the actions tab in GitHub.

Photo by Author

In this tab, there are tons of available workflows like building, testing, and deploying our projects. However, since the task that we are going to do is a simple one, we just needed to click on ‘set up a workflow yourself’. This would open up a text editor where we could put in our commands for GitHub actions. For this specific task, we are going to use the following code:

name: Revert pushed commit

on:
workflow_dispatch:
inputs:
branch:
description: 'Branch name'
required: true
default: 'master'

jobs:
revert_commit:
runs-on: ubuntu-latest
steps:
- name: Checkout branch
uses: actions/checkout@v2
with:
ref: ${{ github.event.inputs.branch }}
fetch-depth: 0
- name: Revert pushed commit
run: |
git config --global user.name 'github-actions[bot]'
git config --global user.email 'github-actions[bot]@users.noreply.github.com'
git revert --no-edit HEAD
git push

Working with the YAML file

Now, I’m going to explain what each part of the code does. The ‘name’ part, as it suggests, is going to be the name of the workflow that will be displayed in the Actions tab.

The ‘on’ part is the trigger that will run the workflow itself, this can be set to many things, such as a push to the branch, a pull request that has been opened, or in this case, a manual workflow dispatch that requires ‘branch name’ as an input.

For the jobs, it would contain a list of steps that would be run in the workflow. To revert a commit, we would first need to check out into the branch that was retrieved using the input from workflow_dispatch. Then we would need to enter the GitHub actions bot in the config, so that they can create the commit itself, without needing to enter our credentials in the YAML file. After that, we just need to run a git revert command and push the changes to the branch.

Note: ‘git revert’ would undo the changes and create a new commit after the previous one, but still retain the previous one in the history, whereas ‘git reset’ removes the commit entirely.

Running the workflow

We’re almost done, now we would need to give the GitHub-actions bot permission to write changes to the repository, this can be done by turning on the following setting.

Photo by Author

Finally, we can revert our previous commit by running the workflow and entering the branch name in the actions tab.

Photo by Author

After running the workflow, the GitHub-actions bot would create a commit that undid all of the changes from the last commit made. However, you would still need to do a ‘git pull’ to get the reverted changes on your local repository.

Photo by Author

Afterthoughts

This article is only a quick introduction to how to set up GitHub Actions. With this powerful tool, there are lots of other uses that could help us in our development process. You could explore more things by tinkering with the YAML file yourself or look for other workflows in the market and hopefully learn something new.

--

--