Setting up Git and Initializing Repositories¶
A guide to getting started with Git, from installation to creating your first repository.
Installing Git¶
Windows¶
Official Installer¶
Download from https://git-scm.com/download/win and run the installer with default options.
Using Winget¶
Using Chocolatey¶
macOS¶
Using Homebrew¶
Using the official installer¶
Download from https://git-scm.com/download/mac and follow installation instructions.
Linux¶
Debian/Ubuntu¶
Fedora¶
RHEL/CentOS¶
Arch Linux¶
Verify installation¶
Initial Configuration¶
Setting Your Identity¶
Git requires knowing who you are for commit attribution:
Set your name¶
Set your email¶
git config --global user.email "[email protected]"
💡 Tip: Use the same email address you use for your remote Git hosting service (GitHub, GitLab, etc.).
Configuring Your Editor¶
Set your preferred text editor for commit messages:
For VS Code¶
For Vim¶
For Notepad++ (Windows)¶
git config --global core.editor "'C:/Program Files/Notepad++/notepad++.exe' -multiInst -notabbar -nosession -noPlugin"
For Sublime Text¶
Checking Your Settings¶
Review all your settings:
List all settings¶
Check a specific setting¶
Configuration Levels¶
Git configurations can be applied at three levels:
System level¶
Applies to all users:
Global level¶
Applies to all repositories of current user:
Local level¶
Applies only to current repository:
📌 Note: Local settings override global settings, which override system settings.
Creating Repositories¶
Initializing a New Repository¶
Turn an existing directory into a Git repository:
Navigate to your project directory¶
Initialize the repository¶
After initializing, you'll see a .git directory which contains all the Git metadata.
Cloning an Existing Repository¶
Copy a repository from a remote server:
Clone via HTTPS¶
Clone via SSH¶
Requires setup of SSH keys:
git clone [email protected]:username/repository.git
Clone to a specific directory¶
Clone a specific branch¶
Clone with limited history¶
Shallow clone:
Understanding the .git Directory¶
The .git directory contains all the data and configuration for your repository:
.git/
├── HEAD # Points to the current branch
├── config # Repository-specific configuration
├── description # Description of the repository (only used by GitWeb)
├── hooks/ # Client or server-side hook scripts
├── index # Staging area information
├── objects/ # Git's object database (commits, trees, blobs)
└── refs/ # Pointers to commit objects (branches, tags)
⚠️ Warning: Never manually modify files in the
.gitdirectory unless you know what you're doing.
First-time Repository Setup¶
Standard Project Files¶
When creating a new repository, consider adding these standard files:
Create a README file¶
Create a license file¶
Example: MIT
Create a .gitignore file¶
Discussed below:
Creating a .gitignore File¶
The .gitignore file specifies intentionally untracked files that Git should ignore:
Sample .gitignore content for various project types:
Node.js¶
Python¶
Java¶
💡 Tip: Use gitignore.io to generate
.gitignorefiles for your specific tech stack.
Command Summary¶
| Operation | Command | Description |
|---|---|---|
| Check version | git --version | Verify Git installation |
| Set username | git config --global user.name "Your Name" | Set your identity name |
| Set email | git config --global user.email "[email protected]" | Set your identity email |
| Set editor (VS Code) | git config --global core.editor "code --wait" | Configure default editor |
| Set editor (Vim) | git config --global core.editor "vim" | Configure default editor |
| Set editor (Notepad++) | git config --global core.editor "'C:/Program Files/Notepad++/notepad++.exe' -multiInst -notabbar -nosession -noPlugin" | Configure default editor |
| Set editor (Sublime) | git config --global core.editor "'subl' -w" | Configure default editor |
| List all settings | git config --list | Display all configurations |
| Check specific setting | git config user.name | View a specific config value |
| System config | git config --system ... | Set config for all users |
| Global config | git config --global ... | Set config for current user |
| Local config | git config --local ... | Set config for current repository |
| Initialize repository | git init | Create a new Git repository |
| Clone via HTTPS | git clone https://github.com/username/repository.git | Copy a repository from remote server |
| Clone via SSH | git clone [email protected]:username/repository.git | Clone using SSH protocol |
| Clone to directory | git clone https://github.com/username/repository.git my-project | Clone to specific folder |
| Clone specific branch | git clone -b branch-name https://github.com/username/repository.git | Clone only one branch |
| Shallow clone | git clone --depth=1 https://github.com/username/repository.git | Clone with limited history |
| Create gitignore | touch .gitignore | Create file to specify ignored files |