How to delete an old Git commit?

Asked

Viewed 2,328 times

3

guys, earlier I was playing some stuff in git and unintentionally copied a folder with a file inside and commited locally, then I deleted it and committed the deletion locally, I was developing what I needed and comittando, when I went to push to the remote, the repository is complaining about the existing file in the first commit, how do I "delete" this queued commit?

  • 1

    You can do git reset --soft HEAD^1, by removing commits until the one you don’t want (in case you don’t have commits in the remote yet).

  • Complaining how? What was the message?

  • he complains about the size of the file passed 140mb (which is precisely the file I accidentally played in a commit), so now I wanted to find some way to delete this commit in order to be able to push pro

  • I already have commit on the remote, never did a rebase, some problem?

1 answer

3

If the commit that introduced the file is called <bad> (locates it with a git log --stat), reconstruct your history from the commit before <bad> (I assume you’re in the branch that introduced the bad commit):

git rebase -i <ruim>~1

A text file will appear in a file editor. It lists all 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 <bad> commit contains only the introduction of the bad file (and nothing else). You just have to delete the line with the bad commit in the file. Save the file, close the editor, let the history do again without the bad commit, and git push in the end.

2 - The <bad> commit contains 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

in:

edit <ruim> Mensagem do commit

Saves the file and closes it. git will redo the history by stopping after the bad commit. Wait until the rebase pause, 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
  • guy I’m never did a rebase, I got a little lost in his explanation kkk

  • I added information in the post. Tell me if you understand now. If you need, I’ll add another way to solve your problem.

Browser other questions tagged

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