How to delete git commits but keep changes?


by kaley , in category: General Help , 2 months ago

How to delete git commits but keep changes?

Facebook Twitter LinkedIn Telegram Whatsapp

1 answer

by sarai_bergstrom , 2 months ago


To delete or remove git commits while keeping the changes, you can use the interactive rebase feature provided by Git. Here are the steps to accomplish this:

  1. Open the terminal or Git Bash.
  2. Navigate to the repository directory using the cd command.
  3. Run the following command to start the interactive rebase process:
git rebase -i HEAD~N

Replace N with the number of commits you want to remove. For example, if you want to remove the last three commits, use HEAD~3.

  1. A text editor will open with a list of commits. Leave the word "pick" in front of the commits you want to keep intact. Change the word "pick" to "drop" or simply delete the line for the commits you want to delete.
  2. Save and close the text editor.
  3. Git will perform the rebase, and the selected commits will be removed.

Please note that rewriting Git history can have consequences if other people have cloned the repository. So, it's recommended to avoid using this method if your commits have already been pushed to a shared repository.