0

I have a previous commit.

After that commit I edited a file. But after that I messed up one thing.

I want to return to a previous commit but save changes in that one file. The file hasn't been committed.

How can I achieve this?

P.S.: I know that I can simply copy the contents of that file that I edited and after the returning to an older commit just paste it in there, but I want to learn how to do it using Git since there could be more complicated scenarios.

10
  • 1
    Are you looking for git commit --fixup and interactive rebase? In other words: do you want to apply the changes to the older commit?
    – knittl
    Commented Jun 17 at 16:15
  • 1
    My gut feeling is you want @knittl's comment, but if you simply can show git log --graph currently and the desired output, we can answer exactly. Also it's not clear if your latest edit has been committed yet.
    – TTT
    Commented Jun 17 at 18:07
  • 1
    @Coder4Fun250 where do you want to save them? In that case I'm with the first two commenters: probably stash or commit on a temporary branch
    – knittl
    Commented Jun 18 at 11:02
  • 1
    Stash was the solution. Thank you everyone for helping me out with this one! Commented Jun 18 at 15:26
  • 1
    jhole or jonsharpe, you can write an answer to this Q and I'll mark it as a solution Commented Jun 18 at 15:27

1 Answer 1

0

git reset --soft HEAD~1

From the git docs:

git reset [<mode>] [<commit>] This form resets the current branch head to and possibly updates the index (resetting it to the tree of ) and the working tree depending on . Before the operation, ORIG_HEAD is set to the tip of the current branch. If is omitted, defaults to --mixed. The must be one of the following:

--soft Does not touch the index file or the working tree at all (but resets the head to , just like all modes do). This leaves all your changed files "Changes to be committed", as git status would put it.

1
  • Thank you but it was a stash command that helped me. What you recommend is just plain rolling back to a previous commit Commented Jun 18 at 15:30

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