Introduction / Why This Is Needed
Git is a distributed version control system that tracks changes in files, primarily source code. It allows you to:
- Revert to any previous version of the project.
- Work on different tasks in parallel in separate branches.
- Collaborate with other developers by merging changes.
This guide is your first step. You will install Git, perform basic configuration, and complete your first operations: create a repository, make a commit, and clone a project from GitHub. All of this is done in the macOS terminal.
Requirements / Preparation
Before you begin, ensure you have:
- macOS (version 12 Monterey or newer).
- Terminal (built into macOS, located in
/Applications/Utilities/). - Homebrew (recommended for installing Git). If you don't have it, install it by running in the terminal:
/bin/bash -c "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/HEAD/install.sh)" - An account on GitHub, GitLab, or another Git hosting service (for the cloning step). You can create this later.
Step 1: Install Git
The simplest way to install Git on macOS is through Homebrew.
- Open Terminal.
- Update the Homebrew formula list:
brew update - Install Git:
brew install git - Verify the installation by running:
The output should look something like:git --versiongit version 2.42.0.
Alternative: Download the installer from the official Git website and run it, following the instructions.
Step 2: Configure Your Username and Email
Git attaches your name and email to every commit. This is important for tracking who changed what.
- In the terminal, run the command with your details:
For example:git config --global user.name "Your First Last Name"git config --global user.name "John Doe". - Specify your email (preferably the one linked to your GitHub account):
git config --global user.email "your.email@example.com" - Check your configuration:
In the output, look for thegit config --listuser.nameanduser.emaillines.
Step 3: Create a New Local Repository
Now let's create a project folder and initialize Git in it.
- Create a new folder for your project and navigate to it:
mkdir ~/Projects/my-first-repo cd ~/Projects/my-first-repo - Initialize an empty Git repository:
The terminal will respond:git initInitialized empty Git repository in /Users/your_name/Projects/my-first-repo/.git/. - Create a simple text file (for example,
README.md):echo "# My First Project" > README.md - Add the file to the staging area (index) — this prepares the file for a commit:
To add all files in the current folder, usegit add README.mdgit add .. - Create your first commit — a permanent record of changes:
Thegit commit -m "Added README.md with project description"-mflag allows you to specify the commit message directly.
Step 4: Clone an Existing Remote Repository
Often, you start working not with an empty folder, but with an already existing project on GitHub.
- Find the desired repository on GitHub (for example,
https://github.com/torvalds/linux). - Click the Code button and copy the URL (HTTPS is recommended).
- In the terminal, navigate to the folder where you want to place the project:
cd ~/Projects - Run the
git clonecommand with the copied URL:
Git will create agit clone https://github.com/torvalds/linux.gitlinuxfolder and download the entire project with all its commit history.
Step 5: View the Change History
The git log command helps you see what changed and when.
- Navigate to any initialized or cloned repository.
- Run:
git log
You will see a list of commits in reverse chronological order (newest at the top). Each commit shows:- commit (a unique hash, e.g.,
a1b2c3d). - Author (the author, from your configuration).
- Date (date and time).
- Message (your commit message).
For a concise output (one line per commit), usegit log --oneline. - commit (a unique hash, e.g.,
Step 6: Create and Switch to a New Branch
Branches allow you to isolate the development of new features or fixes from the main codebase (usually the main or master branch).
- Ensure you are on the
mainbranch:
An asteriskgit branch*will indicate the current branch. - Create a new branch named
feature-add-loginand immediately switch to it:
Or in two steps:git checkout -b feature-add-logingit branch feature-add-loginandgit checkout feature-add-login. - Now all new commits will go into this branch, without affecting
main. After finishing work, you can merge the branch back intomain.
Verification
You have successfully learned the basics if:
- The
git --versioncommand shows an installed Git version. git config --listcontains youruser.nameanduser.email.- You successfully created a local repository and made a commit (the
README.mdfile appears in thegit loghistory). - You cloned a remote repository and see its files in the folder.
- The
git branchcommand shows a list of branches, including the one you created.
Troubleshooting
| Problem | Possible Cause | Solution |
|---|---|---|
git: command not found | Git is not installed or not added to PATH. | Reinstall Git via Homebrew. Ensure the installation completed without errors. Restart Terminal. |
fatal: not a git repository | A Git command (e.g., git commit) is run in a folder that is not a repository. | Ensure you are inside the folder where git init or git clone was executed. Use pwd to check the current path. |
permission denied error during git clone | No write permissions in the target directory or an issue with SSH keys. | 1. Perform the clone in a folder where you have permissions (e.g., ~/Projects).2. For SSH access, ensure your public key ( ~/.ssh/id_rsa.pub) is added to your account settings on GitHub. |
Don't see changes after git add | The file was not changed or the wrong file was added. | Check the repository status: git status. It will show which files are modified, staged for commit (Changes to be committed), or untracked (Untracked files). |