Back to notes
#git#github#linux#devops#tutorial#cheatsheet

The Complete Git & GitHub Guide: Commands Cheatsheet and Authentication Setup

A comprehensive guide covering essential Git commands for Linux/Unix systems and step-by-step GitHub authentication setup from scratch. Perfect for beginners and as a quick reference.

16 min read3,026 words

Whether you're just starting your development journey or need a quick reference for Git commands, this guide has you covered. We'll walk through essential Git commands for Linux/Unix systems and show you how to set up GitHub authentication from scratch.

Table of Contents

Git Basics

Configuration

Set up your Git identity before making any commits:

Bash
1# Set your name and email globally 2git config --global user.name "Your Name" 3git config --global user.email "your.email@example.com" 4 5# View all configuration settings 6git config --list 7 8# Set default branch name to 'main' 9git config --global init.defaultBranch main 10 11# Set your preferred text editor 12git config --global core.editor "nano" # or vim, code, etc.

Getting Help

Bash
1# Get help for any Git command 2git help <command> 3git <command> --help 4 5# Quick reference 6git <command> -h

Repository Setup

Creating Repositories

Bash
1# Initialize a new Git repository in the current directory 2git init 3 4# Initialize with a specific branch name 5git init -b main 6 7# Clone an existing repository 8git clone <repository-url> 9 10# Clone to a specific directory 11git clone <repository-url> <directory-name> 12 13# Clone only a specific branch 14git clone -b <branch-name> <repository-url>

Making Changes

Staging and Committing

Bash
1# Check the status of your working directory 2git status 3 4# Add specific file to staging area 5git add <file-name> 6 7# Add all changes in current directory 8git add . 9 10# Add all changes in repository 11git add -A 12 13# Add all files with specific extension 14git add *.txt 15 16# Commit staged changes with a message 17git commit -m "Your commit message" 18 19# Add and commit in one step (tracked files only) 20git commit -am "Your commit message" 21 22# Amend the last commit (change message or add forgotten files) 23git commit --amend -m "New commit message" 24 25# Amend without changing the message 26git commit --amend --no-edit

Working with Files

Bash
1# Remove file from working directory and staging area 2git rm <file-name> 3 4# Remove file from staging area only (keep in working directory) 5git rm --cached <file-name> 6 7# Rename or move a file 8git mv <old-name> <new-name>

Branching & Merging

Branch Management

Bash
1# List all local branches 2git branch 3 4# List all branches (local and remote) 5git branch -a 6 7# Create a new branch 8git branch <branch-name> 9 10# Switch to a different branch 11git checkout <branch-name> 12 13# Create and switch to a new branch 14git checkout -b <branch-name> 15 16# Modern way to switch branches (Git 2.23+) 17git switch <branch-name> 18 19# Create and switch to new branch (modern) 20git switch -c <branch-name> 21 22# Delete a branch (safe - prevents deletion if unmerged) 23git branch -d <branch-name> 24 25# Force delete a branch 26git branch -D <branch-name> 27 28# Rename current branch 29git branch -m <new-branch-name> 30 31# Rename a different branch 32git branch -m <old-branch-name> <new-branch-name>

Merging

Bash
1# Merge a branch into current branch 2git merge <branch-name> 3 4# Merge with a custom commit message 5git merge <branch-name> -m "Merge message" 6 7# Abort a merge in progress 8git merge --abort 9 10# Continue merge after resolving conflicts 11git merge --continue

Rebasing

Bash
1# Rebase current branch onto another branch 2git rebase <branch-name> 3 4# Interactive rebase for last N commits 5git rebase -i HEAD~<N> 6 7# Abort a rebase in progress 8git rebase --abort 9 10# Continue rebase after resolving conflicts 11git rebase --continue 12 13# Skip current commit during rebase 14git rebase --skip

Remote Repositories

Working with Remotes

Bash
1# List all configured remotes 2git remote -v 3 4# Add a new remote repository 5git remote add <remote-name> <repository-url> 6 7# Add origin remote (most common) 8git remote add origin https://github.com/username/repo.git 9 10# Remove a remote 11git remote remove <remote-name> 12 13# Rename a remote 14git remote rename <old-name> <new-name> 15 16# Change remote URL 17git remote set-url <remote-name> <new-url> 18 19# Show information about a remote 20git remote show <remote-name>

Fetching and Pulling

Bash
1# Fetch changes from remote (doesn't merge) 2git fetch 3 4# Fetch from specific remote 5git fetch <remote-name> 6 7# Fetch from all remotes 8git fetch --all 9 10# Pull changes from remote (fetch + merge) 11git pull 12 13# Pull from specific remote and branch 14git pull <remote-name> <branch-name> 15 16# Pull with rebase instead of merge 17git pull --rebase

Pushing

Bash
1# Push commits to remote repository 2git push 3 4# Push to specific remote and branch 5git push <remote-name> <branch-name> 6 7# Push and set upstream for current branch 8git push -u origin <branch-name> 9 10# Push all branches to remote 11git push --all 12 13# Push tags to remote 14git push --tags 15 16# Force push (use with caution!) 17git push --force 18 19# Safer force push (won't overwrite others' work) 20git push --force-with-lease 21 22# Delete a remote branch 23git push <remote-name> --delete <branch-name>

Viewing History & Status

Logs and History

Bash
1# View commit history 2git log 3 4# View compact one-line log 5git log --oneline 6 7# View log with graph visualization 8git log --graph --oneline --all 9 10# View last N commits 11git log -n <number> 12 13# View commits by specific author 14git log --author="Author Name" 15 16# View commits in date range 17git log --since="2 weeks ago" --until="yesterday" 18 19# View changes introduced by each commit 20git log -p 21 22# View statistics for each commit 23git log --stat 24 25# View commits that affected a specific file 26git log <file-name> 27 28# Search commits by message 29git log --grep="search term"

Inspecting Changes

Bash
1# Show unstaged changes 2git diff 3 4# Show staged changes 5git diff --staged 6# or 7git diff --cached 8 9# Show changes between branches 10git diff <branch1> <branch2> 11 12# Show changes in a specific file 13git diff <file-name> 14 15# Show changes from a specific commit 16git show <commit-hash> 17 18# Show changes from HEAD 19git show HEAD 20 21# Show file contents from a specific commit 22git show <commit-hash>:<file-path>

Blame and Annotate

Bash
1# Show who changed each line in a file 2git blame <file-name> 3 4# Blame with line numbers 5git blame -L <start-line>,<end-line> <file-name>

Undoing Changes

Unstaging and Reverting

Bash
1# Unstage a file (keep changes in working directory) 2git restore --staged <file-name> 3# or (older method) 4git reset HEAD <file-name> 5 6# Discard changes in working directory 7git restore <file-name> 8# or (older method) 9git checkout -- <file-name> 10 11# Discard all local changes 12git restore . 13 14# Revert a commit (creates a new commit that undoes changes) 15git revert <commit-hash> 16 17# Revert without creating a commit immediately 18git revert -n <commit-hash>

Reset Commands

Bash
1# Reset to a specific commit (keep changes staged) 2git reset --soft <commit-hash> 3 4# Reset to a specific commit (keep changes unstaged) 5git reset --mixed <commit-hash> 6# or simply 7git reset <commit-hash> 8 9# Reset to a specific commit (discard all changes - DANGEROUS!) 10git reset --hard <commit-hash> 11 12# Reset to last commit 13git reset --hard HEAD 14 15# Reset to commit before last 16git reset --hard HEAD~1 17 18# Undo last N commits (keep changes) 19git reset HEAD~<N>

Cleaning

Bash
1# Show what would be removed 2git clean -n 3 4# Remove untracked files 5git clean -f 6 7# Remove untracked files and directories 8git clean -fd 9 10# Remove untracked and ignored files 11git clean -fdx

Advanced Commands

Stashing

Bash
1# Stash current changes 2git stash 3 4# Stash with a message 5git stash save "Work in progress on feature X" 6 7# List all stashes 8git stash list 9 10# Apply most recent stash (keep in stash list) 11git stash apply 12 13# Apply specific stash 14git stash apply stash@{<number>} 15 16# Apply most recent stash and remove from list 17git stash pop 18 19# Drop most recent stash 20git stash drop 21 22# Drop specific stash 23git stash drop stash@{<number>} 24 25# Clear all stashes 26git stash clear 27 28# Show stash contents 29git stash show 30git stash show -p # Show full diff

Cherry-Picking

Bash
1# Apply a commit from another branch to current branch 2git cherry-pick <commit-hash> 3 4# Cherry-pick multiple commits 5git cherry-pick <commit1> <commit2> 6 7# Cherry-pick a range of commits 8git cherry-pick <commit1>..<commit2>

Tags

Bash
1# List all tags 2git tag 3 4# Create a lightweight tag 5git tag <tag-name> 6 7# Create an annotated tag 8git tag -a <tag-name> -m "Tag message" 9 10# Tag a specific commit 11git tag <tag-name> <commit-hash> 12 13# Push tag to remote 14git push origin <tag-name> 15 16# Push all tags 17git push --tags 18 19# Delete a local tag 20git tag -d <tag-name> 21 22# Delete a remote tag 23git push origin --delete <tag-name> 24 25# Checkout a tag 26git checkout <tag-name>

Submodules

Bash
1# Add a submodule 2git submodule add <repository-url> <path> 3 4# Initialize submodules 5git submodule init 6 7# Update submodules 8git submodule update 9 10# Clone repository with submodules 11git clone --recurse-submodules <repository-url> 12 13# Update all submodules to latest 14git submodule update --remote

Searching

Bash
1# Search for text in tracked files 2git grep "search term" 3 4# Search in specific file types 5git grep "search term" -- "*.js" 6 7# Search and show line numbers 8git grep -n "search term" 9 10# Search in specific commit 11git grep "search term" <commit-hash>

GitHub Authentication Setup

Setting up authentication with GitHub is essential for pushing and pulling code. Here are the two main methods: HTTPS with Personal Access Token (PAT) and SSH.

Step 1: Generate a Personal Access Token

  1. Log in to GitHub and go to SettingsDeveloper settingsPersonal access tokensTokens (classic)
  2. Click "Generate new token""Generate new token (classic)"
  3. Give your token a descriptive name
  4. Set expiration (recommended: 90 days or custom)
  5. Select scopes (minimum required:
    CODE
    repo
    for full repository access)
  6. Click "Generate token"
  7. IMPORTANT: Copy the token immediately - you won't be able to see it again!

Step 2: Use the Token for Authentication

When Git prompts for credentials:

  • Username: Your GitHub username
  • Password: Your Personal Access Token (not your GitHub password)

To avoid entering credentials repeatedly:

Bash
1# Cache credentials for 1 hour (3600 seconds) 2git config --global credential.helper 'cache --timeout=3600' 3 4# Cache credentials for 1 day (86400 seconds) 5git config --global credential.helper 'cache --timeout=86400' 6 7# For Linux: Use the credential store (stores permanently) 8git config --global credential.helper store 9 10# For macOS: Use the keychain 11git config --global credential.helper osxkeychain

First-time Push with Token

Bash
1# Add remote repository 2git remote add origin https://github.com/username/repository.git 3 4# Push to GitHub (you'll be prompted for username and token) 5git push -u origin main

SSH is more secure and convenient for frequent GitHub users as it doesn't require entering credentials.

Step 1: Check for Existing SSH Keys

Bash
1# List existing SSH keys 2ls -al ~/.ssh 3 4# Look for files named id_rsa.pub, id_ed25519.pub, or similar

Step 2: Generate a New SSH Key

If you don't have an SSH key or want a new one:

Bash
1# Generate an ED25519 SSH key (modern and secure) 2ssh-keygen -t ed25519 -C "your.email@example.com" 3 4# If your system doesn't support ED25519, use RSA 5ssh-keygen -t rsa -b 4096 -C "your.email@example.com"

When prompted:

  • Press Enter to accept default file location (
    CODE
    ~/.ssh/id_ed25519
    )
  • Enter a secure passphrase (optional but recommended)
  • Confirm the passphrase

Step 3: Add SSH Key to the SSH Agent

Bash
1# Start the SSH agent in the background 2eval "$(ssh-agent -s)" 3 4# Add your SSH private key to the agent 5ssh-add ~/.ssh/id_ed25519 6 7# If you used RSA instead 8ssh-add ~/.ssh/id_rsa

Step 4: Add SSH Key to Your GitHub Account

Bash
1# Copy the SSH public key to clipboard 2# For Linux with xclip: 3sudo apt-get install xclip 4xclip -sel clip < ~/.ssh/id_ed25519.pub 5 6# Or display and copy manually: 7cat ~/.ssh/id_ed25519.pub

Then on GitHub:

  1. Go to SettingsSSH and GPG keys
  2. Click "New SSH key"
  3. Give it a title (e.g., "My Linux Laptop")
  4. Paste your public key
  5. Click "Add SSH key"

Step 5: Test SSH Connection

Bash
1# Test your SSH connection to GitHub 2ssh -T git@github.com 3 4# You should see a success message like: 5# Hi username! You've successfully authenticated, but GitHub does not provide shell access.

Step 6: Use SSH URLs for Git Operations

Bash
1# Clone a repository using SSH 2git clone git@github.com:username/repository.git 3 4# Change existing remote from HTTPS to SSH 5git remote set-url origin git@github.com:username/repository.git 6 7# Verify the remote URL 8git remote -v

Switching Between HTTPS and SSH

Bash
1# Check current remote URL 2git remote -v 3 4# Change from HTTPS to SSH 5git remote set-url origin git@github.com:username/repository.git 6 7# Change from SSH to HTTPS 8git remote set-url origin https://github.com/username/repository.git

Troubleshooting Authentication

HTTPS Issues

Bash
1# If credentials are cached incorrectly 2git credential-cache exit 3 4# Or remove stored credentials (Linux) 5rm ~/.git-credentials 6 7# Test connection 8git ls-remote https://github.com/username/repository.git

SSH Issues

Bash
1# Verify SSH key is added to agent 2ssh-add -l 3 4# Test SSH connection with verbose output 5ssh -vT git@github.com 6 7# Check SSH config 8cat ~/.ssh/config 9 10# Verify public key matches what's on GitHub 11cat ~/.ssh/id_ed25519.pub

Common Errors

"Permission denied (publickey)"

  • Your SSH key isn't added to GitHub or the SSH agent
  • Verify with
    CODE
    ssh-add -l
    and check GitHub SSH settings

"Support for password authentication was removed"

  • GitHub no longer accepts passwords for Git operations
  • Use a Personal Access Token or switch to SSH

"Could not resolve host"

  • Check your internet connection
  • Verify the repository URL is correct

Useful Tips & Tricks

Aliases

Create shortcuts for commonly used commands:

Bash
1# Set up common aliases 2git config --global alias.st status 3git config --global alias.co checkout 4git config --global alias.br branch 5git config --global alias.ci commit 6git config --global alias.unstage 'reset HEAD --' 7git config --global alias.last 'log -1 HEAD' 8git config --global alias.visual 'log --graph --oneline --all' 9 10# Now you can use: 11git st # instead of git status 12git co main # instead of git checkout main 13git br # instead of git branch 14git visual # for a nice graph visualization

Ignoring Files

Create a

CODE
.gitignore
file to exclude files from version control:

Bash
1# Create .gitignore 2touch .gitignore 3 4# Add patterns (one per line) 5*.log 6*.tmp 7node_modules/ 8.env 9.DS_Store 10__pycache__/ 11*.pyc

Global gitignore:

Bash
1# Create global gitignore 2git config --global core.excludesfile ~/.gitignore_global 3 4# Add common patterns 5echo ".DS_Store" >> ~/.gitignore_global 6echo "*.swp" >> ~/.gitignore_global

Viewing File from Another Branch

Bash
1# View file content from another branch without switching 2git show <branch-name>:<file-path> 3 4# Save it to a file 5git show <branch-name>:<file-path> > temp_file.txt

Finding Bugs with Bisect

Bash
1# Start bisect session 2git bisect start 3 4# Mark current commit as bad 5git bisect bad 6 7# Mark a known good commit 8git bisect good <commit-hash> 9 10# Git will checkout a commit in between - test it 11# Then mark it as good or bad 12git bisect good # or git bisect bad 13 14# Repeat until Git finds the problematic commit 15 16# End bisect session 17git bisect reset

Useful Git Commands Combinations

Bash
1# Show all commits in current branch not in main 2git log main..HEAD 3 4# Show files changed in last commit 5git diff-tree --no-commit-id --name-only -r HEAD 6 7# List all files ever committed 8git log --pretty=format: --name-only --diff-filter=A | sort -u 9 10# Count commits by author 11git shortlog -sn 12 13# Prune deleted remote branches 14git remote prune origin 15 16# Show commit count for each contributor 17git shortlog -sn --all

Best Practices

Commit Messages

  • Use present tense ("Add feature" not "Added feature")
  • Keep first line under 50 characters
  • Add detailed description after a blank line if needed
  • Reference issue numbers when applicable

Good commit message example:

CODE
1Add user authentication feature 2 3- Implement JWT token generation 4- Add login and registration endpoints 5- Create middleware for protected routes 6- Update documentation 7 8Fixes #123

Branching Strategy

  • Keep
    CODE
    main
    or
    CODE
    master
    branch stable and deployable
  • Create feature branches for new work:
    CODE
    feature/user-auth
  • Create hotfix branches for urgent fixes:
    CODE
    hotfix/login-bug
  • Delete branches after merging
  • Keep branches short-lived

General Tips

  • Commit often, push regularly
  • Pull before you push
  • Write descriptive commit messages
  • Review changes before committing (
    CODE
    git diff
    )
  • Use
    CODE
    .gitignore
    for files that shouldn't be tracked
  • Don't commit sensitive information (passwords, API keys)
  • Keep commits focused on a single change
  • Use branches for experimentation

Quick Reference Card

Essential Commands

Bash
1# Setup 2git init # Initialize repository 3git clone <url> # Clone repository 4 5# Daily workflow 6git status # Check status 7git add <file> # Stage file 8git commit -m "message" # Commit changes 9git push # Push to remote 10git pull # Pull from remote 11 12# Branching 13git branch <name> # Create branch 14git checkout <name> # Switch branch 15git merge <name> # Merge branch 16 17# History 18git log # View history 19git diff # View changes 20 21# Undo 22git restore <file> # Discard changes 23git reset HEAD~1 # Undo last commit 24git revert <commit> # Revert commit

Conclusion

This cheatsheet covers the most essential Git commands for daily development work and complete GitHub authentication setup. Bookmark this page for quick reference, and don't hesitate to explore the official Git documentation for more advanced features.

Remember: Git is a powerful tool, and the best way to learn is by using it regularly. Start with the basics, and gradually incorporate more advanced commands as you become comfortable.

Happy coding! 🚀

Last updated: January 2026

Additional Resources