Mastering Git: Key Commands Explained
Table of contents
No headings in the article.
In this article, I'll help you understand Git commands and workflows in a structured way. Let's build up your knowledge from the fundamentals to more advanced concepts.
First, let's understand what Git is: Git is a distributed version control system that helps track changes in your code over time. Think of it like a time machine for your code that also enables collaboration.
Let's start with the basic workflow:
- Local Git Workflow
# Initialize a new Git repository
git init # Creates a new .git directory to track your project
# Check status of your files
git status # Shows which files are tracked, modified, or untracked
# Add files to staging area
git add filename # Stage specific file
git add . # Stage all files in current directory
# Commit your changes
git commit -m "Your message" # Save staged changes with a descriptive message
Understanding the Three Areas in Git:
Working Directory: Where you actually work on your files
Staging Area (Index): A temporary area where you prepare changes for committing
Repository: Where Git permanently stores changes as commits
- Branch Management
# Create and switch to a new branch
git branch feature-name # Create branch
git checkout feature-name # Switch to branch
# Or do both at once:
git checkout -b feature-name
# List branches
git branch # List local branches
git branch -a # List all branches including remote
# Delete branch
git branch -d feature-name # Safe delete (won't delete if changes aren't merged)
git branch -D feature-name # Force delete
- Remote Repository Operations
# Connect to remote repository
git remote add origin <repository-url>
# Push changes
git push origin branch-name # Send commits to remote repository
# Pull changes
git pull origin branch-name # Fetch and merge changes from remote
# Fetch changes without merging
git fetch origin # Download changes but don't merge them
- Merging and Conflict Resolution
# Merge a branch into current branch
git merge feature-branch
# If conflicts occur, you'll need to:
# 1. Open conflicted files (marked with <<<<<<< and >>>>>>>)
# 2. Resolve conflicts manually
# 3. git add the resolved files
# 4. git commit to complete the merge
- History and Inspection
# View commit history
git log # Show commit history
git log --oneline # Compact view
git log --graph --oneline # Visual representation of branches
# See changes
git diff # Show unstaged changes
git diff --staged # Show staged changes
- Advanced Operations
# Stash changes temporarily
git stash # Save changes without committing
git stash pop # Restore stashed changes
# Reset changes
git reset --soft HEAD~1 # Undo last commit, keep changes staged
git reset --hard HEAD~1 # Undo last commit and discard changes
# Rebase (rewrite history)
git rebase main # Rebase current branch onto main
Common Workflow Example:
- Start a new feature:
git checkout main
git pull origin main
git checkout -b new-feature
- Make changes and commit:
# Edit files
git add .
git commit -m "Implement new feature"
- Keep up with main branch:
git checkout main
git pull origin main
git checkout new-feature
git merge main # Or git rebase main
- Push and create pull request:
git push origin new-feature
Important Tips:
Always write clear, descriptive commit messages
Commit often but logically - each commit should represent one complete change
Pull changes from the main branch regularly to avoid major conflicts
Use branches for new features/bugfixes
Review changes before committing using
git status
andgit diff