29

I have a branch called feat-a and I essentially want to "undo" my last local commit and just have those files staged. Then when they are all staged I want to stash those changes.

Now what I'm doing does sound a bit backwards, but the cause of this issue was a force push on master to re-write some commit authors, so doing a merge or rebase of master onto/into feat-a just completely breaks with a ton of conflicts even though master and feat-a are only off by one commit.

I have read through a similar question here: How to stash my previous commit?

But it isn't necessarily what I want to do. I just want to unstage and stash my last direct commit, versus what that user wanted (i.e. pull a commit out from between other commits).

4
  • You are using the word "staged" incorrectly. You do not need or want to stage the changes before you stash them. You want the changes to appear in the working directory, but not in the index. Oct 11, 2018 at 13:41
  • Interesting. Thanks for the heads up. Let me know if you have any good resources on that vocabulary, but I've almost always been using staging area for anything that is added there via git add .
    – c_idle
    Oct 11, 2018 at 14:14
  • Thanks for that @eftshift0
    – c_idle
    Apr 11, 2019 at 14:08
  • 1
    I have deleted the previous comment and corrected it (I mis-wrote wrote a word and it makes it redundant....). I meant: @WilliamPursell Reading your comments now (even though an answer was accepted). I think he is using the terms correctly. The only thing I would say is that for files that are already tracked, there's no need to stage the changes in order to stash them... however for new files, if you want to stash them, you have to add them (or use up a little more of your brain memory to learn and use one of the options from git-stash, if there's any to include new files when stashing).
    – eftshift0
    Jul 18, 2019 at 15:11

1 Answer 1

68
git reset --soft HEAD~1
git stash save "Saving instead" # or something like that
2
  • 2
    Just an FYI: In case if it's pushed and you want to revert & stash it, we can use git cherry-pick <commit_id> it before entering the commands mentioned above. Oct 15, 2019 at 13:43
  • 1
    As a remark to your remark, the cherry-pick stays an option even after resetting/stashing. The commit isn't destroyed instantly, it will stay available for inspection or cherry-pick until it's ultimetely garbage collected. Jan 21, 2020 at 3:27

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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