NullFound

Staging a Git Revert Pull Request

The Why

When dealing with a risky feature branch change, you may want to have a revert PR already up and ready to merge just in case things get thorny after you deploy. This is useful if you need a lot of folks to sign off on a merge into your repo, or your CI can take some time to run. Having a revert ready to go can give you more confidence to ship.

I searched high and low for a way to do this. A co-worker of mine told me how and I thought I'd share the technique here in case others find it interesting.

For the sake of our example, let's assume we have a repo with a main branch that is production and our feature branch called baseball.

The Workflow

First we need to be on our feature branch, baseball, so let's check it out.

git checkout baseball

Now let's create a new branch off of our feature branch that we'll use for our revert. The name doesn't matter. Call it whatever you want. We'll call ours baseball-revert.

git checkout -B baseball-revert

Then we'll create our patch file. You can also download the patch file from Github if you've created a PR for your feature branch.

git diff origin/main...baseball > baseball.patch

A patch file is a file that stores the diff between files. Here is an example of a patch from this blog post.

/var/folder/staging-a-git-revert-pull-request.md content/posts/staging-a-git-revert-pull-request.md
      25 git checkout -B baseball-rever       25 git checkout -B baseball-rever
t                                       t
      26 ```                                  26 ```
      27                                      27
      28 Then we'll create our patch fi       28 Then we'll create our patch fi
le. You can also download the           le. You can also download the
patch file from Github if you'          patch file from Github if you'
ve created a PR for your featu          ve created a PR for your featu
re branch.                              re branch.
      29                                      29
                                              30 A patch file is a file that st
                                        ores the diff between files.
                                              31
      30 ```                                  32 ```
      31 git diff origin/main...basebal       33 git diff origin/main...basebal
l > baseball.patch                      l > baseball.patch
      32 ```                                  34 ```
      33                                      35
      34 With our patch file created we       36 With our patch file created we
 can apply it using `git apply           can apply it using `git apply
`, but we need to apply it in           `, but we need to apply it in
reverse.                                reverse.

With our patch file created we can apply it using git apply, but we need to apply it in reverse.

git apply --reverse baseball.patch

Remove the patch file now that we've applied the changes to our baseball-revert branch.

rm baseball.patch

That's it! Commit your changes and you can push it up and create a pull request. You'll want to base the branch off of your feature branch in Github so folks can review your changes.

git push origin baseball-revert

Happy shipping! 🥳

#software