Git’s distributed nature means your local repository is a complete, independent backup of the entire project history.
Let’s get Git installed and set up so you can start tracking your code.
First, installation. On most Linux systems, it’s as simple as:
sudo apt update && sudo apt install git
Or on macOS with Homebrew:
brew install git
For Windows, download the Git for Windows installer from git-scm.com and follow the prompts. You’ll want to ensure you select the option to use Git from the command line.
Once installed, the absolute first thing you must do is tell Git who you are. This information gets embedded in your commits, so it needs to be accurate.
git config --global user.name "Your Name"
git config --global user.email "your.email@example.com"
The --global flag means these settings apply to all Git repositories on your machine. If you need to use a different name or email for a specific project (e.g., for a work project vs. a personal one), you can omit --global and run the command inside that project’s directory. Git will then use these local settings for that repo, overriding the global ones.
Now, let’s create a new project and initialize Git. Navigate to your project’s root directory in your terminal. If it doesn’t exist, create it:
mkdir my-awesome-project
cd my-awesome-project
Initialize Git in this directory:
git init
This command creates a hidden .git subdirectory within your project. This is where Git stores all its metadata: the commit history, configuration, hooks, and more. It’s the brain of your repository.
You’ll see output like: Initialized empty Git repository in /path/to/my-awesome-project/.git/.
Now, let’s add a file. Create a simple README.md:
echo "# My Awesome Project" > README.md
To track this file, you need to stage it. Staging is an intermediate step between your working directory and your repository’s history. It’s where you select which changes you want to include in your next commit.
git add README.md
You can check the status of your repository at any time:
git status
After git add, git status will show README.md under "Changes to be committed."
Finally, commit your staged changes. A commit is a snapshot of your repository at a specific point in time, along with a message explaining what changed.
git commit -m "Initial commit: Add README file"
The -m flag allows you to provide a commit message directly on the command line. Good commit messages are crucial for understanding project history. The first line should be a concise summary (ideally under 50 characters), followed by a blank line, and then a more detailed explanation if necessary.
At this point, your local repository is set up and has its first commit. You’ve created a complete, independent Git repository on your machine.
What most people don’t realize is that the git add command, by default, stages all changes to all files in the current directory and its subdirectories. If you have multiple files modified and only want to commit some of them, you can explicitly list the files: git add file1.txt file2.py. Alternatively, git add . stages all changes in the current directory and subdirectories, while git add -u stages only modifications to tracked files, ignoring new untracked files.
The next logical step is to connect this local repository to a remote one, like on GitHub or GitLab, so you can collaborate and back up your work.