xxxxxxxxxx
# Step 1: Find the commit hash of the commit you want to delete
git log
# Step 2: Revert the target commit and create a new commit
git revert <commit_hash>
# Step 3: Delete the original commit (optional)
git branch temp # Create a temporary branch to keep original commit
git rebase -i <commit_hash>~1 # Starts interactive rebase
# In the interactive editor that appears, delete the line for the original commit
# Save and exit the editor
# Note: Be cautious when modifying the commit history
# Step 4: Push the changes to remote repository
git push origin <branch_name> --force-with-lease
xxxxxxxxxx
# How to stash and remove a commit from a branch that has already been pushed
git stash save "<comment>"
git stash pop
# git stash list
git reset --hard HEAD~1
# 1 stands for the amount of commits to go back from HEAD
git push origin HEAD --force
xxxxxxxxxx
git checkout master
git reset --hard e3f1e37
git push --force origin master
# Then to prove it (it won't print any diff)
git diff master..origin/master
xxxxxxxxxx
# Step 1: Identify the commit you want to remove
git log
# Step 2: Start an interactive rebase and mark the commit to be removed
git rebase -i HEAD~n
# Replace 'n' with the number of commits from the present branch head to the commit you want to remove.
# Step 3: In the interactive editor, delete the line corresponding to the commit you want to remove.
# Step 4: Save and exit the editor.
# Step 5: Force push the updated branch to the remote repository
git push origin <branch-name> --force
xxxxxxxxxx
COMMITS MESSAGES
abcd123 4th commit message
xyze456 3rd commit message
98y65r4 2nd commit message wants to be removed.
bl8976t 1st commit message
xxxxxxxxxx
# First, make sure you are on the branch where the commit is located
git checkout your_branch_name
# Then, start an interactive rebase to modify the commit history
git rebase -i HEAD~2
# An editor will open with a list of the last two commits.
# Change "pick" to "drop" for the commit you want to delete.
# Save and close the editor.
# Git will now remove the unwanted commit(s) from the branch.
# If there are no conflicts, the process will complete automatically.
# Finally, if you have already pushed the branch to a remote repository,
# you may need to force push the modified branch to update it.
git push --force origin your_branch_name