I’m trying to solve a problem where I replace my current HEAD with a specific commit by doing git reset --hard (commit)
, but it always ends up dirty, which I would like to remove. I need help with some way to pull a commit and replace the current HEAD, without it being dirty. I know git clean
exists, but if possible I’d like to not have to use it. Thanks for any help
Asked
Active
Viewed 119 times
-1

SwissCodeMen
- 4,222
- 8
- 24
- 34

MeepleDeMeep
- 1
- 1
-
If you want to clean untracked files, you have to use `git clean`, short of manually removing untracked files. Not sure what your question is exactly? – knittl Sep 27 '22 at 19:51
-
Dirty as in it doesn’t delete untracked files? I don’t see what’s wrong with using `git clean` for its intended purpose… – Ry- Sep 27 '22 at 19:51
-
A direct way to do what you want is `git switch -C
`, or if you are trying to reach a "detached HEAD" state : `git switch --detach `. Your question seems a bit odd though, would you care to explain in more details what you need this for ? -
What does _dirty_ mean here? – eftshift0 Sep 27 '22 at 20:13
-
I’m writing a script to update Dolphin. I’m capable of obtaining the commit gases for whatever version I’d like to update or downgrade to, the issue is after switching to one it’s dirty, and I can’t git clean it for the life of me – MeepleDeMeep Sep 27 '22 at 22:33
-
@MeepleDeMeep: ok, I'm not familiar with Dolphin, can you give a quick pointer to the project ? – LeGEC Sep 28 '22 at 04:40
-
@LeGEC https://github.com/dolphin-emu/dolphin – MeepleDeMeep Sep 28 '22 at 16:38
2 Answers
0
You might be looking for git stash -u push
. It stores your changes, both tracked and untracked, and removes them from the visible working tree, without your having to make a full-fledged commit. You can always delete the stash later if you don't really want anything that's there, but it costs little to keep it around.

matt
- 515,959
- 87
- 875
- 1,141
-
Though instead of answering, perhaps I should have suggested closing as a duplicate of https://stackoverflow.com/questions/61212/how-do-i-remove-local-untracked-files-from-the-current-git-working-tree — that would be okay too. – matt Sep 27 '22 at 20:01
-
-
1
-
0
"going from one commit to another" is simply git switch
(or the older git checkout
).
If you want to go to get to <commit>
in a "detached HEAD" state :
git switch --detach <commit>
# or the older :
git checkout <commit sha>
# or
git checkout <ref name that's not a branch>
# 'git checkout' with tags or remote branches leads to a detached HEAD
If you want to keep a specific branch and "move" it to <commit>
, you can use :
git switch -C <branch name> <commit>
# or the older
git checkout -B <branch name> <commit>
# if you want to move the active branch :
git switch -C "$(git branch --show-current)" <commit>
# or the older
git checkout -B "$(git rev-parse --abbrev-ref)" <commit>

LeGEC
- 46,477
- 5
- 57
- 104