I would like to know how to delete a commit.
By delete
, I mean it is as if I didn't make that commit, and when I do a push in the future, my changes will not push to the remote branch.
I read git help, and I think the command I should use is git reset --hard HEAD
. Is this correct?
Here '2' is the number of commits you want to rebase.
if you want to rebase all the commits.
Then you will be able to choose one of these options.
p, pick = use commit
r, reword = use commit, but edit the commit message
e, edit = use commit, but stop for amending
s, squash = use commit, but meld into previous commit
f, fixup = like "squash", but discard this commit's log message
x, exec = run command (the rest of the line) using shell
d, drop = remove commit
These lines can be re-ordered; they are executed from top to bottom. If you remove a line here THAT COMMIT WILL BE LOST. However, if you remove everything, the rebase will be aborted. Note that empty commits are commented out
You can simply remove that commit using option "d" or Removing a line that has your commit.
Say we want to remove commits 2 & 4 from the repo.
Note: You need to have admin rights over the repo since you are using
--hard
and-f
.git checkout b3d92c5
Checkout the last usable commit.git checkout -b repair
Create a new branch to work on.git cherry-pick 77b9b82
Run through commit 3.git cherry-pick 2c6a45b
Run through commit 1.git checkout master
Checkout master.git reset --hard b3d92c5
Reset master to last usable commit.git merge repair
Merge our new branch onto master.git push -f origin master
Push master to the remote repo.What I do usually when I commit and push (if anyone pushed his commit this solve the problem):
hope this help
I'm appending this answer because I don't see why anyone who has just tried to commit work would want to delete all that work because of some mistake using Git!
If you want to keep your work and just 'undo' that commit command (you caught before pushing to repo):
Do not use the --hard flag unless you want to destroy your work in progress since the last commit.
Take backup of your code in to temp folder. Following command will reset same as server.
If you want to keep your changes , and remove recent commits
Forcefully Change History
Assuming you don't just want to delete the last commit, but you want to delete specific commits of the last n commits, go with:
git rebase -i HEAD~<number of commits to go back>
, sogit rebase -i HEAD~5
if you want to see the last five commits.Then in the text editor change the word
pick
todrop
next to every commit you would like to remove. Save and quit the editor. Voila!Additively Change History
Try
git revert <commit hash>
. Revert will create a new commit that undoes the specified commit.