Simply put, version control is like backups for your code, only more powerful.
Git is a very complex tool; only the basics that are needed for this book will be covered here.
Git does not track your changes automatically. In order for Git to run properly, we have to give it the following information:
- Which folders to track
- When to save the state of the code
- What to track and what not to
Before we can do anything, we tell Git to create a git
instance in our directory. In your project directory, run the following in your terminal:
Git will now start to track changes in our project. As git
tracks our files, we can see the status of our tracked files, and any files that are not tracked, by typing the following command:
Now we can save our first commit, which is a snapshot of your code at the time that you run the commit
command.
At any point in the future, we can return to this point in our project. Adding files to be committed is called staging files in Git. Remember to add stage files only if you are ready to commit them. Once the files are staged, any further changes will not be staged as well. For an example of more advanced Git usage, add any text to your main.py
file with your text editor and then run the following:
Your terminal should look something like this:
The Git system's checkout
command is rather advanced for this simple introduction, but it is used to change the current status of the Git system's HEAD
pointer—that is, the current location of our code in the history of our project. This will be shown in the next example.
Now, to see the code in a previous commit, first run this:
The string of characters next to our commit message, f01d1e2
, is called the hash of our commit. It is the unique identifier of that commit that we can use to return to the saved state. Now, to take the project back to that state, run this:
Your Git project is now in a special state where any changes or commits will neither be saved nor affect any commits that were made after the one you checked out. This state is just for viewing old code. To return to the normal mode of Git, run this: