If you've ever pushed multiple commits to your GitHub repository and realized you need to delete some (or all) of them, you're not alone. While GitHub's web interface doesn't allow you to delete commits directly, you can easily manage commit history using Git locally. In this guide, we'll walk you through how to delete up to 20 commits from your GitHub repository.
Why Delete Commits?
Deleting commits might sound drastic, but there are several good reasons why you might want to remove them:
-
Accidental commits: Maybe you pushed commits with sensitive information or unfinished work.
-
Cleaning up history: Perhaps your commit history is messy, and you want to streamline it.
-
Reverting changes: If you made a series of commits that you no longer want, this guide will help you delete them.
No matter the reason, here's a simple way to delete multiple commits using Git on your local machine.
Step 1: Clone Your Repository to Your Local Machine
The first step is to clone your repository to your local computer. Open a terminal (or Git Bash) and run the following command:
Replace username
and projectname
with your actual GitHub username and repository name.
Step 2: Identify the Commit You Want to Go Back To
Now that you've cloned your repo, you need to figure out which commit you want to keep as the latest.
Run this command to see the commit history:
This will show a list of commits with a short hash and commit message:
To delete 20 commits, count backward and find the commit hash you want to keep. This is the commit to which you'll reset your branch. For example, if you want to keep the commit ghi9012
, take note of that hash.
Step 3: Use git reset
to Go Back to That Commit
Once you've identified the commit you want to keep, reset your branch to that point. Replace abc1234
with the commit hash from Step 2:
This command will:
-
Reset your local branch to that commit.
-
Remove all commits after that point (including the 20 commits you want to delete).
Step 4: Push the Changes to GitHub (Force Push)
Since you're rewriting history, you'll need to force-push the changes to GitHub. This is necessary because GitHub will see that your local history no longer matches the remote history.
Run the following command to push your changes:
Note: Replace main
with the name of your branch if it's different.
Step 5: Verify the Changes on GitHub
Once the push is complete, go back to your GitHub repository and check the commit history. You should now see the state of the repository from the commit you reset to, and all the commits after that (including the 20 you wanted to delete) will be gone.
Important Considerations
-
Force Push Warning: Force-pushing rewrites history, so it can disrupt other collaborators who might have already pulled the original commits. Always let your team know if you're doing this on a shared branch.
-
Backup: It’s a good idea to back up your changes before resetting the history, just in case you need to restore any commits later.
-
Collaboration: If other people are working on the same branch, they will need to re-sync their local branches with the rewritten history.
COMMENTS