Table of Contents
Git
Git is a tool for managing repositories. Repositories are like folders, except it's easier to track the changes that happen inside them.
Think of a project you've been working on e.g. a game in written in C++.
You've been working on your game for weeks and now you have quite a bit of code. You're starting to be a little afraid of making changes… what if you break the code? Are you confident you will you be able to fix it? What if it takes too much time to fix it? What if you never fix it? Everything you've been working on for weeks will have been lost forever!
So, like any prudent developer, you make backups. You copy the “Game” folder to Desktop and continue working on it without fear. If something goes wrong you just restore the backup! This works fine for a while, but then you notice you suddenly have a lot of “backups” on your Desktop.
Even worse, some of these “backups” don't compile at all. Yet, these backups that don't compile contain some code you developed that you now need. So, now you have to resort to CTRL + C, CTRL + V from those broken backups, and patch your real code with it. But of course, not before you make another backup.
Okay, this is way too many backups. Surely, there's gotta be a better way?
Illustration
In order to illustrate how git works, let's make a C++ project. We'll see how we can track changes that happen within it with git.
The fact it's C++ doesn't matter. Git can track any kind of project including projects that are not related to programming. If a project has a dedicated directory that contains project files, git can track it.
In order to
- Create a project
- Initialize a repository
- Add some code
- Create a commit
- Go back
Create a project
Create a project directory:
$ mkdir project
Put the following C++ code in project/main.cpp
// Compile: g++ main.cpp -o game // Execute: ./game #include <iostream> int main() { std::cout << "Hello, project!" << std::endl; return 0; /* technically not necessary ;) */ }
Initialize a repository
First you have to create a git repository for your project. This means running git init inside your project directory. This command will create a new directory called .git in your project directory which will serve the role of what your Desktop used to be. Namely, this is where git will place “backups” of your project as you go along. This is called a “local repository”.
git init
Then, you need to make an initial commit. This is going to be your first real “backup”. The commit message (given by -m option) doesn't need to be “Initial commit”. You can name the commit anything you like e.g. “First commit” or “Add: Start tracking with git”.
git add . git commit -m 'Initial commit'
Add some code
Forget about git now. Let's focus on programming.
Concepts
Repository
Repository is a place where git stores info. That's usually the .git folder you'll find at the root of your project. The .git folder itself is called a “local repository”.
You initialize the local repository by going to your project directory and running git init. This creates a hidden directory .git where git stores everything (branches, commits, blobs, tags, hooks, etc.). By default you'll have one default branch main (formerly called master) and no commits. Usually after git init you want to make an “Initial commit”. If you do rm -rf .git you lose everything. You can reinitialize a repository by running git init, but then you start afresh with what's left in your project directory.
Remote repository is another place git stores info. It's somewhere outside your project e.g. on Github. You manipulate remote repositories with git remote. Each remote repository has a name but the default name is origin.
The following are commands to remember
git init- Initialize a local repositorygit remote add origin https://github.com/<your_username>/<your_repo>.gitgit fetch --prune-git pullgit push main origin
Working tree
Diff
Commit
Branch
Tag
HEAD
Rebase
Merge
Commands
The trouble with git commands is that they're worded in plain English (“fetch”, “pull”, “push”, “reset”, “restore”, “checkout”) but that's not descriptive enough to tell you what the command does. Worse, often two commands sound similar but do completely differnt things (“reset”, “revert”, “restore”). Sometimes a command sounds like what you need (“Ctrl + Z” equivalent is… “reset”?) but actually it's something else (“checkout”).
This makes git confusing and its learning curve high. The best way to get ahead of the curve is to understand exactly what each command does, and then
Clone
Add
Commit
Fetch
Fetching means getting the state.
Think
git fetch --prune
Pull
Pulling

