Technomancy

Entries tagged “git”

How to go back in time in a git repository

written by rory, on Jan 9, 2009 2:11:24 PM.

I frequently use git to manage files that are not source code. For example, if I'm doing a lot of data processing on files, I like having the whole thing under git. This way I can add files, rename files, and keep the whole thing under git. I can use git's branching to say "These are the files I'm working on now, and these are the versions of the files that are in use on this system". I was also safe in the knowledge that I could delete files and know they were still around somewhere. Today I needed to look at these old versions of files. When you're creating a new branch with git, normally you create it based on the the current branch you have checked out, i.e. this is where the fork in the road is. However with git, you can branch from any point. So in order to go back in time, you can just create a branch from where you want to 'rewind to' and switch to that branch. All your current versions of files are kept in a different branch so are safe. Firstly check the logs to get the commit id of where you want to go back in time to.
$ git log
...

commit 31d4a2f7d06d3ed8d7af62055370a5914029040d
Author: Rory McCann <rory@technomancy.org>
Date:   Tue Jan 6 09:59:56 2009 +0000

    Removed these unneeded files

commit c19940a4b0e0623a131a923083b72f26b8a1b2f8
Author: Rory McCann <rory@technomancy.org>
Date:   Tue Jan 6 09:57:34 2009 +0000

    Added new CSV files

...
So I want to see the CSV files that I removed, so I want to go back in time to commit c19940a4b0e0623a131a923083b72f26b8a1b2f8. So I branch from that point and call it "my-old-files". That's the name of the branch that has the version of your files at that commit.
$ git branch my-old-files c19940a4b0e0623a131a923083b72f26b8a1b2f8
$ git checkout my-old-files
Switched to branch "my-old-files"