Git is software that allows you to save your work at any moment. It's called a version control system, which means it lets you create versions of your workspace and switch between older and newer states. Let's first see why we need a version control system
https://www.youtube.com/watch?v=2ReR1YJrNOM
Think of it like a video game. You reach a certain point after hours of playing. You're proud of how far you've come and don't want to do it all over again if you lose. So you decide to "save your game. If something goes wrong after that point, you can always reload and start from there.
This is exactly what happens with Git. However, instead of calling it saving your game, we call it committing your changes. A "change" is a code modification you made in one or more files. It's recommended to commit multiple times a day every time you make something worth saving. Committing often makes it easier to reset your work to the last working state. Discarding changes with Git is better than relying on Ctrl-Z to undo failed attempts.
If you ever want to go back to a previous game save, you can check out that commit. You'll learn more about this in the next sections.
Check out the following short clip to learn the essentials of Git. It will go quite fast, but we'll cover each step in more detail later:
https://www.youtube.com/watch?v=hwP7WQkmECE&t
After installing Git, you can use it through the CLI. To verify that it worked, enter the command:
git --version
It should give you a version number which means that it is installed.
Now that you have Git installed, it's important to make a basic configuration. Open a terminal and type in the following:
git config --global user.name "Your name"
git config --global user.email "[email protected]"
This makes sure Git is able to identify you as the person that uses it to save your files and folders.
Next we would like to configure Git to use VSCode as it’s default editor by running the following command.
git config --global core.editor "code --wait"
<aside> ⚠️
Make sure that the code CLI command is installed on your computer.Make sure you followed the steps in Installing the code CLI command
</aside>
If you are running Windows, run this additional command:
# Windows only
git config --global core.autocrlf false
This ensures that your files use the same line endings as your fellow trainees on Mac or Linux computers.
<aside> 🎉
Git is now installed and configured!
</aside>
Watch the following video introducing the Git workflow and basic commands. You can skip the setup section, as we've already covered it above.
https://www.youtube.com/watch?v=8JJ101D3knE
There are different ways of using Git. We saw one important procedure: committing your workspace to a local repository. Let's break down that phrase:
.git that functions as the local repository.To setup a new repository, use:
git init
This command creates a brand new local repository in your project folder. You must do this before you can follow the procedure below.
The procedure happens in three stages:
<aside> ⌨️
Hands on: Create your own local Git repository, make three commits and check the history
</aside>
When working with Git, you don't always want to track every file in your project. Some files, like temporary files, logs, or sensitive information, should be excluded from version control. This is where .gitignore comes in.
A .gitignore file is a special hidden text file that tells Git which files and folders to ignore. When you list a file or folder in .gitignore, Git will not track changes to those files, and they won't appear in your staging area.
To create a .gitignore file, simply create a new file called .gitignore in the root of your repository. You can do this through the CLI:
touch .gitignore
Or create it directly in VSCode.
Each line in .gitignore represents a file, folder or a pattern to ignore.
You can find ready-made .gitignore templates for different programming languages and frameworks at github.com/github/gitignore.
<aside> ⌨️
Hands on: Create .gitignore in your local git repository and add one of the files. What happens when you modify that file and try to commit?
</aside>
We recommend bookmark or save this useful cheat sheet with the most important commands.
https://education.github.com/git-cheat-sheet-education.pdf
Next week, we will cover two very important features of Git: branches and merges. You don’t have to learn those concepts yet.
The HackYourFuture curriculum is licensed under CC BY-NC-SA 4.0 **
