The git checkout command in git can be used to switch to another branch, but also to an older commit on the same branch and back again. One of the major reasons to use source control to begin with is so that I can go back to any point in development to which a commit has been made. Another major feature of using git is branching, being able to create a whole other branch to make some changes that may or may not be merged down into the main master branch at some point.
The checkout command can then be used to switch the current head of the git folder to a given commit, it can also be used to switch back to the latest commit for a branch by just giving the branch name rather than a commit hash id. The git checkout command is then one of several commands that I or any developer that uses source control should be aware of. So it is worth it to write about a few simple examples of the git checkout command that have to do with typical use cases.
In this section I will be going over a simple example that involves creating a basic test git folder. In the folder I will be just creating two commits, and use the git checkout command to switch back to the first commit, and then back to the latest commit.
The first step here is to have a git folder to work with to being with. I could use the git clone command to copy down something however for this section I will be creating a new git folder with git init. So I start out by creating a new folder called foo and make foo the current working path. Inside the foo folder I do a git init to make foo a git folder.
Once I have my foo git folder it is time to create some content for it. So I used the echo command and redirection to just create a simple test file called file1.txt. Now that I have a file I can stage the file to be committed by using git add, and then use the git commit command to create the first commit for the git folder.
So now that I have by first commit I will want at least one more commit so that I have something to switch to with the git checkout command. So I just repeat the process of creating another file, and once again create another commit.
So now that I have at least two commits in my foo test folder I can now move back one commit using the git checkout command. In order to do so however I will need a tag, or a hash id of some kind. One way to get a list of recent hash ids would be to use the git log command.
So now that I have hash ids I can use them as a way to go to a certain commit in the git folder. I just need to call git checkout and then paste in the hash id of the commit I want to go to. So if I us the hash id of the first commit, and then do a ls command I will just have file1.txt which was the case with the first commit.
I can then pass the name of the current branch to go back to the latest commit for the current branch.
So the git checkout command is like the cd command of git folders, it can be used to change what the current commit is for the git folder. The git log command is then similar to the ls command in posix systems in that the git log command can show me commit ids to go to from where I am currently.
There are a whole lot of other reasons such as typically using a remote as a way to back up source, and make it easier for other people to work on a project and share the code. However even if git is use purly offline without any remote it is still way better to use git rather than