2

My log looks something like this with the last 2 commits:

commit ABC
TEMP

commit DEF

Working!

Something I did after DEF broke my code. I didn’t want to lose the changes, so I made a commit named TEMP. I then did git checkout DEF -f, then git status told me HEAD detached at DEF. When I do a log command, I don’t see my TEMP commit.

What I would like to do is to use git restore (or another command) to get each modified file from ABC, one by one until I find the culprit file. Then I’d like to delete or “strip” ABC. I’m coming from mercurial where you can delete commits. I haven’t pushed to my remote repo, because I don’t want the TEMP/ABC commit to go the remote repo.

I realize I’m probably doing it “the wrong way”, so how should I handle a situation where I messed something up, and I want to go back to the previous commit but save my changes so I can grab them file by file? I assume there is a better way than creating a TEMP commit as I did.

Abdollah
  • 4,579
  • 3
  • 29
  • 49
WhiskerBiscuit
  • 4,795
  • 8
  • 62
  • 100

1 Answers1

2

Since you have not pushed, you can (assuming you don't have any work in progress)

That is:

git switch master
git reset --hard DEF
git diff-tree --no-commit-id --name-only -r ABC
git restore -s ABC -SW -- afile

This uses the new Git 2.23 (August 2019) git restore command.
And the new git switch command.

VonC
  • 1,262,500
  • 529
  • 4,410
  • 5,250
  • I’ll give that a shot when I wake up. How would you handle this assuming I never made a TEMP commit? Is this a case for `git stash`? – WhiskerBiscuit Sep 29 '19 at 07:53
  • @WhiskerBiscuit The reset (after switch master) will get rid of the TEMP detached HEAD. See the second part of https://stackoverflow.com/a/3965714/6309 – VonC Sep 29 '19 at 08:06
  • Ok, but what I was asking about is what would be if I didn’t create a TEMP commit. That is I have a changed working directory and want to save the changes and then test each file. Is creating a temp commit the way to go, or is there another way of temporarily saving the files? – WhiskerBiscuit Sep 29 '19 at 09:21
  • @WhiskerBiscuit Yes, creating a temp commit is a good way to stash more permanently your changes. Then you can follow the same recipe I describe in my answer above. – VonC Sep 29 '19 at 09:41