0

When working on a git branch I got some changes in the latest commit. I suspect those changes to be wrong, but not sure. It is easy to remove the commit, and start over.

I want to move that commit to git-stash just incase I need them, and then try another solution on the branch, and still have the option to get the first commit back.

3
  • 3
    Why do you need stash? Create a new branch, rewind the old branch. (Or revert the commit) Commented Feb 7, 2024 at 9:33
  • That is much easier solution. Commented Feb 7, 2024 at 9:36
  • In fact, you should stop using git stash and use temporary commits exclusively instead. Normal commits are much safer in they do not have dangerous, complicated quirks like stash have. – Commented Feb 7, 2024 at 23:19

1 Answer 1

2

You don't need a stash in this case, a regular branch works just fine.

git branch backup-my-work git reset --hard HEAD^ 

reset --hard HEAD^ will move your branch back by one commit and discard ALL changes in your working tree. Drop the --hard if you want to keep your local changes and the changes of the undone commit (you can then selectively remove the changes from your working tree)

Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.