One of the nice things about using git, or source control in general is that I can go back to any point in development to which a commit has been made. The git log command can at any point be called to get a list of commit hash ids that can then be used with the git checkout command to switch to any one of those commits.
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 sever 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.
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. I could just clone done something and start using git log and git checkout to navigate around the tree. However for this section I will be creating a new git folder. 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 not move back one commit using the git checkout command. The git log command can be used to get a list of hash ids for each commit. It would be best to read up more on each little detail about the git log command, but that would be off topic.
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.