Guides/Fundamentals
fundamentalstechnical

Git Command Guide

Comprehensive version control commands for developers

What is Git?

Git is a version control system that tracks changes to your code. Think of it as an infinite undo button that also lets you work with others without overwriting each other's work.

Basic Repository Setup

Creating or cloning a repository
# Initialize a new Git repository
git init

# Clone an existing repository
git clone <repository-url>

Daily Workflow Commands

Checking status and staging changes
# Show which files have changed
git status

# Add specific file to staging
git add filename.txt

# Add all changes in current directory
git add .

# Add all changes in entire repository
git add -A
Committing changes
# Commit with inline message
git commit -m "Add user authentication feature"

# Commit and add all modified files (skips staging)
git commit -am "Fix login bug"

# Open editor for longer commit message
git commit

Branch Management

Working with branches
# List local branches
git branch

# List all branches (local and remote)
git branch -a

# Create new branch
git branch feature-login

# Create and switch to new branch
git checkout -b feature-login

# Modern alternative to create and switch
git switch -c feature-login

# Switch to existing branch
git switch main

# Merge branch into current branch
git merge feature-login

# Delete local branch (safe)
git branch -d feature-login

# Force delete local branch
git branch -D feature-login

Working with Remotes

Remote repository commands
# Add a remote repository
git remote add origin https://github.com/username/repository.git

# View remote repositories
git remote -v

# Push current branch to remote
git push origin main

# Push and set upstream tracking
git push -u origin feature-login

# Pull changes from remote
git pull origin main

# Fetch changes without merging
git fetch origin

Viewing History

Viewing commits and changes
# Basic log
git log

# Compact one-line format
git log --oneline

# Show last 5 commits
git log -5

# Show unstaged changes
git diff

# Show staged changes
git diff --cached

# Show changes in specific commit
git show <commit-hash>

Undoing Changes

Reverting and resetting
# Unstage files (keep changes)
git restore --staged filename.txt

# Discard changes in specific file
git restore filename.txt

# Soft reset - keeps changes staged
git reset --soft HEAD~1

# Mixed reset - keeps changes but unstages
git reset HEAD~1

# Hard reset - permanently deletes changes
git reset --hard HEAD~1

# Create a new commit that undoes previous commit
git revert <commit-hash>

Stashing Changes

Temporarily save work
# Stash current changes
git stash

# Stash with custom message
git stash push -m "Work in progress on login"

# Apply most recent stash
git stash pop

# List all stashes
git stash list

# Apply specific stash
git stash apply stash@{1}

When in doubt, use 'git status' to see where you are. It's the most useful command for understanding your current state.