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
CodePudding user response:
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.
CodePudding user response:
"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>