How to Delete Git Commits on Github
Link to download Git Bash: https://git-scm.com/downloads
Git commands used in this tutorial:
git checkout branchName
git reset --hard HEAD~2
git push --force
Greetings, in this Git tutorial, you'll learn exactly how to delete Git commits from a GitHub repository using three simple commands: git checkout, git reset --hard, and git push --force. If you've ever made a mistake in your Git history, such as committing broken code, the wrong files, or even private information, this video will show you how to undo those changes and wipe them from your Git history—locally and remotely on GitHub.
Deleting Git commits can be a powerful and sometimes dangerous operation if you're not careful. This tutorial is designed to help you understand how to properly delete recent commits, and how to force push those changes to GitHub safely when needed.
Git commands used in this tutorial:
git checkout branchName
git reset --hard HEAD~2
git push --force
These three commands are all you need to delete the last two commits from your current branch and push the updated history to GitHub. Here's a quick breakdown of what each one does:
git checkout branchName: Switch to the branch where you want to delete commits.
git reset --hard HEAD~2: Permanently delete the last two commits from the current branch.
git push --force: Overwrite the remote history on GitHub with your local changes.
The git reset --hard command is destructive. It not only removes the specified commits but also deletes any uncommitted changes in your working directory. Make sure you’ve backed up anything important before running this command.
Additionally, when you run git push --force, you’re telling GitHub to completely overwrite the remote branch history. If you're working on a shared branch, this can cause serious problems for your teammates by rewriting history they may have already based their work on. Only use --force when you’re certain it's safe to do so.
Please keep in mind that Git will keep commits in the database even if you have deleted them. If you have pushed up sensitive information by mistake, you will need to delete the entire repo. The deleted commits can still be accessible directly via SHA1.
Thanks for watching this tutorial on how to remove git commuts.
If this tutorial on how to delete git commits on your github repo was useful, then be sure to subscribe for more git tutorials.
Here is a playlist of Git tutorials: • Git Tutorials
How to Delete Git Commits on Github