From 002480c60db644a29e1f220ad29db9b046e24947 Mon Sep 17 00:00:00 2001 From: Silas Santini <70163606+pancakereport@users.noreply.github.com> Date: Wed, 7 May 2025 14:46:27 -0700 Subject: [PATCH] instructions for removing commits --- using-github/licenses.md | 2 +- using-github/oh-no.md | 100 +++++++++++++++++++++++++++++++++++++++ 2 files changed, 101 insertions(+), 1 deletion(-) create mode 100644 using-github/oh-no.md diff --git a/using-github/licenses.md b/using-github/licenses.md index 757f8c7..03fe3b3 100644 --- a/using-github/licenses.md +++ b/using-github/licenses.md @@ -2,7 +2,7 @@ layout: page title: Licensing parent: Using GitHub -nav_order: 3 +nav_order: 4 --- # Please, Please, Please, License Your Public GitHub Repositories diff --git a/using-github/oh-no.md b/using-github/oh-no.md new file mode 100644 index 0000000..c2a0825 --- /dev/null +++ b/using-github/oh-no.md @@ -0,0 +1,100 @@ +--- +layout: page +title: Remove Commits +parent: Using GitHub +nav_order: 3 +--- + +# Oh No! I committed solutions to a public repository and pushed to GitHub + +{: .warning} +Be careful running the following commands. These are DESTRUCTIVE actions. 🌋 + +## Remove all commits up to a certain point + +1. Find the last "good" commit hash by running: + + ``` + git log + ``` + + The output should have entries that look something like + + ``` + commit a3cf8b9261a52a8c34098e5316278ce52e40a9b5 + Author: Cal-CS-61A-Staff + Date: Wed Mar 26 20:41:07 2025 +0000 + ``` + +2. Take the commit hash (above `a3cf8b9261a52a8c34098e5316278ce52e40a9b5`) and run the following locally: + + ``` + git reset --hard + ``` + + If you'd like to remove only the latest commit you can run the following instead: + + ``` + git reset --hard HEAD~1 + ``` + +3. Reflect your changes on the remote (public GitHub repo): + + ``` + git push origin HEAD --force + ``` + + +## Remove only specific commits + +If you have a mix of "good" and "bad" commits, you'll want to interactively rebase. [Read more about rebasing.](https://www.atlassian.com/git/tutorials/rewriting-history/git-rebase) + +1. Identify the last commit hash after all all of the commits that you want to remove. The following commands may be useful for this: + ``` + git log + git reflog + ``` + +2. Start the interactive rebase using that hash: + + ``` + git rebase -i + ``` + +3. Identify the commits that you want to remove and remove them in the edit screen. + +4. Resolve any conflicts. + +5. In the edit screen, save and exit. + +6. End the rebase by running: + + ``` + git rebase --continue + ``` + + If you messed up the rebase, here's your options: + + * Abort the rebase without resetting HEAD to the original branch ("clean up" the rebase): + + ``` + git rebase --quit + ``` + + * Abort the rebase and reset HEAD to the original branch ("undo" the rebase): + + ``` + git rebase --abort + ``` + + * Continue with the rebase, but skip any commits that introduce a merge conflict (any commits with merge conflicts will not be in the git history after finishing the rebase): + + ``` + git rebase --skip + ``` + +7. Reflect your changes on the remote (public GitHub repo): + + ``` + git push origin HEAD --force + ``` \ No newline at end of file