Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I reset the working tree without moving HEAD?

Tags:

git

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.

like image 839
Wilfred Hughes Avatar asked Oct 17 '16 20:10

Wilfred Hughes


People also ask

How do you reset the head of a branch?

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).

What does the command git reset hard head 3 do?

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.

Does git reset hard affect all branches?

(It does not affect the working tree or the current branch.) This means that git reset <pathspec> is the opposite of git add <pathspec> .

How do I clean up the working tree after a reset?

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.

What is the work­ing tree?

It’s full of the files you edit, where you add new files, and from which you remove unneed­ed files. Any changes to the Work­ing Tree are not­ed by the Index (see below), and show up as mod­i­fied files. When you open the files for a project that is being man­aged as a Git repos­i­to­ry then you are access the Work­ing Tree.

How do I clear the working tree in Git?

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.

What is a working tree in Git?

Another term demystified. Th Work­ing Tree in Git is a direc­to­ry (and its files and sub­di­rec­to­ries) on your file sys­tem that is asso­ci­at­ed with a repository. It’s full of the files you edit, where you add new files, and from which you remove unneed­ed files.


1 Answers

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.

like image 72
xaizek Avatar answered Oct 13 '22 17:10

xaizek