wiki.nitaking.dev

Git CheatSheet

Git commands and workflows reference

GitHubView on GitHub

Configuration

Auto Squash Setup

git config rebase.autosquash true

Auto-squash設定により、git commit --fixupgit commit --squash でコミットした際に、git rebase -i で自動的に適切な位置に配置される。

Essential Global Config

git config --global user.name "Your Name"
git config --global user.email "[email protected]"
git config --global init.defaultBranch main
git config --global pull.rebase false

Daily Commands

Status & Information

git status                    # Working directory status
git log --oneline            # Compact commit history
git log --graph --oneline    # Visual commit graph
git diff                     # Working directory changes
git diff --staged           # Staged changes

Adding & Committing

git add .                    # Stage all changes
git add -p                   # Interactive staging
git commit -m "message"      # Commit with message
git commit --amend          # Modify last commit
git commit --fixup <hash>   # Create fixup for commit

Branching & Merging

Branch Operations

git branch                   # List branches
git branch <name>           # Create branch
git checkout <branch>       # Switch branch
git switch <branch>         # Modern switch branch
git checkout -b <name>      # Create and switch
git branch -d <name>        # Delete merged branch
git branch -D <name>        # Force delete branch

Merging

git merge <branch>          # Merge branch
git merge --no-ff <branch>  # Force merge commit
git merge --squash <branch> # Squash merge

Rebase & History

Interactive Rebase

git rebase -i HEAD~3        # Interactive rebase last 3
git rebase -i <hash>        # Rebase from specific commit
git rebase --autosquash -i  # Auto-arrange fixup commits

Rebase Commands (in editor)

  • pick - Use commit as-is
  • reword - Edit commit message
  • edit - Stop for amending
  • squash - Combine with previous
  • fixup - Combine, discard message
  • drop - Remove commit

Remote Operations

Remote Management

git remote -v               # List remotes
git remote add <name> <url> # Add remote
git fetch                   # Fetch updates
git pull                    # Fetch and merge
git push                    # Push changes
git push -u origin <branch> # Set upstream and push

Tracking Branches

git branch -u origin/<branch>    # Set upstream
git push --set-upstream origin <branch>  # Push and set upstream

Stashing

git stash                   # Stash changes
git stash -u               # Include untracked files
git stash list             # List stashes
git stash pop              # Apply and remove stash
git stash apply            # Apply without removing
git stash drop             # Delete stash

Undoing Changes

Working Directory

git checkout -- <file>     # Discard file changes
git restore <file>         # Modern discard changes
git clean -fd              # Remove untracked files/dirs

Commits

git reset --soft HEAD~1    # Undo commit, keep changes staged  
git reset --mixed HEAD~1   # Undo commit, unstage changes
git reset --hard HEAD~1    # Undo commit, discard changes
git revert <hash>          # Create reverting commit

Useful Aliases

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'

Workflow Patterns

Feature Branch Workflow

git checkout main
git pull
git checkout -b feature/new-feature
# Make changes and commits
git push -u origin feature/new-feature
# Create Pull Request

Hotfix Workflow

git checkout main
git pull
git checkout -b hotfix/critical-fix
# Make fix
git commit -m "fix: critical issue"
git checkout main
git merge hotfix/critical-fix
git push
git branch -d hotfix/critical-fix

Advanced Tips

Search in History

git log -S "search term"    # Search content in commits
git log --grep="pattern"    # Search commit messages
git blame <file>           # See who changed what

Cherry Pick

git cherry-pick <hash>     # Apply commit to current branch
git cherry-pick -n <hash>  # Apply without committing

Submodules

git submodule add <url>    # Add submodule
git submodule update --init # Initialize submodules
git submodule update --remote # Update to latest

🤖 Edited with assistance from Claude

Last updated on