Ready to get hands-on with Git? After understanding the fundamentals of version control and the differences between VCS types, it's time to install Git and create your first repository. This tutorial follows a real terminal session, explaining every command and its output in detail.
šÆ What You'll Learn: In this hands-on tutorial, you'll discover:
- How to download and install Git on your operating system
- Step-by-step verification that Git is properly installed
- Essential Git configuration for collaboration
- What a Git repository actually is and how to create one
- Understanding the mysterious .git folder and its contents
- The difference between working directory and repository
- Real terminal output with detailed explanations of every message
Prerequisites: Understanding of VCS concepts from Parts 1 & 2
š¾ Installing Git: Your First Step
Download Git from Official Source
The first step is downloading Git from the official website. This ensures you get the latest, secure version directly from the Git developers.
Download Location: Visit git-scm.com
Why use the official site?
- ā Latest Version: Always get the most recent stable release
- ā Security: Official builds are properly signed and verified
- ā OS Detection: Website automatically detects your operating system
- ā Documentation: Access to complete installation guides
Operating System Specific Installation
Operating System | Installation Method | Notes |
---|---|---|
Windows | Download .exe installer from git-scm.com | Includes Git Bash terminal |
macOS | Download .dmg installer or use Homebrew | May come pre-installed with Xcode |
Linux (Ubuntu/Debian) | sudo apt update && sudo apt install git | Available in package managers |
Linux (CentOS/RHEL) | sudo yum install git or sudo dnf install git | Use dnf for newer versions |
ā Verifying Git Installation
After installation, the first thing to do is verify that Git is properly installed and accessible from your terminal.
Testing Git Installation with Command
Let's run the basic git
command to see if Git is installed:
[centos9@vbox random 16:22:25]$ git
Command Explanation:
git
- The base Git command without any parameters- Purpose: Shows the main Git help screen and available commands
- When to use: To verify Git is installed and see basic command overview
Understanding Git's Help Output
When you run git
without any parameters, Git displays its usage information:
usage: git [-v | --version] [-h | --help] [-C <path>] [-c <name>=<value>]
[--exec-path[=<path>]] [--html-path] [--man-path] [--info-path]
[-p | --paginate | -P | --no-pager] [--no-replace-objects] [--no-lazy-fetch]
[--no-optional-locks] [--no-advice] [--bare] [--git-dir=<path>]
[--work-tree=<path>] [--namespace=<name>] [--config-env=<name>=<envvar>]
<command> [<args>]
What This Output Means:
- usage: Shows the basic syntax for Git commands
- Options in brackets: Optional parameters you can use with Git
<command> [<args>]
: Indicates Git expects a subcommand and optional arguments
Git Commands by Category
The help output organizes Git commands into logical categories:
Category | Key Commands | Purpose |
---|---|---|
Start a working area | clone , init | Create or copy repositories |
Work on current change | add , mv , restore , rm | Stage and manage files |
Examine history and state | bisect , diff , log , show , status | View changes and history |
Grow, mark and tweak | branch , commit , merge , rebase , tag | Record and organize changes |
Collaborate | fetch , pull , push | Sync with remote repositories |
Checking Git Version
Let's verify the specific version of Git installed:
[centos9@vbox random 16:22:26]$ git -v
git version 2.47.3
Command Explanation:
git -v
orgit --version
- Shows the installed Git version- Purpose: Verify you have a recent version and for troubleshooting
- Output:
git version 2.47.3
indicates Git version 2.47.3 is installed
ā Version Check Passed: Version 2.47.3 is a recent release with all modern features. Any version 2.20+ is recommended for beginners.
āļø Essential Git Configuration
Before creating your first repository, you need to configure Git with your identity. This information will be associated with every commit you make.
Why Git Configuration Matters
Why set user.name and user.email?
In collaborative environments, Git needs to track who made each change. Every commit in Git includes:
- Author information: Who wrote the code
- Timestamp: When the change was made
- Commit message: What the change does
Without proper configuration, your commits might be attributed to "unknown" or show confusing author information.
Setting Up Your Git Identity
Here are the essential configuration commands (shown as comments in our session since they were already set):
# Set your name (replace with your actual name)
git config --global user.name "Your Name"
# Set your email (replace with your actual email)
git config --global user.email "your.email@example.com"
Command Breakdown:
git config
- The Git configuration management command--global
- Apply this setting to all repositories on your systemuser.name
- The configuration key for your nameuser.email
- The configuration key for your email address
Checking Current Configuration
Let's see what configuration is currently set:
[centos9@vbox random 16:25:00]$ git config --global user.name
Owais Abbasi
[centos9@vbox random 16:25:12]$ git config --global user.email
owais.abbasi9@gmail.com
Command Explanation:
git config --global user.name
- Shows the currently configured namegit config --global user.email
- Shows the currently configured email- Output: Returns the values that are currently set
Changing Git Configuration
If you need to change your configuration later:
# These commands were shown in comments in our session
git config --global user.name="Abc"
git config --global user.email="abc@gmail.com"
When to change configuration:
- New job: Different work email and potentially different name format
- Personal vs Work: Switch between personal and work identities
- Correction: Fix typos in your name or email
š Understanding Git Repositories
What is a Repository?
A repository (often called "repo") is a directory that contains your project files plus Git's tracking information. Think of it as a folder that has been "supercharged" with version control capabilities.
Repository Components:
- Working Directory: Your actual project files that you edit
- Git Database: Hidden
.git
folder containing all version history - Staging Area: Temporary space for changes before committing
Analogy: Think of a repository like a photo album with a time machine:
- Working Directory = Current photos you're organizing
- Staging Area = Photos selected for the next album page
- Git Database = Complete album with every page (snapshot) ever created
Creating Your Project Structure
Let's follow the exact steps from our terminal session to create a project:
[centos9@vbox random 16:43:52]$ ls -al
total 0
drwxr-xr-x. 2 centos9 centos9 6 Sep 22 14:50 .
drwxr-xr-x. 5 centos9 centos9 88 Sep 2 23:06 ..
[centos9@vbox random 16:43:58]$ mkdir Repo
[centos9@vbox random 16:44:07]$ cd Repo/
[centos9@vbox Repo 16:44:19]$ mkdir myproject
[centos9@vbox Repo 16:44:28]$ cd myproject/
Command Breakdown:
ls -al
- List all files and directories with detailed informationmkdir Repo
- Create a directory named "Repo"cd Repo/
- Change into the Repo directorymkdir myproject
- Create a subdirectory for our projectcd myproject/
- Change into the project directory
Understanding Directory Structure
[centos9@vbox myproject 16:44:33]$ ls -al
total 0
drwxr-xr-x. 2 centos9 centos9 6 Sep 22 16:44 .
drwxr-xr-x. 3 centos9 centos9 23 Sep 22 16:44 ..
Output Explanation:
total 0
- No files in the directory (0 blocks used)drwxr-xr-x.
- Directory permissions and attributes.
- Current directory (myproject)..
- Parent directory (Repo)- Notice: No
.git
folder yet - this is just a regular directory
š Creating Your First Git Repository
Initializing a Git Repository
Now comes the magic moment - transforming a regular directory into a Git repository:
[centos9@vbox myproject 16:44:50]$ git init
hint: Using 'master' as the name for the initial branch. This default branch name
hint: is subject to change. To configure the initial branch name to use in all
hint: of your new repositories, which will suppress this warning, call:
hint:
hint: git config --global init.defaultBranch <name>
hint:
hint: Names commonly chosen instead of 'master' are 'main', 'trunk' and
hint: 'development'. The just-created branch can be renamed via this command:
hint:
hint: git branch -m <name>
Initialized empty Git repository in /home/centos9/Razzaq-Labs-II/random/Repo/myproject/.git/
Command Explanation:
git init
- Initialize a new Git repository in the current directory- Purpose: Transform a regular folder into a Git repository
- What happens: Creates the
.git
folder with all necessary Git infrastructure
Understanding Git's Output Messages
Let's break down each part of Git's response:
Message Part | Explanation | Action Needed |
---|---|---|
Branch Name Hint | Git is using 'master' as default branch name | Optional: Can change to 'main' for modern conventions |
Alternative Names | Suggests modern branch names (main, trunk, development) | None - this is just informational |
Success Message | "Initialized empty Git repository" | None - repository is ready to use |
Repository Path | Shows exact location of .git folder | Remember this path for troubleshooting |
Verifying Repository Creation
Let's check what changed after running git init
:
[centos9@vbox myproject 16:45:01]$ ls -l
total 0
[centos9@vbox myproject 16:45:19]$ ls -al
total 0
drwxr-xr-x. 3 centos9 centos9 18 Sep 22 16:45 .
drwxr-xr-x. 3 centos9 centos9 23 Sep 22 16:44 ..
drwxr-xr-x. 7 centos9 centos9 119 Sep 22 16:45 .git
Key Differences:
ls -l
- Shows no visible files (same as before)ls -al
- Now shows the.git
directory!- Important: The
.git
folder is hidden (starts with.
) - Size change: Directory now contains 119 bytes of Git infrastructure
š” Hidden Files: The .git
folder is hidden by default. Use ls -al
(not just ls
) to see hidden files and directories that start with a dot.
š Exploring the .git Folder
What's Inside the .git Directory?
The .git
folder contains all of Git's magic. Let's explore its contents:
[centos9@vbox myproject 16:45:22]$ cd .git/
[centos9@vbox .git 16:45:27]$ ls
branches config description HEAD hooks info objects refs
[centos9@vbox .git 16:45:29]$ ls -al
total 16
drwxr-xr-x. 7 centos9 centos9 119 Sep 22 16:45 .
drwxr-xr-x. 3 centos9 centos9 18 Sep 22 16:45 ..
drwxr-xr-x. 2 centos9 centos9 6 Sep 22 16:45 branches
-rw-r--r--. 1 centos9 centos9 92 Sep 22 16:45 config
-rw-r--r--. 1 centos9 centos9 73 Sep 22 16:45 description
-rw-r--r--. 1 centos9 centos9 23 Sep 22 16:45 HEAD
drwxr-xr-x. 2 centos9 centos9 4096 Sep 22 16:45 hooks
drwxr-xr-x. 2 centos9 centos9 21 Sep 22 16:45 info
drwxr-xr-x. 4 centos9 centos9 30 Sep 22 16:45 objects
drwxr-xr-x. 4 centos9 centos9 31 Sep 22 16:45 refs
Understanding Each .git Component
Component | Type | Purpose |
---|---|---|
branches | Directory | Legacy branch storage (rarely used in modern Git) |
config | File (92 bytes) | Repository-specific configuration settings |
description | File (73 bytes) | Repository description for web interfaces |
HEAD | File (23 bytes) | Points to current branch/commit |
hooks | Directory (4096 bytes) | Scripts that run at specific Git events |
info | Directory | Additional repository information and global excludes |
objects | Directory | The heart of Git: All commits, files, and trees stored here |
refs | Directory | References to commits (branches, tags) |
ā ļø Important: Never manually modify files in the .git
directory unless you're an advanced user. Git manages this folder automatically, and manual changes can corrupt your repository.
Working Directory vs Repository
Let's return to our working directory to understand the relationship:
[centos9@vbox .git 16:45:32]$ cd ..
[centos9@vbox myproject 16:45:38]$
Directory Structure Now:
myproject/ <- Working Directory (where you edit files)
āāā .git/ <- Git Repository (where Git stores history)
ā āāā branches/
ā āāā config
ā āāā description
ā āāā HEAD
ā āāā hooks/
ā āāā info/
ā āāā objects/ <- Your commits and file history live here
ā āāā refs/
āāā (your project files will go here)
š Understanding Repository Status
Checking Repository Status
Now let's check the status of our new repository:
[centos9@vbox myproject 16:45:38]$ git status
On branch master
No commits yet
nothing to commit (create/copy files and use "git add" to track)
Breaking Down Git Status Output
Status Line | Meaning | What It Tells You |
---|---|---|
"On branch master" | Currently on the master branch | Which timeline/branch you're working on |
"No commits yet" | Repository has no snapshots | This is a brand new, empty repository |
"nothing to commit" | No changes ready to save | Working directory is clean |
"create/copy files" | Suggestion for next step | Add some files to start tracking changes |
šÆ Key Concepts Summary
Working Directory Explained
Working Directory: The folder where you create and edit your project files. This is the "normal" part of your project that you interact with daily.
Key Points:
- Contains your actual project files (code, documents, images, etc.)
- This is where you make changes to your project
- Git watches this directory for changes
- Files here can be in different states: untracked, modified, staged, etc.
Repository vs Working Directory
Aspect | Working Directory | Git Repository (.git folder) |
---|---|---|
Purpose | Where you work on files | Where Git stores history |
Visibility | Visible files you edit | Hidden, managed by Git |
Content | Current version of your files | All versions of your files |
Interaction | Direct editing with any tool | Managed through Git commands |
šÆ Key Takeaways
ā Essential Concepts Mastered
-
Git Installation: Downloaded from git-scm.com and verified with
git --version
-
Configuration Setup: Set user.name and user.email for commit attribution
-
Repository Creation: Used
git init
to transform a directory into a Git repository -
Hidden .git Folder: Contains all Git's tracking data and history
-
Working Directory: The visible folder where you edit your project files
-
Git Status: Shows current state of repository and suggestions for next steps
-
Branch Concept: Started on 'master' branch (modern projects often use 'main')
š Congratulations! You have successfully:
- ā Installed Git and verified it works
- ā Configured Git with your identity
- ā Created your first Git repository
- ā Understood the structure of a Git repository
- ā Learned the difference between working directory and Git database
You're now ready to start tracking files and making commits! The next tutorial will show you how to add files, understand file status, and create your first snapshots.
š¬ Discussion
How did your Git installation and setup go?
- Did you encounter any issues during installation on your operating system?
- Which part of the .git folder structure was most surprising to you?
- Do you understand the relationship between working directory and repository?
- What questions do you have about Git configuration or repository setup?