Git
Git is a distributed version control system designed to handle everything from small to very large projects with speed and efficiency. Created by Linus Torvalds in 2005, Git tracks changes in files and coordinates work among multiple developers.
Core Concepts
Section titled “Core Concepts”Repository (Repo)
Section titled “Repository (Repo)”A Git repository contains the complete history of a project, including all files, commits, and branches. Repositories can be local (on your machine) or remote (hosted on services like GitHub, GitLab, or Bitbucket).
Working Directory, Staging Area, and Repository
Section titled “Working Directory, Staging Area, and Repository”Git uses a three-stage workflow:
- Working Directory: Where you edit files
- Staging Area (Index): Where you prepare changes for commit
- Repository: Where committed changes are permanently stored
Commits
Section titled “Commits”A commit is a snapshot of your project at a specific point in time. Each commit has a unique hash identifier and contains metadata like author, timestamp, and commit message.
Branches
Section titled “Branches”Branches allow you to diverge from the main line of development and work on features independently. The default branch is typically called main or master.
Essential Commands
Section titled “Essential Commands”Repository Setup
Section titled “Repository Setup”# Initialize a new repositorygit init
# Clone an existing repositorygit clone <repository-url>
# Add remote repositorygit remote add origin <repository-url>Basic Workflow
Section titled “Basic Workflow”# Check repository statusgit status
# Add files to staging areagit add <filename>git add . # Add all changesgit add -A # Add all changes including deletions
# Commit changesgit commit -m "Descriptive commit message"git commit -am "Add and commit in one step" # For tracked files only
# Push changes to remote repositorygit push origin <branch-name>git push # Push to default upstream branchViewing History
Section titled “Viewing History”# View commit historygit loggit log --oneline # Condensed viewgit log --graph --oneline --all # Visual branch representation
# Show changes in a commitgit show <commit-hash>
# View differencesgit diff # Working directory vs staging areagit diff --staged # Staging area vs last commitgit diff <commit1> <commit2> # Between two commitsBranch Management
Section titled “Branch Management”# List branchesgit branch # Local branchesgit branch -r # Remote branchesgit branch -a # All branches
# Create and switch to new branchgit checkout -b <branch-name>git switch -c <branch-name> # Modern alternative
# Switch between branchesgit checkout <branch-name>git switch <branch-name> # Modern alternative
# Merge branchesgit checkout maingit merge <feature-branch>
# Delete branchgit branch -d <branch-name> # Safe deletegit branch -D <branch-name> # Force deleteRemote Operations
Section titled “Remote Operations”# Fetch changes from remotegit fetch origin
# Pull changes (fetch + merge)git pull origin <branch-name>git pull # From default upstream
# Push new branch to remotegit push -u origin <branch-name>Undoing Changes
Section titled “Undoing Changes”# Discard changes in working directorygit checkout -- <filename>git restore <filename> # Modern alternative
# Unstage filesgit reset HEAD <filename>git restore --staged <filename> # Modern alternative
# Undo last commit (keep changes)git reset --soft HEAD~1
# Undo last commit (discard changes)git reset --hard HEAD~1
# Revert a commit (creates new commit)git revert <commit-hash>Advanced Workflows
Section titled “Advanced Workflows”Rebasing
Section titled “Rebasing”Rebasing rewrites commit history by moving commits to a new base commit, creating a cleaner, linear history.
# Rebase current branch onto maingit rebase main
# Interactive rebase (edit last 3 commits)git rebase -i HEAD~3
# Continue after resolving conflictsgit rebase --continue
# Abort rebasegit rebase --abortStashing
Section titled “Stashing”Temporarily save changes without committing them.
# Stash current changesgit stashgit stash save "Work in progress on feature X"
# List stashesgit stash list
# Apply stashgit stash apply # Keep stash in listgit stash pop # Apply and remove from list
# Apply specific stashgit stash apply stash@{2}Cherry-picking
Section titled “Cherry-picking”Apply specific commits from one branch to another.
# Apply a specific commit to current branchgit cherry-pick <commit-hash>
# Cherry-pick without committinggit cherry-pick -n <commit-hash>Git Configuration
Section titled “Git Configuration”Global Configuration
Section titled “Global Configuration”# Set user informationgit config --global user.name "Your Name"git config --global user.email "your.email@example.com"
# Set default editorgit config --global core.editor "nvim"
# Set default branch namegit config --global init.defaultBranch main
# Enable color outputgit config --global color.ui autoUseful Aliases
Section titled “Useful Aliases”# Add helpful aliasesgit config --global alias.st statusgit config --global alias.co checkoutgit config --global alias.br branchgit config --global alias.ci commitgit config --global alias.unstage 'reset HEAD --'git config --global alias.last 'log -1 HEAD'git config --global alias.visual '!gitk'Best Practices
Section titled “Best Practices”Commit Messages
Section titled “Commit Messages”Follow the conventional commit format:
<type>(<scope>): <subject>
<body>
<footer>Examples:
feat(auth): add user login functionality
Implement JWT-based authentication with email and password.Includes password validation and remember me option.
Closes #123Branching Strategy
Section titled “Branching Strategy”- main/master: Production-ready code
- develop: Integration branch for features
- feature/: Feature development branches
- hotfix/: Emergency fixes for production
- release/: Preparation for new releases
File Management
Section titled “File Management”# Create .gitignore file for common exclusionsecho "node_modules/" >> .gitignoreecho ".env" >> .gitignoreecho "*.log" >> .gitignoreecho ".DS_Store" >> .gitignoreCommon Scenarios
Section titled “Common Scenarios”Resolving Merge Conflicts
Section titled “Resolving Merge Conflicts”# When conflicts occur during mergegit status # See conflicted files# Edit files to resolve conflictsgit add <resolved-files>git commit # Complete the mergeCollaborating with Others
Section titled “Collaborating with Others”# Update your local repositorygit fetch origingit pull origin main
# Create feature branchgit checkout -b feature/new-functionality
# Work on feature, commit changesgit add .git commit -m "Add new functionality"
# Push feature branchgit push -u origin feature/new-functionality
# Create pull request on GitHub/GitLab# After review and merge, clean upgit checkout maingit pull origin maingit branch -d feature/new-functionalityEmergency Hotfix
Section titled “Emergency Hotfix”# Create hotfix from maingit checkout maingit pull origin maingit checkout -b hotfix/critical-bug-fix
# Fix the issue and commitgit add .git commit -m "hotfix: resolve critical security vulnerability"
# Push and create immediate pull requestgit push -u origin hotfix/critical-bug-fixTroubleshooting
Section titled “Troubleshooting”Common Issues
Section titled “Common Issues”Accidentally committed to wrong branch:
git reset --soft HEAD~1 # Undo commit, keep changesgit stash # Stash changesgit checkout correct-branchgit stash pop # Apply changes to correct branchNeed to change last commit message:
git commit --amend -m "Corrected commit message"Remove file from Git but keep locally:
git rm --cached <filename>git commit -m "Remove file from version control"Integration and Related Tools
Section titled “Integration and Related Tools”Git integrates seamlessly with development workflows and has many complementary tools:
Workflow Integration:
- Code editors: VS Code, NeoVim, and other editors with Git plugins
- CI/CD pipelines: GitHub Actions, GitLab CI for automated testing and deployment
- Project management: Issue tracking and pull requests for collaborative development
- Code quality: Pre-commit hooks with linters and formatters
Alternative Interfaces:
- GitHub CLI: Command-line interface for GitHub operations
- GitKraken: Visual Git client with branch visualization
- SourceTree: Free Git GUI for Windows and Mac
- lazygit: Terminal-based Git interface with intuitive navigation