Can I delete a git commit but keep the changes

704    Asked by LilyHemmings in Devops , Asked on Jul 15, 2021

In one of my development branches, I made some changes to my codebase. Before I was able to complete the features I was working on, I had to switch my current branch to master to demo some features. But just using a "git checkout master" preserved the changes I also made in my development branch, thus breaking some of the functionality in master. So what I did was commit the changes on my development branch with a commit message "temporary commit" and then checkout master for the demo.

Now that I'm done with the demo and back to work on my development branch, I would like to remove the "temporary commit" that I made while still preserving the changes I made. Is this possible? How to undo commit but keep changes?

Answered by Yajaira Satchell
   git reset --soft HEAD~1

And this will delete the recent commit and it doesn’t do anything to your work.

  Refer: https://git-scm.com/docs/git-reset#Documentation/git-reset.txt---soft

Or there is a way you can use without using --soft option

Use

  git reset HEAD^

Which moves your HEAD to point to the specified commit, without changing any files

And HEAD^ refers to the parent commit of your current commit.

This way git undo commit but keep changes.



Your Answer

Interviews

Parent Categories