Git – Distributed Version Control System

4.9 Stars
Version 2.43
50 MB

Comprehensive Guide to Git: Mastering Version Control

Git has fundamentally transformed how software is developed, enabling distributed version control that tracks every change to your codebase with precision and reliability. Created by Linus Torvalds in 2005 for Linux kernel development, Git has become the industry standard for source code management. Its distributed architecture means every developer has a complete copy of the repository, enabling offline work, multiple remote collaborations, and unprecedented flexibility in development workflows.

Understanding Git goes beyond memorizing commands—it requires grasping the underlying model of commits, branches, and merges that enables powerful collaboration patterns. Whether working alone or with thousands of contributors, Git provides the tools to track changes, experiment safely, and maintain a clear history of project evolution.

Installing Git

Linux Installation

# Ubuntu/Debian
sudo apt update
sudo apt install git

# Fedora
sudo dnf install git

# Arch Linux
sudo pacman -S git

# Verify installation
git --version

# Configure identity
git config --global user.name "Your Name"
git config --global user.email "your.email@example.com"

# Useful global settings
git config --global init.defaultBranch main
git config --global core.editor "vim"
git config --global pull.rebase false
git config --global push.autoSetupRemote true

macOS Installation

# Xcode Command Line Tools
xcode-select --install

# Or via Homebrew
brew install git

git --version

Windows Installation

# Git for Windows
winget install Git.Git

# Or download from git-scm.com

git --version

Basic Git Workflow

# Initialize repository
git init
git init project-name

# Clone existing repository
git clone https://github.com/user/repo.git
git clone git@github.com:user/repo.git
git clone https://github.com/user/repo.git local-folder

# Check status
git status
git status -s  # Short format

# Stage changes
git add file.txt
git add *.js
git add .            # All changes
git add -A           # All including deletions
git add -p           # Interactive staging

# Commit changes
git commit -m "Commit message"
git commit -am "Add and commit"
git commit --amend   # Modify last commit
git commit --amend --no-edit

# View history
git log
git log --oneline
git log --graph --oneline --all
git log -p           # Show patches
git log --stat       # Show statistics
git log -n 5         # Last 5 commits
git log --author="Name"
git log --since="2024-01-01"

# View changes
git diff             # Unstaged changes
git diff --staged    # Staged changes
git diff HEAD        # All changes
git diff branch1..branch2

# Remove and move files
git rm file.txt
git rm --cached file.txt  # Keep file, remove from git
git mv old.txt new.txt

Branching and Merging

# List branches
git branch
git branch -a        # All including remote
git branch -v        # With last commit

# Create branch
git branch feature-name
git checkout -b feature-name  # Create and switch
git switch -c feature-name    # Modern syntax

# Switch branches
git checkout main
git switch main

# Merge branch
git checkout main
git merge feature-name
git merge --no-ff feature-name  # Create merge commit

# Delete branch
git branch -d feature-name      # Safe delete
git branch -D feature-name      # Force delete
git push origin --delete feature-name

# Rename branch
git branch -m old-name new-name
git branch -m new-name          # Current branch

# Rebase
git checkout feature
git rebase main
git rebase -i HEAD~3            # Interactive rebase

# Cherry-pick
git cherry-pick commit-hash
git cherry-pick commit1..commit3

Remote Repositories

# List remotes
git remote -v

# Add remote
git remote add origin https://github.com/user/repo.git
git remote add upstream https://github.com/original/repo.git

# Remove remote
git remote remove origin

# Fetch changes
git fetch origin
git fetch --all

# Pull changes
git pull
git pull origin main
git pull --rebase

# Push changes
git push
git push origin main
git push -u origin feature-name
git push --force-with-lease      # Safer force push
git push --tags                  # Push tags

# Track remote branch
git checkout --track origin/feature
git branch -u origin/main

Stashing Changes

# Stash changes
git stash
git stash push -m "Description"
git stash push -p              # Interactive

# List stashes
git stash list

# Apply stash
git stash pop                  # Apply and remove
git stash apply                # Apply and keep
git stash apply stash@{2}

# View stash
git stash show
git stash show -p stash@{0}

# Drop stash
git stash drop stash@{0}
git stash clear                # Drop all

Undoing Changes

# Discard working directory changes
git checkout -- file.txt
git restore file.txt           # Modern syntax
git checkout -- .              # All files

# Unstage files
git reset HEAD file.txt
git restore --staged file.txt

# Reset commits
git reset --soft HEAD~1        # Keep changes staged
git reset --mixed HEAD~1       # Keep changes unstaged
git reset --hard HEAD~1        # Discard changes

# Revert commit (creates new commit)
git revert commit-hash
git revert HEAD

# Recover deleted branch
git reflog
git checkout -b recovered commit-hash

Tags

# Create tag
git tag v1.0.0
git tag -a v1.0.0 -m "Version 1.0.0"
git tag v1.0.0 commit-hash

# List tags
git tag
git tag -l "v1.*"

# Show tag
git show v1.0.0

# Push tags
git push origin v1.0.0
git push origin --tags

# Delete tag
git tag -d v1.0.0
git push origin --delete v1.0.0

Git Aliases

# Create aliases
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.lg "log --graph --oneline --all"
git config --global alias.amend 'commit --amend --no-edit'

# Usage
git st
git lg

Advanced Git Operations

# Bisect (find bug introduction)
git bisect start
git bisect bad                 # Current is bad
git bisect good commit-hash    # Known good commit
# Git checks out commits to test
git bisect good/bad
git bisect reset

# Blame (find who changed line)
git blame file.txt
git blame -L 10,20 file.txt

# Clean (remove untracked files)
git clean -n                   # Dry run
git clean -f                   # Remove files
git clean -fd                  # Include directories

# Worktrees (multiple working directories)
git worktree add ../feature-branch feature
git worktree list
git worktree remove ../feature-branch

# Submodules
git submodule add https://github.com/lib/repo.git
git submodule update --init --recursive
git submodule foreach git pull origin main

.gitignore Patterns

# .gitignore
# Comments start with #

# Ignore specific file
secret.txt

# Ignore file type
*.log
*.tmp

# Ignore directory
node_modules/
dist/
.cache/

# Negation (don't ignore)
!important.log

# Ignore in specific directory
docs/*.txt

# Ignore everywhere
**/cache

# Global gitignore
git config --global core.excludesfile ~/.gitignore_global

Git Hooks

# Hooks location: .git/hooks/

# Pre-commit hook example
#!/bin/sh
# .git/hooks/pre-commit
npm run lint
if [ $? -ne 0 ]; then
    echo "Lint failed, commit aborted"
    exit 1
fi

# Make executable
chmod +x .git/hooks/pre-commit

# Common hooks:
# pre-commit: Before commit
# commit-msg: Validate commit message
# pre-push: Before push
# post-merge: After merge

# Husky for npm projects
npm install husky --save-dev
npx husky init

Debugging and Troubleshooting

# Verify repository
git fsck

# Garbage collection
git gc
git gc --aggressive

# View reflog
git reflog
git reflog show feature-branch

# Debug
GIT_TRACE=1 git status

# Large file issues
git rev-list --objects --all | sort -k 2 | cut -f 2 -d ' ' | uniq

# Fix corrupted index
rm .git/index
git reset

Conclusion

Git’s distributed architecture and powerful branching model have made it essential for modern software development. From simple personal projects to massive open-source collaborations, Git provides the foundation for tracking changes, coordinating work, and maintaining project history. Mastering Git commands and understanding its underlying concepts enables efficient collaboration and confident code management across any development workflow.

Developer: Git Project

Download Options

Download Git – Distributed Version Control System

Version 2.43

File Size: 50 MB

Download Now
Safe & Secure

Verified and scanned for viruses

Regular Updates

Always get the latest version

24/7 Support

Help available when you need it