Darek Błaszczyk
Darek Błaszczyk

Reputation: 31

How to restore working directory to given commit without affecting repository state?

I want to restore whole content of my working directory to the one saved in certain commit, but without making ANY changes to repository state - so, no detached head state please, because it makes my own head busy with irrelevant stuff, when all I want is basic restore this folder contents from that backup.

I am newbie in git and Im trying my best to not make this post a git rant, but for God's sake, why there is no --wdironly switch (or something like that) for git checkout command?

Why HEAD reassignment is mandatory?

The lack of simple "git restore" equivalent is so inconvenient, counterintuitive and frustrating that I found myself asking: "Why am I even using this? It has such an overhead in usage and I cant even utilize it to do the simplest task I wish for."

Anyway, here is the deal:

Assume there are 2 commits in master branch.

There may be some other branches out there, but we will focus on this one. The commits are: "First" and "Second", with "Second" being newest one, obviously.

I have also some trash in my working directory that I dont care for, so its contents may be safely erased before "restoring the backup".

However, there may be some precious data in my Index, so please, dont touch it if possible.

Now:

I want to take all the files that "First" commit contains and copy them to my working directory.
Thats it.

How can I fulfill this "unusual desire"?

Upvotes: 0

Views: 1041

Answers (2)

Darek Błaszczyk
Darek Błaszczyk

Reputation: 31

OK, now since I found the answer myself I realized that was a poor question.

Anyway, for your convenience - present and future newbies out there - here is the git command I was looking for:

git checkout <given commit SHA> *

Notice asterisk at the end of the command. Since git checkout <commit> <filename> restores selected file, the asterisk is just a wildcard for "all files".

Upvotes: 2

CodeWizard
CodeWizard

Reputation: 142114

I want to take all the files that "First" commit contains and copy them to my working directory.

You can use git cherry-pick <sha1> to add the desired commit to your current branch

enter image description here


I want to restore whole content of my working directory to the one saved in certain commit, but without making ANY changes to repository state

# checkout new branch from a given commit
# and switch to this branch 
git checkout -b new_branch 7cd6289

I have also some trash in my working directory that I dont care for, so its contents may be safely erased before "restoring the backup".

# clean all your uncommitted files
git clean -Xfd
git clean -xfd

-x (lower case)

Don’t use the standard ignore rules read from .gitignore (per directory) and $GIT_DIR/info/exclude, but do still use the ignore rules given with -e options. This allows removing all untracked files, including build products.

This can be used (possibly in conjunction with git reset) to create a pristine working directory to test a clean build.

-X (upper case)

Remove only files ignored by Git. This may be useful to rebuild everything from scratch, but keep manually created files.

Upvotes: 0

Related Questions