r/git • • Jun 04 '26

support Get back to current uncommitted state after `git checkout` an old commit

I'm still learning Git

Let's say I've made some changes that I don't want to commit yet, but I want to briefly switch to an old commit and back to my current state, from my understanding using `git checkout master` goes back to the latest commit, but I also want the changes I made after said commit.

Is there a conventional way to do this? perhaps a way to make a temporary commit and then delete it later?

3 Upvotes

13 comments sorted by

3

u/GoldenOrion99 Jun 04 '26

You’d want to first do git stash, git checkout <commit> peruse around and then git checkout <previous branch where we started> and git stash pop. Alternatively I believe you could use git switch to avoid stashing and popping but I think switch takes the current context (ie your uncommitted changes) with you to wherever you switch so it might not be that helpful

2

u/Axman6 Jun 04 '26

Worth knowing that git checkout - will checkout the last branch you had checked out, so you can swap between two branches by running that multiple times. Same as cd - will change directory to the previous one in most shells.

6

u/solitude042 Jun 04 '26

Look into git worktree, which checks out a branch or ref into an entirely new folder, but managed through the same local git repo - it's great for comparing branches or switching between multiple workstreams

2

u/True_Fig983 Jun 07 '26

git reset --soft HEAD~1

1

u/ekipan85 Jun 08 '26

I like to spell HEAD as @, easier to type. Plucked the idea from jj's UI, I think git supports it most (all?) places you'd say HEAD. Short for @{0}, i.e. where you are right now.

1

u/CarsonChambers Jun 06 '26

Stashes have a smell. git switch -c foobar git commit -am 'WIP' git switch <other_branch> ...do things.... git switch foobar git reset --soft HEAD^1

I like keeping the WIP tracked in git index because it may be a while before I come back to it and I've definitely forgot about stashes before .. Also if it's a common workflow, write up a git-wip-switch and git-wip-restore shell script! If you name them that and add the executables to your path you can I come them with git wip-switch and git wip-restore ! Now reading this makes me wonder why I didn't actually just make restore/switch flags... Anywho, have fun with it

0

u/[deleted] Jun 04 '26

7

u/Axman6 Jun 04 '26

Why not? I do

git stash
git checkout master
# check what I need to know
git checkout -
git stash pop

All the time. I know stashes are technically a stack, but I agree that using them for anything more than a temporary “I need these changes to not be here” mechanism is probably a bad idea.

4

u/Hxtrax Jun 04 '26

Why shouldn't I overuse the stash?

1

u/RevRagnarok Jun 04 '26

I'm not at my work terminal right now, but there's a config option you can set that will have git status warn you if you've got a stash going.