Skip to content

Git Commands Reference

Overview

Essential Git commands for AzmX development workflows, covering GitHub Flow, branch management, collaboration patterns, and troubleshooting. This reference focuses on practical commands used in daily development.

Git Workflow Commands

Repository Setup

# Clone repository
git clone https://github.com/azmx-org/project.git
cd project

# Set up remote tracking
git remote -v
git remote add upstream https://github.com/azmx-org/project.git

# Configure user information
git config user.name "Your Name"
git config user.email "[email protected]"

# Configure Git for the project
git config core.autocrlf input        # Normalize line endings
git config pull.rebase false          # Use merge for pulls
git config init.defaultBranch main    # Use 'main' as default branch

Branch Management

Branch Operations Flow

graph LR
    A[main branch] --> B[git checkout -b feature/new-feature]
    B --> C[Development Work]
    C --> D[git add & commit]
    D --> E[git push -u origin feature/new-feature]
    E --> F[Create Pull Request]
    F --> G[Code Review]
    G --> H[Merge to main]
    H --> I[git checkout main]
    I --> J[git pull origin main]
    J --> K[git branch -d feature/new-feature]

    style B fill:#4ecdc4
    style E fill:#74b9ff
    style H fill:#00b894
    style K fill:#fdcb6e

Creating and Switching Branches

# Create and switch to new branch
git checkout -b feature/user-authentication
git switch -c feature/user-authentication  # Git 2.23+

# Switch to existing branch
git checkout main
git switch main  # Git 2.23+

# Create branch from specific commit
git checkout -b hotfix/security-fix abc1234

# Create branch from remote branch
git checkout -b local-branch origin/remote-branch

Branch Information

# List local branches
git branch

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

# List remote branches only
git branch -r

# Show current branch
git branch --show-current

# Show branch with last commit
git branch -v

# Show merged branches
git branch --merged

# Show unmerged branches
git branch --no-merged

Branch Cleanup

# Delete local branch
git branch -d feature/completed-feature

# Force delete local branch
git branch -D feature/abandoned-feature

# Delete remote branch
git push origin --delete feature/completed-feature

# Prune remote tracking branches
git remote prune origin

# Clean up merged branches
git branch --merged main | grep -v "main\|develop" | xargs -n 1 git branch -d

Commit Operations

Basic Commits

# Stage files for commit
git add .                    # Stage all changes
git add file1.js file2.py   # Stage specific files
git add src/                 # Stage directory
git add *.js                 # Stage by pattern

# Commit changes
git commit -m "feat(auth): add JWT token authentication"

# Stage and commit in one step
git commit -am "fix(ui): resolve mobile responsive issue"

# Amend last commit (add changes to previous commit)
git add forgotten-file.js
git commit --amend --no-edit

# Amend commit message
git commit --amend -m "feat(auth): add JWT token authentication with refresh"

Advanced Commit Operations

# Interactive staging
git add -i

# Patch staging (stage parts of files)
git add -p

# Commit with detailed message
git commit -m "feat(payment): implement Stripe integration

- Add Stripe SDK configuration
- Create payment processing service
- Implement webhook handling
- Add comprehensive error handling

Closes #123"

# Empty commit (useful for triggering CI)
git commit --allow-empty -m "ci: trigger deployment"

# Sign commits with GPG
git commit -S -m "feat(auth): add secure authentication"

Commit History

# View commit history
git log

# Compact log view
git log --oneline

# Graph view of branches
git log --graph --oneline --all

# Log with file changes
git log --stat

# Log for specific file
git log --follow filename.js

# Log between dates
git log --since="2025-01-01" --until="2025-01-31"

# Log by author
git log --author="Developer Name"

# Search commit messages
git log --grep="authentication"

Remote Operations

Fetching and Pulling

# Fetch latest changes from remote
git fetch origin

# Fetch all remotes
git fetch --all

# Pull changes (fetch + merge)
git pull origin main

# Pull with rebase
git pull --rebase origin main

# Pull specific branch
git pull origin feature/backend-api

Pushing Changes

# Push to remote branch
git push origin feature/user-authentication

# Push and set upstream tracking
git push -u origin feature/user-authentication

# Push all branches
git push --all origin

# Push tags
git push --tags origin

# Force push (use with caution)
git push --force-with-lease origin feature/user-authentication

# Push specific tag
git push origin v1.2.3

Remote Management

# List remotes
git remote -v

# Add remote
git remote add upstream https://github.com/original/repo.git

# Remove remote
git remote remove old-remote

# Rename remote
git remote rename origin new-origin

# Change remote URL
git remote set-url origin https://github.com/new/repo.git

Stashing Operations

Stash Workflow

graph LR
    A[Working on Feature] --> B[Urgent Bug Report]
    B --> C[git stash push -m "WIP: feature work"]
    C --> D[git checkout main]
    D --> E[git checkout -b hotfix/urgent-fix]
    E --> F[Fix Bug & Commit]
    F --> G[git checkout feature/original]
    G --> H[git stash pop]
    H --> I[Continue Feature Work]

    style C fill:#fdcb6e
    style F fill:#ff6b6b
    style H fill:#4ecdc4

Stash Commands

# Stash current changes
git stash

# Stash with message
git stash push -m "WIP: working on authentication feature"

# Stash including untracked files
git stash -u

# Stash specific files
git stash push -m "WIP: partial changes" file1.js file2.py

# List stashes
git stash list

# Apply stash (keep in stash list)
git stash apply
git stash apply stash@{1}

# Pop stash (apply and remove from stash list)
git stash pop
git stash pop stash@{1}

# Show stash contents
git stash show stash@{0}
git stash show -p stash@{0}  # Show diff

# Drop stash
git stash drop stash@{1}

# Clear all stashes
git stash clear

Merge and Rebase Operations

Merging

# Merge branch into current branch
git merge feature/user-authentication

# Merge without fast-forward (create merge commit)
git merge --no-ff feature/user-authentication

# Merge with custom commit message
git merge -m "Merge user authentication feature" feature/user-authentication

# Abort merge in case of conflicts
git merge --abort

Rebasing

# Rebase current branch onto main
git rebase main

# Interactive rebase (last 3 commits)
git rebase -i HEAD~3

# Continue rebase after resolving conflicts
git rebase --continue

# Abort rebase
git rebase --abort

# Skip current commit during rebase
git rebase --skip

# Rebase onto specific commit
git rebase abc1234

Conflict Resolution

# Show files with conflicts
git status

# Show conflict diff
git diff

# Mark conflicts as resolved
git add resolved-file.js

# Use merge tool
git mergetool

# Accept all changes from current branch
git checkout --ours conflicted-file.js

# Accept all changes from incoming branch
git checkout --theirs conflicted-file.js

Undoing Changes

Unstaging and Reverting

# Unstage file
git reset HEAD file.js
git restore --staged file.js  # Git 2.23+

# Discard working directory changes
git checkout -- file.js
git restore file.js  # Git 2.23+

# Discard all working directory changes
git checkout .
git restore .  # Git 2.23+

# Reset to specific commit (keep changes in working directory)
git reset abc1234

# Reset to specific commit (discard all changes)
git reset --hard abc1234

# Reset to previous commit
git reset --hard HEAD~1

Reverting Commits

# Revert specific commit (create new commit that undoes changes)
git revert abc1234

# Revert multiple commits
git revert abc1234..def5678

# Revert merge commit
git revert -m 1 merge-commit-hash

# Revert without creating commit immediately
git revert --no-commit abc1234

Tagging

Creating and Managing Tags

# Create lightweight tag
git tag v1.2.3

# Create annotated tag
git tag -a v1.2.3 -m "Release version 1.2.3"

# Create tag for specific commit
git tag -a v1.2.2 abc1234 -m "Hotfix release"

# List tags
git tag
git tag -l "v1.*"

# Show tag information
git show v1.2.3

# Delete local tag
git tag -d v1.2.3

# Delete remote tag
git push origin --delete v1.2.3

# Push specific tag
git push origin v1.2.3

# Push all tags
git push --tags

Advanced Git Operations

Cherry-picking

# Cherry-pick specific commit
git cherry-pick abc1234

# Cherry-pick multiple commits
git cherry-pick abc1234 def5678

# Cherry-pick without committing
git cherry-pick --no-commit abc1234

# Continue cherry-pick after resolving conflicts
git cherry-pick --continue

# Abort cherry-pick
git cherry-pick --abort

Searching and Blaming

# Search for text in files
git grep "authentication"
git grep -n "function.*auth"  # With line numbers

# Find when a line was changed
git blame file.js
git blame -L 10,20 file.js    # Specific lines

# Find when a file was deleted
git log --diff-filter=D --summary

# Find commits that changed a specific line
git log -p -S "specific code"

# Binary search for bug introduction
git bisect start
git bisect bad HEAD
git bisect good v1.2.0
# Follow prompts to find the problematic commit

Worktrees (Multiple Working Directories)

# Create new worktree
git worktree add ../project-hotfix hotfix/critical-fix

# List worktrees
git worktree list

# Remove worktree
git worktree remove ../project-hotfix

# Prune worktree references
git worktree prune

Git Configuration

Global Configuration

# User information
git config --global user.name "Your Name"
git config --global user.email "[email protected]"

# Editor preference
git config --global core.editor "code --wait"  # VS Code
git config --global core.editor "vim"          # Vim

# Merge tool
git config --global merge.tool vimdiff

# Line ending handling
git config --global core.autocrlf input       # Unix/Mac
git config --global core.autocrlf true        # Windows

# Default branch name
git config --global init.defaultBranch main

# Push behavior
git config --global push.default simple

# Credential helper
git config --global credential.helper cache

Useful Aliases

# Add to ~/.gitconfig or use git config --global alias.name "command"
git config --global alias.co checkout
git config --global alias.br branch
git config --global alias.ci commit
git config --global alias.st status
git config --global alias.unstage 'reset HEAD --'
git config --global alias.last 'log -1 HEAD'
git config --global alias.visual '!gitk'
git config --global alias.lg "log --color --graph --pretty=format:'%Cred%h%Creset -%C(yellow)%d%Creset %s %Cgreen(%cr) %C(bold blue)<%an>%Creset' --abbrev-commit"

Troubleshooting

Common Issues and Solutions

Detached HEAD State

# If you're in detached HEAD state
git checkout main                    # Go back to main branch
git checkout -b new-branch          # Create branch from current state

Large File Issues

# Remove large file from history
git filter-branch --force --index-filter \
  'git rm --cached --ignore-unmatch path/to/large/file' \
  --prune-empty --tag-name-filter cat -- --all

# Use BFG for better performance
bfg --delete-files large-file.zip
git reflog expire --expire=now --all && git gc --prune=now --aggressive

Recover Lost Commits

# Find lost commits
git reflog

# Recover commit
git checkout -b recovery-branch abc1234

# Recover deleted branch
git reflog | grep branch-name
git checkout -b recovered-branch commit-hash

Clean Repository

# Remove untracked files (dry run first)
git clean -n

# Remove untracked files
git clean -f

# Remove untracked files and directories
git clean -fd

# Remove ignored files too
git clean -fxd

Performance and Optimization

Repository Maintenance

# Garbage collection
git gc

# Aggressive garbage collection
git gc --aggressive

# Prune unreferenced objects
git prune

# Check repository integrity
git fsck

# Optimize repository
git repack -ad

Large Repository Handling

# Shallow clone (limited history)
git clone --depth 1 https://github.com/azmx-org/project.git

# Partial clone (Git 2.19+)
git clone --filter=blob:none https://github.com/azmx-org/project.git

# Fetch only specific branch
git clone -b main --single-branch https://github.com/azmx-org/project.git

Last Updated: 2025-01-20 Tools Required: Git, GitHub CLI (optional) Team Contacts: Tech Leads for workflow questions, DevOps for repository administration