If you accidentally committed something you shouldn’t have, and pushed it to Github, there are still ways to delete or modify it. Usually, you don’t want to mess with Git’s history, but in some cases it can be safe when done properly.
Don’t Do This If You Can Avoid It
Removing commits from Git’s history is generally a bad idea. Git is meant to track every version of your files, and there’s always alternatives to deletion, like git revert, that will keep the history intact.
Once it’s on a remote repository, like Github, it’s much harder to get rid of the commit entirely. This is because anyone else using the repo will have their own local copy of that repo, and forcibly deleting a commit will make them go out of sync.
There are still a few reasons to do this though—maybe you accidentally committed private data to a public repo, which would still be visible in the history no matter what you do. It’ll cause some issues for your coworkers, but even in this case it’s possible with a force push.
Fixing Commits Locally
If you’re working locally, and you haven’t pushed your changes to Github yet, you can safely reset or modify commits that aren’t on the remote. Git tracks your local directory, stages changes internally, and keeps a Git history. This is all kept in sync with the remote, but only when running
push/pull. Github doesn’t care about the state of your repo until you push.
If you just made a mistake when committing files, you can do a soft reset and make a new commit. This is actually commonplace and useful if you want to merge multiple commits into one, called “squashing.” If you wanted to completely discard the changes, you can do that as well (with a
If you haven’t pushed to Github yet, you can revert the most recent commit with a soft reset, which doesn’t effect your local files at all, but makes it so that you never committed the changes to Git in the first place.
git reset --soft HEAD~
If you just want to add a new file/change to the most recent commit, Git has a flag built in to do exactly that:
git commit --amend --no-edit
Reverting (Github Safe)
If you want to undo the changes from a commit you already pushed to Github, the safe way to do that is with a revert. Reverting will generate an “opposite commit,” which will basically undo all the changes from a specific commit. If you added a line, it’s removed, and if you deleted a file, it’s added back.
This is safe because it doesn’t change the history, only adds new history on top of it. You can push the revert commit to Github, and have your coworkers run
git pull to get the updates.
git log to get a list of commits:
Then, copy the SHA1 hash and revert the commit:
git revert 62ff517cc7c358eaf0bffdebbbe1b38dea92ba0f
Force Reset (Unsafe)
If you really want to remove a commit, the method to do that is to remove it locally, and then force push to Github. Since this is very dangerous and can mess up your coworker’s local repositories, if you still want to, you may have to disable force push branch protection in Github’s repository settings:
Then, you can remove the commit locally, which is easiest if it’s the latest commit:
git reset --soft HEAD~
You can also do an interactive rebase, which is useful if the commit isn’t the most recent one. If the commit was, for example, 12 commits ago, you can rebase from then, remove the offending commit, and save.
git rebase -i HEAD~12
Once your local repo is in working order, you can force push to Github.
git push origin master --force
Make sure your repo is up to date! If the remote branch has changes that you don’t, they will be overwritten by this push.
You may want to coordinate this with your team members, as you may have to ping them and tell them to run a fetch and reset:
git fetch git reset origin/master --soft
Moving a Commit to a Different Branch
If you accidentally committed to the wrong branch, and want to remove the commit and move it to the correct branch, there are tools in Git to handle that. You can read our guide to moving commits around to learn more, but the same principles apply—if you need to completely remove a commit, you must force reset. If you’re fine with reverting, you can simply revert and cherry-pick.
- › What Are the Best Nintendo Switch Games in 2022?
- › NZXT Signal 4K30 Capture Card Review: Lossless High-Quality Footage
- › The 10 Best Netflix Original Movies in 2022
- › T-Mobile Is Selling Your App Activity: Here’s How to Opt Out
- › What’s New in Windows 11’s 22H2 Update: Top 10 New Features
- › A World Without Wires: 25 Years of Wi-Fi