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:
In order to test this process, let's make an empty file: touch script.js
You can see the current status of your folder any time with: git status
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
Now you're ready to create a snapshot! You have to include a message - make it short but meaningful.
Notes:
You can view a log of all commits with:
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
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
Did you delete a file or change code since the last commit and you want it back?
If you delete files from the project folder, you need to tell Git to stop tracking them
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.