2

I accidentally inserted 1 commit in my local master branch. I have not pushed this changed and I want to remove it. This was supposed to be in a new branch.

So, I created a new branch based on master including the commit. Then, I decided to remove this commit from local master. In my terminal, I can successfully do:

git reset --hard HEAD~1

Since I have been using Emacs, I tried doing the same in Magit. First, on Magit status buffer, I pressed X (Reset). After, I chose h(hard). Then, the mini-buffer asked

Hard reset master to (default master):

I pressed enter (choosing default). After that, I decided to inspect the git log. I was expecting that the last local commit would be discarded. Unfortunately, it was not.

1
  • 1
    Magit's interactive rebasing could also be of help in this situation.
    – aadcg
    Commented Mar 12, 2022 at 12:45

2 Answers 2

2

You can also just move to the commit you want to reset to and then invoke the reset command, and Magit will interactively offer that as the default value.

Magit commands do this in general, and I recommend taking advantage of this contextual behaviour to avoid having to manually type references.


This was supposed to be in a new branch

bs to create a "spinoff" branch is designed for that exact situation, and streamlines the process down to simply naming the new branch.

0

The problem was not answering the mini-buffer question in the appropriate manner. Instead of typing a number (such as 1 after HEAD~) it is necessary to insert the commit id before the last to be removed commit. Problem solved :)

Not sure if there is a way of using the number/amount of commits to go back instead of the commit id as a reference.

1
  • 1
    You don't need to use a commit hash. You can enter any reference in any format Git recognises. You originally entered master which, if you were already on the master branch, would hard-reset to the commit you were already at.
    – phils
    Commented Mar 11, 2022 at 19:03

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.