DEV Community

Cover image for The Complete Git Commands Cheat Sheet - Everything You Need to Know
zainaboyedeji
zainaboyedeji

Posted on

The Complete Git Commands Cheat Sheet - Everything You Need to Know

Git is the free and open source distributed version control system that's responsible for everything GitHub-related that happens locally on your computer. This comprehensive cheat sheet features the most important and commonly used Git commands for easy reference.

1. Getting Started & Configuration 🏁

Installation & GUIs

With platform specific installers for Git, GitHub also provides the ease of staying up-to-date with the latest releases of the command line tool while providing a graphical user interface for day-to-day interaction, review, and repository synchronization.

Installation Setup

Configuring user information used across all local repositories:

# Check Git version
git --version

# Set username
git config --global user.name "[name]"

# Set email
git config --global user.email "[email]"

# List all configurations
git config --list

# Enable helpful colorization
git config --global color.ui auto

# Display help documentation
git help
Enter fullscreen mode Exit fullscreen mode

Repository Initialization

# Initialize a new Git repository
git init

# Create repository in specific directory
git init [directory]

# Clone a repository
git clone [url]

# Clone into specific directory
git clone [url] [directory]

# Clone specific branch
git clone --branch [branch_name] [repository_url]
Enter fullscreen mode Exit fullscreen mode

2. Basic Snapshotting πŸ“Έ

Adding Files

# Add file to staging
git add [file]

# Add all new and changed files
git add .

# Add all files (including deleted)
git add -A

# Interactive staging
git add -p
Enter fullscreen mode Exit fullscreen mode

Committing Changes

# Commit with message
git commit -m "[descriptive message]"

# Add and commit tracked files
git commit -am "[message]"

# Modify most recent commit
git commit --amend

# Amend without changing message
git commit --amend --no-edit

# Create new note
git notes add

# Restore file to last commit
git restore <file>
Enter fullscreen mode Exit fullscreen mode

Status & Differences

# Show modified files
git status

# Show ignored files
git status --ignored

# Show working directory changes
git diff

# Show staged changes
git diff --staged
git diff --cached

# Compare branches
git diff [branch1] [branch2]

# Unstage file
git reset [file]

# Compare with last commit
git diff HEAD
Enter fullscreen mode Exit fullscreen mode

3. Branching and Merging 🌲

Branch Management

# List local branches
git branch

# List all branches
git branch -a

# Create new branch
git branch [branch-name]

# Delete branch locally
git branch -d [branch-name]

# Force delete branch
git branch -D [branch-name]

# Rename branch
git branch -m [old-name] [new-name]

# Switch to branch
git switch -c [branch-name]
Enter fullscreen mode Exit fullscreen mode

Navigation

# Switch branches
git checkout [branch-name]

# Create and switch
git checkout -b [branch-name]

# Switch to last branch
git checkout -

# Check out commit
git checkout [commit-hash]
Enter fullscreen mode Exit fullscreen mode

Merging

# Merge branch
git merge [branch]

# Abort merge
git merge --abort

# Create merge commit
git merge --no-ff [branch]
Enter fullscreen mode Exit fullscreen mode

4. Remote Operations 🌐

Remote Management

# List remote repositories
git remote -v

# Add remote
git remote add [name] [url]

# Remove remote
git remote remove [name]

# Delete file
git rm [file]

# Rename remote
git remote rename [old-name] [new-name]
Enter fullscreen mode Exit fullscreen mode

Syncing

# Fetch branches
git fetch [remote]

# Remove obsolete branches
git fetch --prune

# Fetch and merge
git pull

# Fetch and rebase
git pull --rebase

# Push changes
git push [branch]

# Push and set upstream
git push -u origin [branch]

# Force push
git push --force
Enter fullscreen mode Exit fullscreen mode

5. History and Comparison πŸ“š

Logging

# Show commit history
git log

# One line format
git log --oneline

# Show as graph
git log --graph

# Show with stats
git log --stat -M

# Show file changes by author
git blame [file]

# Compare branch commits
git log branchB..branchA

# Show file history
git log --follow [file]
Enter fullscreen mode Exit fullscreen mode

Inspection

# Show commit details
git show [commit]

# Show current commit hash
git rev-parse HEAD

# Show reference logs
git reflog

# Show object details
git show [SHA]
Enter fullscreen mode Exit fullscreen mode

6. Undoing Changes ↩️

Working Directory

# Discard changes
git checkout -- [file]

# Preview cleanup
git clean -n

# Remove untracked files
git clean -f

# Remove untracked directories
git clean -fd
Enter fullscreen mode Exit fullscreen mode

Staged Changes

# Unstage file
git reset [file]

# Unstage all
git reset

# Reset to commit
git reset --hard [commit]
Enter fullscreen mode Exit fullscreen mode

Commits

# Undo commits
git reset [commit]

# Create undo commit
git revert [commit]

# Revert without commit
git revert --no-commit <commit>

# Soft reset
git reset --soft [commit]

# Hard reset
git reset --hard [commit]
Enter fullscreen mode Exit fullscreen mode

7. Ignoring Patterns 🚫

# Common .gitignore patterns
logs/
*.notes
pattern*/

# Set global ignore file
git config --global core.excludesfile [file]
Enter fullscreen mode Exit fullscreen mode

8. Advanced Operations πŸ”§

Stashing

# Save changes
git stash

# List stashes
git stash list

# Apply top stash
git stash pop

# Apply without removing
git stash apply

# Remove top stash
git stash drop

# Clear all stashes
git stash clear
Enter fullscreen mode Exit fullscreen mode

Rebasing

# Rebase onto branch
git rebase [branch]

# Interactive rebase
git rebase -i HEAD~[n]

# Abort rebase
git rebase --abort

# Continue rebase
git rebase --continue

# Move file
git mv [existing-path] [new-path]
Enter fullscreen mode Exit fullscreen mode

Tags

# List tags
git tag

# Create lightweight tag
git tag [tag-name]

# Create annotated tag
git tag -a [tag-name] -m "[message]"

# Push tag
git push origin [tag-name]
Enter fullscreen mode Exit fullscreen mode

9. Maintenance and Data Recovery πŸ› οΈ

Maintenance

# Check integrity
git fsck

# Clean up repository
git gc

# Remove unreachable objects
git prune

# Verify packed objects
git verify-pack -v .git/objects/pack/pack-*.idx
Enter fullscreen mode Exit fullscreen mode

Recovery

# Expire reflog
git reflog expire --expire=now --all

# Recover commit
git checkout [lost-commit-hash]

# Cherry-pick commit
git cherry-pick [commit]
Enter fullscreen mode Exit fullscreen mode

10. GitHub Specific Commands πŸ™

Pull Requests

# Fetch PR
git fetch origin pull/[PR-number]/head:[branch-name]

# Check out PR
git checkout pr/[PR-number]

# Push to PR
git push origin [branch]:[PR-branch]
Enter fullscreen mode Exit fullscreen mode

GitHub CLI

# Create repository
gh repo create

# Create pull request
gh pr create

# Create issue
gh issue create

# Fork repository
gh repo fork
Enter fullscreen mode Exit fullscreen mode

11. Configuration and Info βš™οΈ

Help and Information

# Get command help
git help [command]

# Alternative help
git [command] --help

# Quick help
git -h
Enter fullscreen mode Exit fullscreen mode

Advanced Configuration

# Set default editor
git config --global core.editor "[editor]"

# Create alias
git config --global alias.[shortcut] "[command]"

# Configure line endings
git config --global core.autocrlf true

# Cache credentials
git config --global credential.helper cache

# Set default branch
git config --global init.defaultBranch main
Enter fullscreen mode Exit fullscreen mode

πŸ“– Glossary

  • Git: An open-source, distributed version control system that tracks changes in code.
  • GitHub: A web-based platform for hosting, sharing, and collaborating on Git repositories.
  • Commit: A snapshot of the entire repository at a given point in time, identified by a unique SHA (hash).
  • Branch: A movable pointer to a commit, allowing parallel development without affecting the main codebase.
  • Clone: A local copy of a Git repository, including all commits, branches, and history.
  • Remote: A shared repository (typically on GitHub) that team members use to push and pull changes.
  • Fork: A personal copy of someone else's repository on GitHub, allowing independent modifications.
  • Pull Request (PR): A request to merge changes from one branch into another, often reviewed with comments, tests, and approvals.
  • HEAD: The current reference point in a Git repository, typically pointing to the latest commit on the active branch.

πŸ“š Resources for Further Learning

Official Resources

Educational Platforms

Additional References

🀝 Contributing

Found this helpful?
Consider sharing it with your network!
If you spot any errors or have suggestions for improvement, feel free to leave a comment below.

Happy coding! πŸš€

Hostinger image

Get n8n VPS hosting 3x cheaper than a cloud solution

Get fast, easy, secure n8n VPS hosting from $4.99/mo at Hostinger. Automate any workflow using a pre-installed n8n application and no-code customization.

Start now

Top comments (0)

Billboard image

The Next Generation Developer Platform

Coherence is the first Platform-as-a-Service you can control. Unlike "black-box" platforms that are opinionated about the infra you can deploy, Coherence is powered by CNC, the open-source IaC framework, which offers limitless customization.

Learn more