The Complete Git Guide: Installation to Advanced Usage
December 8, 2024
:87 :1
Git is the most widely used distributed version control system, essential for modern software development. This guide covers everything from installation to advanced workflows used by professional teams.
Installation Guide
Windows
- Download from git-scm.com
- Recommended options:
- Select "Git Bash Here" integration
- Choose "Use Visual Studio Code as Git's default editor"
- Select "Git from the command line and also from 3rd-party software"
macOS
# Using Homebrew
brew install git
# Install Git GUI tools (optional)
brew install --cask github
Linux
# Debian/Ubuntu
sudo apt update && sudo apt install git -y
# RHEL/CentOS
sudo yum install git
# Verify installation
git --version # Should return git version 2.40+
Initial Configuration
Set Global Identity
git config --global user.name "Your Name"
git config --global user.email "your.email@example.com"
Configure Default Editor
git config --global core.editor "code --wait" # VS Code
Enable Color Output
git config --global color.ui auto
View All Configs
git config --list
Essential Git Commands
Repository Management
Command | Description |
---|---|
git init | Initialize new repository |
git clone <url> | Clone existing repository |
git status | View changed files |
Basic Workflow
# Stage changes
git add filename.txt # Specific file
git add . # All changes
# Commit changes
git commit -m "Descriptive message"
# Push to remote
git push origin main
Viewing History
git log --oneline --graph --decorate --all
git log -p filename.txt # View changes to specific file
Branching & Merging
Branch Operations
# Create and switch to new branch
git checkout -b feature-branch
# List all branches
git branch -av
# Delete branch
git branch -d old-branch
Merge Strategies
gitGraph
commit
branch feature
checkout feature
commit
commit
checkout main
merge feature
# Fast-forward merge
git merge feature-branch
# Recursive merge (with merge commit)
git merge --no-ff feature-branch
# Abort merge in progress
git merge --abort
Resolving Conflicts
- Identify conflicted files with
git status
- Open files and resolve marked conflicts
- Mark as resolved:
git add resolved-file.txt
git commit
Advanced Techniques
Stashing Changes
# Save uncommitted work
git stash push -m "Work in progress"
# List stashes
git stash list
# Apply and remove stash
git stash pop stash@{0}
Rewriting History
# Amend last commit
git commit --amend
# Interactive rebase (last 3 commits)
git rebase -i HEAD~3
# Reset to previous commit (CAUTION)
git reset --hard HEAD~1
Tagging Releases
# Create annotated tag
git tag -a v1.4.0 -m "Release version 1.4.0"
# Push tags to remote
git push origin --tags
Remote Repository Management
Working with Remotes
# Add new remote
git remote add upstream https://github.com/original/repo.git
# Fetch changes
git fetch upstream
# View remote URLs
git remote -v
Common Workflows
- Feature Branch Workflow:
git checkout -b new-feature git push -u origin new-feature
- Git Flow:
git flow feature start myfeature git flow feature finish myfeature
- Forking Workflow:
git remote add fork https://github.com/yourname/repo.git git push fork main
Troubleshooting Guide
Common Issues
-
Accidental commit to wrong branch:
git reset --soft HEAD~1 git stash git checkout correct-branch git stash pop git add . git commit -m "Message"
-
Recover deleted branch:
git reflog git checkout -b recovered-branch <commit-hash>
-
Clean untracked files:
git clean -fd
Git Best Practices
-
Commit Guidelines:
- Atomic commits (one logical change per commit)
- Conventional Commits format:
feat: add new payment endpoint fix: resolve login validation bug docs: update API documentation
-
Branch Naming:
feature/checkout-flow
bugfix/login-validation
hotfix/database-connection
-
.gitignore Essentials:
# Node.js example node_modules/ .env *.log dist/
Learning Resources
- Pro Git Book - Free official documentation
- GitHub Training - Interactive tutorials
- Git Visualizer - Visual learning tool
Pro Tip: Configure Git aliases for common commands in
~/.gitconfig
:
[alias] co = checkout br = branch ci = commit st = status lg = log --oneline --graph --decorate --all