DEV Community

Cover image for Git Command Cheetsheet Sheet
Devashish Roy
Devashish Roy

Posted on

Git Command Cheetsheet Sheet

A concise reference for Git commands.


⚙️ Configure Git

🌍 Global (applies to all repositories)

# Set global name
git config --global user.name "Your Name"

# Set global email
git config --global user.email "your@email.com"
Enter fullscreen mode Exit fullscreen mode

📂 Local (specific to a repository)

# Set local name (for this repo only)
git config --local user.name "Your Name"

# Set local email (for this repo only)
git config --local user.email "your@email.com"
Enter fullscreen mode Exit fullscreen mode

🏗️ Initialize & Clone

🆕 Initialize a Git repository

git init
Enter fullscreen mode Exit fullscreen mode

📥 Clone a repository

# Clone into a new directory
git clone [url]

# Clone into the current directory
git clone [url] .

# Clone into a specific directory
git clone [url] [path-to-clone]
Enter fullscreen mode Exit fullscreen mode

📌 Stage & Snapshot

🔎 Check file status

git status
Enter fullscreen mode Exit fullscreen mode

➕ Add files to the staging area

# Stage a specific file
git add [file]

# Stage all files
git add .
Enter fullscreen mode Exit fullscreen mode

➖ Remove files from the staging area

# Unstage a specific file
git reset [file]

# Unstage all files
git reset .
Enter fullscreen mode Exit fullscreen mode

📝 Check file changes

# Show unstaged changes
git diff

# Show staged changes
git diff --staged
Enter fullscreen mode Exit fullscreen mode

💾 Commit changes

# Commit with a message
git commit -m "Commit message"

# Commit with title and description
git commit -m "Commit title" -m "Commit description"
Enter fullscreen mode Exit fullscreen mode

🌿 Branching

📋 List branches

git branch
Enter fullscreen mode Exit fullscreen mode

🌱 Create a new branch

git branch [branch-name]
Enter fullscreen mode Exit fullscreen mode

🔀 Switch to another branch

git checkout [branch-name]
Enter fullscreen mode Exit fullscreen mode

✏️ Rename the current branch

git branch -M [new-branch-name]
Enter fullscreen mode Exit fullscreen mode

🕒 View commit history

git log
Enter fullscreen mode Exit fullscreen mode

🔄 Share & Update

🌐 Remote repositories

# Add a remote
git remote add [alias] [url]

# List remotes
git remote -v

# Show details of a remote
git remote show [alias]

# Get the URL of a remote
git remote get-url [alias]

# Remove a remote
git remote remove [alias]
Enter fullscreen mode Exit fullscreen mode

📤 Fetching & pushing

# Fetch and merge changes from remote
git pull

# Push and set upstream for the first time
git push -u [alias] [branch]

# Push to a specific remote and branch
git push [alias] [branch]

# Push to the default remote/branch
git push
Enter fullscreen mode Exit fullscreen mode

🚫 Ignoring Files

Create a .gitignore file to exclude files/folders from being tracked:

logs/
*.notes
pattern*/
Enter fullscreen mode Exit fullscreen mode

📖 References

Top comments (0)