Git - remove commit from history

I did something very stupid. I entered a cursor in my code and I clicked the code on the main branch. After that, I clicked a few more times so that people would not pull out bad things, but I can still find the cursor in the commit history.

Is there a way to remove a commit from a story? Edit: I do not want to add the file to gitignore because we need this file.

thanks

+6
source share
3 answers

Once you click on the repo, you really don't want to change the story. However, if you are absolutely sure that no one pulled / extracted from the repo from your abusive commit, you have 2 options.

If you want to completely remove the commit (and every commit that comes after that), do git reset --hard ABC~ (assuming the hash is ABC commit). Then do a git push -f .

If you just want to edit this message and save the commits that came after it, do git rebase -i ABC~ . This will launch your editor, showing a list of your commits, starting with the offensive. Change the pick flag to e, save the file and close the editor. Then make the necessary changes to the files and do git commit -a --amend , then do git rebase --continue . Follow it with git push -f .

I want to repeat, these options are only available to you if no one has done pull or fetch containing your abusive commit. If they do, following these steps will only worsen the situation.

+5
source

If this is only on your local PC (or no one has verified your changes):

1) Use:

git log

to find the commit you want to remove. Copy hash (long sqeuence like: e8348ebe553102018c ...).

2) Use:

git rebase -i your_commit_hash_code_comes_here

Just delete the commit you don't need and save the file.

Interactive git rebase may also allow you to fix a broken commit - there is no need to delete it.

If you pushed the changes on the server or someone already received your changes - never change the history - this will cause serious problems for your team.

+1
source

No, git add changes to the story. Read this article for more details.

0
source

Source: https://habr.com/ru/post/989281/


All Articles