Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to set the working directory to an older commit?

To track down at which point I broke a feature in my software, I need to review older versions of my repository. I just want to set the working directory to an older commit, play with the code, afterwards discard the changes, and then try another commit.

I do not want to change anything about commits, neither remove nor create ones. I tried using git reset but after that newer commit weren't shown anymore. So I downloaded the repository again, because I didn't know how to revert that.

like image 786
danijar Avatar asked May 14 '13 09:05

danijar


2 Answers

A simple git checkout old-sha1 can be a start, but the real command for that kind of task is:

git bisect.

Find by binary search the change that introduced a bug

If you have a script able to test if your working tree "works" or not, you can execute that script on previous commits through git bisect, locating the first commit which breaks your test.

Note that this command isn't yet supported directly by GitHub for Windows: you will have to open a shell.


A git checkout would leave you in a detached HEAD, which doesn't matter since you won't make any modification.
To get back to were you were, checkout a branch:

git checkout master

See "Why did git detach my head?".

like image 129
VonC Avatar answered Nov 13 '22 11:11

VonC


You can checkout the code in another branch to the index by using the following:

git checkout my-other-branch .

The dot is a path specifier indicating you want to do this for the entire repository. You could likewise specify only a specific folder. Technically, you can specify any 'tree-ish' specifier for the my-other-branch parameter, such as a tag, a commit hash, something like HEAD~1, ect... If you want these changes to be unstaged, you could then do:

git reset HEAD

Assuming you began in a clean state, you'll now have your working directory be in the same state as my-other-branch.

like image 11
mhand Avatar answered Nov 13 '22 12:11

mhand