Given a git branch with some commits on it (C is the most recent commit):
A -> B -> C
How do I reset my workspace so that all the files are in the state they were at commit B, but HEAD is still at C?
I've looked at git-reset
, but none of the options seem to help. The man page suggests that all the different modes will move HEAD:
--soft Does not touch the index file or the working tree at all (but resets the head to <commit>, just like all modes do).
I've tried git reset HEAD~
but that moves HEAD.
To hard reset files to HEAD on Git, use the “git reset” command with the “–hard” option and specify the HEAD. The purpose of the “git reset” command is to move the current HEAD to the commit specified (in this case, the HEAD itself, one commit before HEAD and so on).
Summary. To review, git reset is a powerful command that is used to undo local changes to the state of a Git repo. Git reset operates on "The Three Trees of Git". These trees are the Commit History ( HEAD ), the Staging Index, and the Working Directory.
(It does not affect the working tree or the current branch.) This means that git reset <pathspec> is the opposite of git add <pathspec> .
If you have created some new files or directories, they may still remain after resetting. You can use the command below to clean up the working tree by recursively removing files from the previous branch that are not under version control.
It’s full of the files you edit, where you add new files, and from which you remove unneeded files. Any changes to the Working Tree are noted by the Index (see below), and show up as modified files. When you open the files for a project that is being managed as a Git repository then you are access the Working Tree.
Cleans the working tree by recursively removing files that are not under version control, starting from the current directory. Normally, only files unknown to Git are removed, but if the -x option is specified, ignored files are also removed. This can, for example, be useful to remove all build products.
Another term demystified. Th Working Tree in Git is a directory (and its files and subdirectories) on your file system that is associated with a repository. It’s full of the files you edit, where you add new files, and from which you remove unneeded files.
git checkout [-p|--patch] [<tree-ish>] [--] <pathspec>...
git checkout with <paths>
or--patch
is used to restore modified or deleted paths to their original contents from the index or replace paths with the contents from a named<tree-ish>
(most often a commit-ish).
So you need to run this at root of your repository (works fine for any sub-tree or file(s) too):
git checkout HEAD~ -- .
This will result in git applying changes necessary to revert files to HEAD~
state, the changes will be in the index.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With