Profile photo

from miller import blake

Hello, I'm a Seattle-based site reliabililty engineering manager, I get paid to do what I love, I like Python, I'm in an abusive relationship with JavaScript, I'm a fan of good design, and I don't think things always have to be so stupid.
You can follow me @bltmiller, subscribe via RSS, and email me.

Remove or Change Content in an Already Pushed Commit to a Git Server

Rebasing with git is an extremely powerful and intimidating tool both new users and even those more familiar with basic git concepts. It can work across multiple branches and tags to massage complex commit structures, but the most common and useful feature of rebase is to delete or modify content in a single commit that has been pushed to a public-facing server (e.g. GitHub).

Getting started

First, make sure your local repository is up-to-date (you may also want to copy your local repo to a temporary backup location elsewhere on your local machine):

git pull origin master

If the pull didn’t work, make sure you are already working in a git repository. If not, clone the repo before anything else. There are plenty of tutorials available for basic git functions.

Rebasing

Find the commit in which you would like to change or remove content, either by using git log or by browsing GitHub (or equivalent GUI). For example, I’d like to remove content in the commit with hash f18fae74e91cd6eab48efa82bfed71b20b6ef93c:

git rebase -i f18fae7

Only the first 7 characters of the SHA-1 hash are necessary in most cases.

This will interactively (using your default text editor, e.g. vim) present you with a list of commits, in reverse chronological order. Note that there are some commented lines with information about different options available, such as reword, squash, or fixup. Their respective uses are self-explanatory, but right now we only care about edit. Select the line with your commit that you’d like to amend. The line will look something like:

pick f18fae7 saving some time here by hard-coding in ssh credentials with access to a private server

pick is the default action, f18fae7 is the commit followed by the original commit message. Delete pick and in its place, type edit or ‘e’. It should now look like:

edit f18fae7 saving some time here by hard-coding in ssh credentials with access to a private server

Save and exit (e.g. :wq in vim). A quick git status will show you what you’ve done so far. Open up the relevant file and see your fatal error which needs to be removed or changed. Make your edits and save. I’ll wait here.

Done? Okay. Now your changes are saved and you need to tell git what to do. Add the file to be committed, commit it as an amendment with a comment, and continue the rebase:

git add path/to/file.py
git commit --amend -m 'removed private data. do not place private credentials in git for the world to see.'
git rebase --continue

Push your changes

To reflect your changes on your git server, force a push:

git push --force origin master

Browse the GitHub (or equivalent) repo and the sensitive information should no longer exist anywhere in the commit history.


comments powered by Disqus

Copyright © 2020, Blake Miller. All rights reserved. | Sitemap · RSS