The old file revision will show up as a “Change to be committed,” giving you the opportunity to revert back to the previous version of the file. Remember, unlike checking out a commit, this does affect the current state of your project. For example, if you only wanted to see the hello.py file from the old commit, you could use the following command: git checkout a1e8fb5 hello.py If you’re only interested in a single file, you can also use git checkout to fetch an old version of it. Once you’re back in the master branch, you can use either git revert or git reset to undo any undesired changes. This assumes that you're developing on the default master branch, which will be thoroughly discussed in the Branches Module. To continue developing, you need to get back to the “current” state of your project: git checkout master Nothing you do in here will be saved in your repository. You can look at files, compile the project, run tests, and even edit files without worrying about losing the current state of the project. This makes your working directory match the exact state of the a1e8fb5 commit. You can use git checkout to view the “Make some import changes to hello.py” commit as follows: git checkout a1e8fb5 Let’s say your project history looks something like the following: b7119f2 Continue doing crazy thingsĪ1e8fb5 Make some important changes to hello.py First, you’ll need to find the ID of the revision you want to see. To help you decide, you want to take a look at the state of the project before you started your experiment. This example assumes that you’ve started developing a crazy experiment, but you’re not sure if you want to keep it or not. Image 2.4.2 - Checking out a previous version of a file Example Viewing an Old Revision So, in effect, this usage of git checkout serves as a way to revert back to an old version of an individual file. You can re-commit the old version in a new snapshot as you would any other file. On the other hand, checking out an old file does affect the current state of your repository. This is called a “detached HEAD” state, and it can be visualized as the following: Image 2.4.1 - Checking out a previous commit During the normal course of development, the HEAD usually points to master or some other local branch, but when you check out a previous commit, HEAD no longer points to a branch-it points directly to a commit. The “current” state of your project remains untouched in the master branch (see the Branches Module for details). It’s impossible to harm your repository while viewing an old revision. Once you’ve built up a project history, git checkout is an easy way to “load” any of these saved snapshots onto your development machine.Ĭhecking out an old commit is a read-only operation. The whole idea behind any version control system is to store “safe” copies of a project so that you never have to worry about irreparably breaking your code base. This will put you in a detached HEAD state. You can use either a commit hash or a tag as the argument. Update all files in the working directory to match the specified commit. This turns the that resides in the working directory into an exact copy of the one from and adds it to the staging area. git checkout Ĭheck out a previous version of a file. Branches are covered in depth in the next module, but for now, you can just think of this as a way to get back to the “current” state of the project. Checking out a file lets you see an old version of that particular file, leaving the rest of your working directory untouched. This can be used to view an old state of your project without altering your current state in any way. In this module, we’re only concerned with the first two configurations.Ĭhecking out a commit makes the entire working directory match that commit. The git checkout command serves three distinct functions: checking out files, checking out commits, and checking out branches. resetting unpublished changes on your local machine. First, it shows you how to explore old commits, then it explains the difference between reverting public commits in the project history vs. This tutorial provides all of the necessary skills to work with previous revisions of a software project.
0 Comments
Leave a Reply. |