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

  1. Download from git-scm.com
  2. 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

CommandDescription
git initInitialize new repository
git clone <url>Clone existing repository
git statusView 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

  1. Identify conflicted files with git status
  2. Open files and resolve marked conflicts
  3. 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

  1. Feature Branch Workflow:
    git checkout -b new-feature
    git push -u origin new-feature
    
  2. Git Flow:
    git flow feature start myfeature
    git flow feature finish myfeature
    
  3. Forking Workflow:
    git remote add fork https://github.com/yourname/repo.git
    git push fork main
    

Troubleshooting Guide

Common Issues

  1. 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"
    
  2. Recover deleted branch:

    git reflog
    git checkout -b recovered-branch <commit-hash>
    
  3. Clean untracked files:

    git clean -fd
    

Git Best Practices

  1. 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
      
  2. Branch Naming:

    • feature/checkout-flow
    • bugfix/login-validation
    • hotfix/database-connection
  3. .gitignore Essentials:

    # Node.js example
    node_modules/
    .env
    *.log
    dist/
    

Learning Resources

  1. Pro Git Book - Free official documentation
  2. GitHub Training - Interactive tutorials
  3. 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