Version control and backup
Version control helps to keep track of file changes, save snapshots of the code, and roll back to older versions if necessary. Git is the most used versioning system at present; it is free and integrated into most editors. In this section, we will use version control in combination with VS Code.
Initializing the repository
Once Git is installed, it can be used from VS Code, by activating the Source Control tab using the branch icon on the left column bar. The Initialize Repository button adds versioning to our folder:
Figure 1.25: Adding version control in VS Code
The icon will change and warn us about the presence of files. We click the + icon next to the filename to add them to versioning. In Git terminology, we are going to Stage the current changes:
Figure 1.26: Staging changes in VS Code
The editor shows the before/after conditions of our files. We can add a message in the text field on the top left and click the tick icon. This will Commit our changes to the project history:
Figure 1.27: Commit changes in VS Code
Making changes
Let’s say we don’t want our script to delete the current objects. To do that, we delete line number 13:
bpy.ops.object.delete(use_global=False)
When the file is saved, version control detects this change. We can stage that by clicking the + icon, as before. If we select OurFirstScript.py
in the left column, VS Code highlights the current changes. We add a message for this new commit and click the tick button again:
Figure 1.28: Displaying changes in VS Code
If we go back to the Explorer tab and select our script, we will see that a section called Timeline can be expanded: it contains a list of our commit messages. Selecting a commit displays the related changes, allowing us to restore old lines of code. Every change that is not committed can be easily undone using the Revert function.
Reverting uncommitted changes
Let’s add some incorrect text at line 7 and save. If, for any reason, we cannot undo that, we can right-click our file in the Version Control tab and select Discard Changes:
Figure 1.29: Discarding uncommitted changes in VS Code
The importance of version control can be underestimated at first but becomes vital in more complex projects. It’s a wide topic that goes beyond the scope of this book, but it’s important to grasp at least the basics of it.