Setup and First Steps
Install Git on your system, configure your environment, and create your first local repository.
Setup and First Steps
Now that you understand the core concepts of Git, it’s time to install and configure it on your system. This tutorial will walk you through installation, initial configuration, and creating your first Git repository.
Installing Git
Windows
Option 1: Direct download
- Visit git-scm.com
- Download the appropriate version (32-bit or 64-bit)
- Run the installer and follow the instructions
Option 2: Chocolatey
# Install Chocolatey first if needed
choco install git
Option 3: Winget
winget install --id Git.Git -e --source winget
macOS
Option 1: Homebrew (recommended)
# Install Homebrew first if needed
brew install git
Option 2: MacPorts
sudo port install git
Option 3: Direct download
Visit git-scm.com
Linux
Ubuntu/Debian
sudo apt update
sudo apt install git
CentOS/RHEL/Fedora
# CentOS/RHEL
sudo yum install git
# Fedora
sudo dnf install git
Arch Linux
sudo pacman -S git
Verifying the Installation
After installation, verify that Git is installed correctly:
git --version
You should see something like:
git version 2.41.0
Initial Configuration
Required global configuration
Git needs at least your name and email to work:
# Set your name
git config --global user.name "Your Name"
# Set your email
git config --global user.email "your.email@example.com"
Setting the default editor
# Visual Studio Code
git config --global core.editor "code --wait"
# Vim
git config --global core.editor "vim"
# Nano
git config --global core.editor "nano"
# Notepad++ (Windows)
git config --global core.editor "'C:/Program Files/Notepad++/notepad++.exe' -multiInst -notabbar -nosession -noPlugin"
Configuring line endings
Windows
git config --global core.autocrlf true
macOS/Linux
git config --global core.autocrlf input
Configuring colors
# Enable syntax highlighting
git config --global color.ui auto
Configuring the default branch
# Use 'main' as the default branch name
git config --global init.defaultBranch main
Verifying Your Configuration
Display your current configuration:
# View the entire configuration
git config --list
# View a specific setting
git config user.name
git config user.email
Configuration Levels
Git uses three configuration levels:
1. System configuration (—system)
- Affects all users on the system
- File:
/etc/gitconfig(Linux/macOS) orC:\Program Files\Git\etc\gitconfig(Windows)
2. Global configuration (—global)
- Affects the current user
- File:
~/.gitconfigor~/.config/git/config
3. Local configuration (—local)
- Affects only the current repository
- File:
.git/configin the repository
Configuration precedence
Local > Global > System
Creating Your First Repository
Method 1: Initialize a new repository
# Create a new folder
mkdir mon-premier-projet
cd mon-premier-projet
# Initialize the Git repository
git init
Method 2: Clone an existing repository
# Clone from GitHub
git clone https://github.com/utilisateur/depot.git
# Clone with a different name
git clone https://github.com/utilisateur/depot.git nouveau-nom
Structure of a Git Repository
After initialization, your folder contains:
mon-premier-projet/
├── .git/ # Git folder (hidden)
│ ├── config # Local configuration
│ ├── objects/ # Object database
│ ├── refs/ # References (branches, tags)
│ └── HEAD # Pointer to the current branch
└── (your files)
First Commit
Create a file
# Create a README file
echo "# Mon Premier Projet" > README.md
Add the file to the index
# Add the file to the staging area
git add README.md
# Or add all files
git add .
Create the commit
# Create the first commit
git commit -m "Premier commit : ajout du README"
Check the history
# View the commit history
git log
# Condensed version
git log --oneline
Advanced Configuration
Useful aliases
# Shortcuts for common commands
git config --global alias.st status
git config --global alias.co checkout
git config --global alias.br branch
git config --global alias.ci commit
git config --global alias.unstage 'reset HEAD --'
git config --global alias.last 'log -1 HEAD'
git config --global alias.visual '!gitk'
Merge and rebase configuration
# Default merge strategy
git config --global merge.tool vimdiff
# Pull behavior
git config --global pull.rebase false # merge (default)
# git config --global pull.rebase true # rebase
# git config --global pull.ff only # fast-forward only
GPG signing configuration (optional)
# Set the GPG key
git config --global user.signingkey YOUR_GPG_KEY_ID
# Sign all commits by default
git config --global commit.gpgsign true
Example .gitconfig File
Here is an example of a well-configured .gitconfig file:
[user]
name = Votre Nom
email = votre.email@example.com
[core]
editor = code --wait
autocrlf = true # Windows
# autocrlf = input # macOS/Linux
[init]
defaultBranch = main
[color]
ui = auto
[alias]
st = status
co = checkout
br = branch
ci = commit
unstage = reset HEAD --
last = log -1 HEAD
lg = log --oneline --graph --decorate --all
[pull]
rebase = false
[push]
default = simple
Hands-On Exercises
Exercise 1: Complete configuration
- Verify your Git installation
- Set your name and email
- Set your preferred editor
- Create a few useful aliases
- Verify your configuration
Exercise 2: First repository
- Create a new folder for a project
- Initialize a Git repository
- Create a
README.mdfile with a description - Add the file to the index
- Create your first commit
- Check the history
Exercise 3: Exploration
# Create several files
touch index.html style.css script.js
# Add some content
echo "<h1>Mon Site Web</h1>" > index.html
echo "body { font-family: Arial; }" > style.css
echo "console.log('Hello World!');" > script.js
# Add and commit
git add .
git commit -m "Ajout des fichiers de base du site web"
# Explore the history
git log --oneline
git show HEAD
Common Troubleshooting
Problem: “Author identity unknown”
# Solution: set your name and email
git config --global user.name "Votre Nom"
git config --global user.email "votre.email@example.com"
Problem: Editor won’t open
# Check the configuration
git config --global core.editor
# Reconfigure if needed
git config --global core.editor "code --wait"
Problem: Line endings on Windows
# Recommended configuration for Windows
git config --global core.autocrlf true
Recommended Graphical Tools
Free
- GitHub Desktop: Simple and effective
- SourceTree: Full-featured and professional
- GitKraken: Modern interface (limited free version)
Built into IDEs
- VS Code: Built-in Git extension
- IntelliJ IDEA: Native Git support
- Eclipse: EGit plugin
Configuration Best Practices
- Use aliases for frequently used commands
- Set an editor you are comfortable with
- Enable colors for better readability
- Configure line endings according to your OS
- Use a consistent default branch name (main)
Additional Resources
Documentation
Configuration tools
Conclusion
Congratulations! You now have Git installed and configured on your system. You have created your first repository and made your first commit. This solid foundation will let you explore Git’s more advanced features.
In the next tutorial, we’ll cover the essential Git commands you’ll use every day to manage your projects.
Estimated reading time: 20 minutes
Level: Beginner
Previous step: Introduction to Git
Next step: Essential Git Commands