How to delete a commit?

Asked

Viewed 228 times

-1

I made a few wrong commit’s and I’m not getting one deleted specifically. All that were made after they were erased and it was what I wanted, I used the command git reset --hard + hash commit and get this message HEAD is now at 8932874 Creation of readme. When I try to give a git push --force get the message Everything up-to-date. How do I delete the first commit I made?

This is my log and I want to delete commit 8932874. If anyone can help I’d appreciate it.

8932874 (HEAD -> feature/CALONI-65-goiabinha, origin/feature/CALONI-65-goiabinha, master) Criação do readme
a687f69 (origin/master, origin/HEAD) Merged in feature/CALONI-62-GlofosApp (pull request #22)
43282e7 CALONI-62: Correção de erro onde edição de dados deletava o usuário. Motivo: Comando Bind ainda continha atributo IsVisible ao invés de Enabled.
  • I usually use git to rebase -i <commit previous to what you want to delete> to change the history of a repository. It’s a Swiss Army knife that’s worth learning, because it’s meant to erase a commit from the story, change the order of commits, edit a commit in the middle, and so on.

2 answers

0

You can try git rebase -i -p HEAD~2

where ~2 is the last two commits.

git log --to list the commits.

commit 0a6c84980 (HEAD -> hotfix/1, origin/hotfix/1)
Author: Murilo  <[email protected]>
Date:   Thu Aug 12 14:16:03 2021 -0300

  commit original

commit dedde93a7498e
Author: Murilo  <[email protected]>
Date:   Fri Aug 6 17:21:20 2021 -0300

    Commit duplicado

I run the rebase command listing the last two commits.

git rebase -i -p HEAD~2

Will load the following information;

pick dedde93a commit original
pick 0a6c8498 Commit duplicado

# Rebase 72337dee..0a6c8498 onto 72337dee (4 commands)
#
# Commands:
# p, pick <commit> = use commit
# r, reword <commit> = use commit, but edit the commit message
# e, edit <commit> = use commit, but stop for amending
# s, squash <commit> = use commit, but meld into previous commit
# f, fixup <commit> = like "squash", but discard this commit's log message
# x, exec <commit> = run command (the rest of the line) using shell
# d, drop <commit> = remove commit
# l, label <label> = label current HEAD with a name
# t, reset <label> = reset HEAD to a label
# m, merge [-C <commit> | -c <commit>] <label> [# <oneline>]
# .       create a merge commit using the original merge commit's
# .       message (or the oneline, if no original merge commit was
# .       specified). Use -c <commit> to reword the commit message.
#
# 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.

Mark duplicate commit as "drop"

pick dedde93a original commit drop 0a6c8498 Duplicate commit

# Rebase 72337dee..0a6c8498 onto 72337dee (4 commands)
#
# Commands:
# p, pick <commit> = use commit
# r, reword <commit> = use commit, but edit the commit message
# e, edit <commit> = use commit, but stop for amending
# s, squash <commit> = use commit, but meld into previous commit
# f, fixup <commit> = like "squash", but discard this commit's log message
# x, exec <commit> = run command (the rest of the line) using shell
# d, drop <commit> = remove commit
# l, label <label> = label current HEAD with a name
# t, reset <label> = reset HEAD to a label
# m, merge [-C <commit> | -c <commit>] <label> [# <oneline>]
# .       create a merge commit using the original merge commit's
# .       message (or the oneline, if no original merge commit was
# .       specified). Use -c <commit> to reword the commit message.
#
# 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.

I give an Esc and type :x to save and problem solved.

It will run rebasing and then just push -f on the remote branch to force updates.

0

Try to reverse the commit

git revert [hash do commit]

You can try too:

git reset --hard [hash do commit]
  • It didn’t work, it still keeps that commit, and I wanted it to be deleted.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.