Skip to main content

Git Workflow & Common Operations

This guide covers common day-to-day Git operations based on our actual usage patterns.

Daily Workflow

Basic Workflow Sequence

# 1. Check current status
git status

# 2. Add changes to staging
git add .

# 3. Commit changes
git commit -m "Descriptive commit message"

# 4. Push to remote
git push

# Or push and set upstream
git push -u origin main

Check Repository State

# View status
git status

# View commit history
git log -1 # Last commit
git log -5 # Last 5 commits
git log --oneline # Condensed view

# View branches
git branch # Local branches
git branch -a # All branches (including remote)

# View remotes
git remote -v

Staging and Committing

Add Files to Staging

# Add all changes
git add .

# Add specific file
git add README.md

# Add specific directory
git add src/

# Add multiple files
git add file1.txt file2.txt file3.txt

# Add with pattern
git add *.js

Commit Changes

# Commit with message
git commit -m "Add new feature"

# Commit with detailed message
git commit -m "Add user authentication

- Implement JWT token generation
- Add login/logout endpoints
- Create middleware for auth verification"

# Amend last commit (don't change message)
git commit --amend --no-edit

# Amend last commit (change message)
git commit --amend -m "Updated commit message"

Combined Operations

# Add and commit in one step
git add . && git commit -m "Update configuration files" && git push

Pushing Changes

Basic Push

# Push to current branch
git push

# Push and set upstream
git push -u origin main

# Set upstream explicitly
git push --set-upstream origin main

Force Push (Use with Caution)

# Force push (overwrites remote)
git push -f origin main

# Safer force push (checks remote hasn't changed)
git push --force-with-lease origin main

Pulling Changes

Basic Pull

# Pull from remote
git pull

# Pull from specific branch
git pull origin main

# Pull with rebase
git pull origin main --rebase

Fetch and Merge

# Fetch without merging
git fetch origin

# View what changed
git log HEAD..origin/main

# Merge fetched changes
git merge origin/main

Handling Conflicts

Pull Conflicts

# Attempt pull
git pull origin main

# If conflicts occur:
# 1. View conflicted files
git status

# 2. Edit files to resolve conflicts
vim conflicted-file.txt

# 3. Mark as resolved
git add conflicted-file.txt

# 4. Complete merge
git commit

Abort Merge/Rebase

# Abort merge
git merge --abort

# Abort rebase
git rebase --abort

# Start over with reset
git fetch origin
git reset --hard origin/main

Real-World Examples from Our History

Example 1: Simple Update and Push

cd /home/rezriz/github/01-production/vps-management
git status
git add .
git commit -m "update config"
git push

Example 2: Add Documentation Files

cd /home/rezriz/github/03-lab/AI-Agent/transfer-to-another-vps

git add GITHUB_SETUP.md PROJECT_SUMMARY.md setup_github_repo.sh
git commit -m "Add project documentation and setup scripts"
git push origin main

Example 3: Migrate Content

cd /home/rezriz/github/01-production/vps-management

# Add migrated content
git add .
git commit -m "Migrate Docker deployment and app configurations from lab to production"
git push

Example 4: Remove Folder

cd /home/rezriz/github/01-production/vps-management

# Remove directory from Git
git rm -r Docker
git commit -m "Remove Docker folder (moved to its own repository)"
git push

Example 5: Handle Push Failure

cd /home/rezriz/github/01-production/vps-management

# Pull with rebase to integrate changes
git pull origin main --rebase

# Push after rebase
git push -u origin main

Example 6: Reset to Remote State

cd /home/rezriz/github/01-production/vps-management

# Abort any ongoing operations
git rebase --abort

# Fetch latest
git fetch origin

# Hard reset to match remote
git reset --hard origin/main

# Make new changes
echo "# VPS Management" > README.md
git add README.md

# Amend and force push
git commit --amend --no-edit
git push -f origin main

Working with Branches

Create and Switch Branches

# Create new branch
git branch feature-branch

# Switch to branch
git checkout feature-branch

# Create and switch in one command
git checkout -b feature-branch

# Modern alternative (Git 2.23+)
git switch -c feature-branch

Merge Branches

# Switch to main branch
git checkout main

# Merge feature branch
git merge feature-branch

# Delete merged branch
git branch -d feature-branch

Rename Branch

# Rename current branch
git branch -m new-name

# Rename to 'main' (modern standard)
git branch -M main

Cloning Repositories

Basic Clone

# Clone via SSH (preferred)
cd /home/rezriz/github
git clone git@github.com:donnyaw/vps-management.git

# Clone via HTTPS
git clone https://github.com/donnyaw/restic-rclone.git

Clone to Specific Directory

# Clone to custom directory name
git clone git@github.com:donnyaw/vps-management.git my-vps-tools

Post-Clone Setup

# Navigate to cloned repository
cd restic-rclone

# Configure local user
git config user.email "donnyaw@gmail.com"
git config user.name "donnyaw"

# Check remote configuration
git remote -v

# Check branches
git branch -a

Inspecting Repository

View Changes

# View unstaged changes
git diff

# View staged changes
git diff --staged

# View changes in specific file
git diff README.md

# Compare branches
git diff main..feature-branch

View History

# View commit log
git log

# Last commit details
git log -1

# Compact log
git log --oneline

# Graph view
git log --oneline --graph --decorate --all

# Show files changed in commit
git show HEAD

Undoing Changes

Unstage Files

# Unstage all files
git reset HEAD

# Unstage specific file
git reset HEAD README.md

Discard Local Changes

# Discard changes in file
git checkout -- README.md

# Discard all changes
git checkout -- .

# Modern alternative
git restore README.md

Reset Commits

# Soft reset (keep changes staged)
git reset --soft HEAD~1

# Mixed reset (keep changes unstaged)
git reset HEAD~1

# Hard reset (discard all changes)
git reset --hard HEAD~1

# Reset to specific commit
git reset --hard abc123

Cleaning Repository

Remove Untracked Files

# Preview what will be removed
git clean -n

# Remove untracked files
git clean -f

# Remove untracked files and directories
git clean -fd

# Remove ignored files too
git clean -fdx

Advanced Operations

Stash Changes

# Stash current changes
git stash

# Stash with message
git stash save "Work in progress"

# List stashes
git stash list

# Apply stash
git stash apply

# Apply and remove stash
git stash pop

# Clear all stashes
git stash clear

Cherry-Pick Commits

# Apply commit from another branch
git cherry-pick abc123

# Apply multiple commits
git cherry-pick abc123 def456

Interactive Rebase

# Rebase last 3 commits
git rebase -i HEAD~3

# Actions in interactive rebase:
# pick - use commit
# reword - change commit message
# edit - amend commit
# squash - combine with previous
# drop - remove commit

Useful Combined Commands

Quick Commit and Push

# Stage, commit, and push
git add . && \
git commit -m "Update documentation" && \
git push

Sync with Remote

# Fetch, reset, and clean
git fetch origin && \
git reset --hard origin/main && \
git clean -fd

New Branch Workflow

# Create branch, add changes, commit, push
git checkout -b feature/new-feature && \
git add . && \
git commit -m "Implement new feature" && \
git push -u origin feature/new-feature

Troubleshooting Commands

Check What's Wrong

# Comprehensive status
git status -v

# Check remote connection
git remote -v
git fetch --dry-run

# Verify SSH
ssh -T git@github.com

Fix Common Issues

# Fix detached HEAD
git checkout main

# Fix "no upstream branch"
git push --set-upstream origin main

# Fix "pushed commit was rejected"
git pull --rebase origin main
git push

Git Ignore

Create .gitignore

# Create .gitignore file
cat > .gitignore << 'EOF'
# System files
.DS_Store
Thumbs.db

# IDE
.vscode/
.idea/

# Dependencies
node_modules/
vendor/

# Environment
.env
.env.local

# Build output
dist/
build/
*.log

# Sensitive
*.key
*.pem
*token*
EOF

# Add to repository
git add .gitignore
git commit -m "Add .gitignore"

Best Practices

Commit Messages

Good commit messages:

git commit -m "Add user authentication with JWT"
git commit -m "Fix memory leak in connection pool"
git commit -m "Update Docker deployment configuration"
git commit -m "Remove deprecated API endpoints"

Poor commit messages:

git commit -m "update"          # Too vague
git commit -m "fix bug" # Which bug?
git commit -m "changes" # What changes?
git commit -m "wip" # Not descriptive

Commit Frequency

  • Commit logical units of work
  • Commit working code
  • Commit before switching tasks
  • Don't commit broken code to main
  • Don't commit sensitive data

Safe Operations

# Always check status before committing
git status

# Review changes before staging
git diff

# Use --dry-run for destructive operations
git clean -n

# Use --force-with-lease instead of -f
git push --force-with-lease

Quick Reference

Essential Commands

CommandPurpose
git statusCheck repository status
git add .Stage all changes
git commit -m "msg"Commit with message
git pushPush to remote
git pullPull from remote
git logView commit history
git diffView changes
git branchList branches
git checkoutSwitch branches
git mergeMerge branches

Emergency Commands

CommandPurpose
git stashSave changes temporarily
git reset --hard HEADDiscard all changes
git rebase --abortCancel rebase
git merge --abortCancel merge
git clean -fdRemove untracked files