6 - Version Control Part 1
Having a history of your code and the ability to go back in time or merge your code with someone else's has transformed the way we work!
Git can sound very complicated, but if you stick to the basics, it's extremely helpful and powerful. At the very least - consider it a backup system to keep your code safe.
What are we learning?
In this lesson we will:
Learn how to configure GIT - the most commonly used Version Control system for code
Initialize our own local Git Repository
Create and modify some files, commiting them to history
Discuss branches
Create and utilize a separate branch so as not to destory the main branch
Merge work from the development branch to the main branch
The Lesson
Git comes installed with Linux, but just in case, you can try sudo apt install git
On MS Windows you will need to install Git for Windows
Configuring Git
Use the following commands to setup Git for your computer (replace the text with your information). You only have to do this once per computer.
git config --global user.name "Mr. Squirrel"
git config --global user.email "mr.squirrel@stu.ocsb.ca"
git config --global init.defaultBranch main
Initializing a Local Repository
We need to tell the git program that we want to start tracking changes:
Enter the folder you will be using for your project: cd my_project_folder
Initialize Git with this command: git init
This creates a hidden folder that will track all your information. Don't mess with this folder unless you know what you're doing.
Create / Edit Some Files
In order to test this process, let's make an empty file: touch script.js
Check the Current Status
You can see the current status of your folder any time with: git status
Staging Changes
When you're ready to make a snapshot in time (called a "commit"), you need to decide which new or updated files will be part of the saved information.
To add all new and changed files to the "staging": git add *
To add specific files, just list them: git add script.js README.txt style.css
You can always ensure things are setup the way you want with git status
Committing a Snapshot
Now you're ready to create a snapshot! You have to include a message - make it short but meaningful.
git commit -m "Initial Snapshot"
Notes:
You can stage all changed files while committing with -a (won't add new files)
If you don't include a message with -m a text editor will open for you to add a message.
Seeing History
You can view a log of all commits with:
git log
git log --oneline
Branches
The other major benefit of Git is creating alternate timelines using branches. You should never work directly on the main branch - you should always develop and test on a separate branch and then merge the tested code into the main branch. Let's try it:
Create a development branch: git branch dev
Switch to the new branch (leave the main branch alone): git checkout dev
Modify your script and create an addition function that adds two given numbers (or something similar)
Commit your changes: git commit -a -m "Created add() function"
Check the logs: git log
Notice the location of "main" and "dev" in the log. This is showing that the "dev" branch is ahead of the "main" branch.
If you test your code and you're sure that it belongs in the "main" branch, you merge it:
git checkout main
git merge dev
Check the log: commit log
Restoring Files
Did you delete a file or change code since the last commit and you want it back?
git restore <filename>
Deleting Files
If you delete files from the project folder, you need to tell Git to stop tracking them
git rm <filename>
Reseting to a Previous Commit
To go fully back in time (careful, this is dangerous), you can perform a "hard reset".
Get the short hash of the commit you want to revert to with: git log --oneline
Revert back to that commit and restore all changes or delete files (also removes new files): git reset --hard ####### where ####### is the hash in question.