Grab any file from a past commit in Git

Have you ever been working on a project and you realize that at some point you altered a file for the worse? The code was fine before, but you went in and changed it, thinking it was an improvement, and now you want it back?

If you’ve been tracking it with Git, as you really should with any project, it’s easy to hop in your time machine and retrieve the file. Simply enter the following command:

git log --follow -- public/functions/main.php

This will show you a history of commits where that file was changed, along with the author of the commit, the date of the commit, and the commit message. You’ll see something like this:

Let’s say looking back at the history of this file, you decide that “update all functions in main.php” is the last commit before you made the bad changes to file.php. Simply run the following command:

git checkout b43123 public/functions/main.php

This does exactly what it looks like, it checks out the file “main.php” from the commit starting with hash b43123. This version of main.php is now in your working directory. Review and test it, and if you’re happy with it, commit it and push it back to the tip of your branch. Voila!

Practical Git Tutorial Part 1

Getting started with Git can be a complicated affair. What do all those commands mean, exactly? When can I stop reading this tutorial, and learn something practical? You end up spending a lot of time bouncing between high level theory and searching Stack Overflow for specific errors you are getting. You end up with neither a good grasp on Git theory, or a nuts and bolts understanding of how to complete basic tasks. There is a real lack of easily digestible Git tutorials that help you get your hands dirty while understanding what you’re doing conceptually. That’s what this series of articles attempts to remedy.

Creating a Repository and Pushing Work to a Central Repository

Step one: Create a repository

This part is simple. git init will create a folder titled “.git” in the directory you’re in, and allow you to push files (and subsequent file updates) from this directory to a central repository that you will specify.

Step two: Point your repository to a central repository

Somewhere on another computer (or your own) is another .git folder that we want to push our changes to; a central (or “remote”) repository. What we’ve done here is called the git remote command with the directive add. We’ve then passed the add directive two parameters: a name to refer to our remote repo by and it’s location on the internet.

Step three: Test it

Great. Now let’s test it out. Find a file in your directory you want to push to the central repository, and add it to your list of files to push:

Save the list

The -m flag stands for “message” and “first commit” is the message I chose to associate with this list. You can write whatever message you want.

Now push the list to your remote repository

Here origin is the name we previously gave to our remote repository, and master is the default name of the branch we’re working with. Until we create another one, the concept of branches can be safely ignored.

And that’s it. All we’ve done is create our own repository, pointed it to another central repository, and sent a file to it.

Thanks for reading. Please let me know what you thought in the comments below.