What is Git?
Git is a distributed version control system that tracks changes in any set of computer files, typically used for coordinating work among programmers collaboratively developing source code during software development.
Why Git is Essential for Developers
Git has become an indispensable tool in modern software development for several critical reasons:
- Version Control: Git allows you to track every change made to your codebase over time, creating a complete history of your project. This makes it possible to view previous versions and revert to earlier states if necessary.
- Collaboration: Git enables multiple developers to work on the same project simultaneously without overwriting each other’s changes. It provides mechanisms for merging changes and resolving conflicts.
- Branching and Experimentation: With Git, you can create separate branches to experiment with new features or fixes without affecting the main codebase. This allows for parallel development streams and safer experimentation.
- Backup and Redundancy: Since Git is distributed, every developer has a complete copy of the repository. This creates natural redundancy and protects against data loss.
- Project Management: Git integrates with tools like GitHub, GitLab, and Bitbucket to add features such as issue tracking, pull requests, and code reviews that streamline the development lifecycle.
- Portfolio Building: For career development, Git platforms like GitHub serve as a portfolio showcasing your code and contributions to projects.
Essential Git Commands
Setup and Configuration
# Initialize a new Git repository
git init
# Clone an existing repository
git clone https://github.com/username/repository.git
# Configure global username
git config --global user.name "Your Name"
# Configure global email
git config --global user.email "your.email@example.com"
# View configuration settings
git config --list
Basic Workflow
# Check status of your working directory
git status
# Add files to staging area
git add filename.txt
# Add all modified files to staging area
git add .
# Commit your changes with a message
git commit -m "Descriptive commit message"
# Commit all modified files directly (skips staging)
git commit -am "Commit message"
# View commit history
git log
# View compact commit history
git log --oneline
Working with Remotes
# List remote repositories
git remote -v
# Add a remote repository
git remote add origin https://github.com/username/repository.git
# Fetch changes from remote repository
git fetch origin
# Pull changes from remote repository (fetch + merge)
git pull origin main
# Push local changes to remote repository
git push origin main
# Push and set upstream branch
git push -u origin feature-branch
Branching and Merging
# List all branches
git branch
# Create a new branch
git branch new-feature
# Switch to a branch
git checkout feature-branch
# Create and switch to a new branch (shorthand)
git checkout -b new-feature
# Merge a branch into your current branch
git merge feature-branch
# Delete a branch locally
git branch -d feature-branch
# Delete a branch on the remote
git push origin --delete feature-branch
Stashing Changes
# Temporarily save uncommitted changes
git stash
# List all stashed changes
git stash list
# Apply the most recent stash
git stash apply
# Apply a specific stash
git stash apply stash@{2}
# Apply and remove the most recent stash
git stash pop
# Clear all stashed changes
git stash clear
Inspecting and Comparing
# Show changes between working directory and staging
git diff
# Show changes between staging and last commit
git diff --staged
# Show changes between two commits
git diff commit1..commit2
# Show changes in a file
git diff -- path/to/file
# Show who changed what in a file
git blame filename.txt
Undoing Changes
# Discard changes in working directory
git restore filename.txt
# Unstage a file
git restore --staged filename.txt
# Amend the most recent commit
git commit --amend
# Revert a commit (creates a new commit that undoes changes)
git revert commit-hash
# Reset to a previous commit (dangerous for shared branches)
git reset --hard commit-hash
Advanced Operations
# Cherry-pick a commit from another branch
git cherry-pick commit-hash
# Interactive rebase to clean up commits
git rebase -i HEAD~3
# Create a tag
git tag v1.0.0
# Create an annotated tag
git tag -a v1.0.0 -m "Version 1.0.0"
# Push tags to remote
git push --tags
# Create a patch
git format-patch master --stdout > patch.diff
# Apply a patch
git apply patch.diff
Workflow Examples
# Start a new feature
git checkout -b feature-x
# Make changes
git add .
git commit -m "Add feature X"
git push -u origin feature-x
# Update your feature branch with main
git checkout feature-x
git fetch origin
git rebase origin/main
# Create a hotfix
git checkout -b hotfix-bug main
# Fix the bug
git add .
git commit -m "Fix critical bug"
git checkout main
git merge hotfix-bug
git push origin main
Git Workflows
# GitFlow - Start a feature
git flow feature start feature-name
# GitHub Flow - Create a pull request (after pushing branch)
# Use GitHub UI to create PR
# Trunk-Based Development
git checkout main
# Make small changes
git commit -am "Small feature addition"
git push origin main
Best Practices
- Commit Often: Make small, focused commits with clear messages.
- Pull Before Push: Always pull the latest changes before pushing to avoid conflicts.
- Use Branches: Create branches for features, fixes, and experiments.
- Write Meaningful Commit Messages: Use descriptive, present-tense messages.
- Review Changes Before Committing: Use
git diff
andgit status
to check what you’re committing. - Don’t Commit Generated Files: Use
.gitignore
to exclude build artifacts, dependencies, and environment-specific files. - Protect Your Main Branch: Consider making main/master branch protected to require code reviews.
- Regularly Clean Your Repository: Delete merged branches and use
git gc
to optimize.