How to delete an old Git commit?

3

guys, I was previously playing some stuff in git and I accidentally copied a folder with a file inside and commitei locally, then I deleted it and commitei the deletion locally, I was developing what I needed and committing, when I went to give a

    
asked by anonymous 22.05.2016 / 20:19

1 answer

3

If the commit that entered the file is called < bad > (find it with git log --stat ), rebuild your history from the commit before < bad > (I assume you are in the branch that entered the bad commit):

git rebase -i <ruim>~1

A text file will appear in a file editor. It lists all the commits you use to redo history (an interactive rebase) from the bad commit (first line). You have to decide what to do. 2 possibilities:

1- The commit < bad > contain only the introduction of the bad file (and nothing else). Just delete the line with the bad commit in the file. Save the file, close the editor, let the historian do it again without the bad commit and git push at the end.

2 - The & bad; commit > contain the introduction of the bad file and other things you want to talk about. Replace the commit line like this:

pick <ruim> Mensagem do commit

on:

edit <ruim> Mensagem do commit

Save the file and close it. Git will redo the history by stopping after the bad commit. Wait until the pause in the rebase, and:

git reset HEAD~1 # cancelar commit ruim
git status # ver que arquivos introduzir no commit
git add <arquivos que conservar>
...
rm <mau arquivo>
git commit -m "Mensagem do commit" # refazer commit
git rebase --continue # aplicar os commits seguintes se tem mais no rebase
git push
    
31.05.2016 / 11:34