🔧 Tools

Git Version Control Mastery

Last updated: 2025-09-25 12:47:03

Git Version Control

Git is the most popular version control system used by developers worldwide. Master the essential commands and workflows.

Basic Git Commands

# Initialize a new repository
git init

# Clone a remote repository
git clone https://github.com/user/repo.git

# Check status
git status

# Add files to staging area
git add filename.txt
git add .  # Add all files

# Commit changes
git commit -m "Your commit message"

# Push to remote repository
git push origin main

# Pull latest changes
git pull origin main

Branch Management

# Create and switch to new branch
git checkout -b feature-branch
# or (newer syntax)
git switch -c feature-branch

# List all branches
git branch
git branch -r  # Remote branches
git branch -a  # All branches

# Switch between branches
git checkout main
git switch main

# Merge branch
git checkout main
git merge feature-branch

# Delete branch
git branch -d feature-branch
git push origin --delete feature-branch

Advanced Git Operations

# View commit history
git log --oneline --graph --decorate

# Undo changes
git reset --soft HEAD~1  # Undo last commit, keep changes
git reset --hard HEAD~1  # Undo last commit, discard changes
git revert commit-hash   # Create new commit that undoes changes

# Stash changes
git stash                    # Save current changes
git stash list              # List all stashes
git stash apply             # Apply latest stash
git stash pop               # Apply and remove latest stash
git stash drop              # Delete latest stash

# Rebase
git rebase main             # Rebase current branch onto main
git rebase -i HEAD~3        # Interactive rebase last 3 commits

Git Workflow Best Practices

# Feature branch workflow
1. git checkout main
2. git pull origin main
3. git checkout -b feature/new-feature
4. # Make changes and commits
5. git push origin feature/new-feature
6. # Create pull request
7. git checkout main
8. git pull origin main
9. git branch -d feature/new-feature

# Conventional commit messages
feat: add user authentication
fix: resolve login button bug
docs: update API documentation
style: format code with prettier
refactor: simplify user validation
test: add unit tests for auth service