Reimagining Git fundamentals through the lens of the iconic periodic table, transforming complex commands into easy chemical elements.
Welcome to the companion repository for the "Periodic Table of Git" talk at Git Merge 2025! This repository contains practical examples and explanations of Git commands organized like elements in a periodic table.
Just like the periodic table organizes chemical elements by their properties, we'll organize Git commands into logical families that share similar behaviors and use cases.
The fundamental building blocks of version control
Initializes a new Git repository, creating the foundation for version control.
git init # Initialize a new repository
git init --bare # Create a bare repository (for servers)
git init --template=<path> # Use a custom templateWhen to use: Starting any new project or adding version control to existing code.
Stages changes for the next commit, moving files from working directory to staging area.
git add file.txt # Stage a specific file
git add . # Stage all changes in current directory
git add -A # Stage all changes in entire repository
git add -p # Interactively stage chunks of changesWhen to use: Preparing changes before committing, controlling exactly what gets included in each commit.
Creates a snapshot of staged changes with a descriptive message.
git commit -m "Add user authentication feature" # Commit with message
git commit --amend -m "Fix typo in commit message" # Amend last commit
git commit -S # Sign commit with GPG
git commit --allow-empty -m "Trigger CI build" # Empty commitWhen to use: Saving your work at logical points, creating checkpoints in development.
Shows the state of the working directory and staging area.
git status # Full status
git status -s # Short status
git status --porcelain # Machine-readable formatWhen to use: Before commits to see what will be included, checking current state of repository.
Get and set repository or global options.
git config --global user.name "Your Name" # Set global username
git config --global user.email "[email protected]" # Set global email
git config --list # List all config values
git config user.name # Get specific config valueWhen to use: Setting up Git for first time, configuring user information, customizing Git behavior.
Temporarily save uncommitted changes.
git stash # Stash uncommitted changes
git stash pop # Apply most recent stash and remove it
git stash list # List all stashes
git stash drop # Delete most recent stashWhen to use: Switching branches with uncommitted changes, temporarily saving work in progress.
Remove files from working tree and index.
git rm file.txt # Remove file from working tree and index
git rm --cached file.txt # Remove from index only, keep in working tree
git rm -r directory/ # Remove directory recursively
git rm "*.log" # Remove all .log filesWhen to use: Deleting files from version control, cleaning up project structure.
Move or rename files, directories, or symlinks.
git mv oldname.txt newname.txt # Rename file
git mv file.txt folder/ # Move file to folder
git mv folder1/ folder2/ # Rename directory
git mv src/*.js lib/ # Move multiple filesWhen to use: Renaming files, moving files to different directories, reorganizing project structure.
Moving through branches, commits, and repository states
Switches between branches, commits, or restores files from different states.
git checkout main # Switch to main branch
git checkout -b feature/new-ui # Create and switch to new branch
git checkout HEAD~2 # Move to 2 commits back
git checkout -- file.txt # Restore file from last commit
git checkout v1.0.0 -- config.json # Get file from specific tagWhen to use: Switching contexts, creating branches, restoring files, exploring history.
Lists, creates, or deletes branches in your repository.
git branch # List local branches
git branch -a # List all branches (local + remote)
git branch feature/api-update # Create new branch
git branch -d feature/completed # Delete merged branch
git branch -D feature/experimental # Force delete unmerged branch
git branch -m old-name new-name # Rename branchWhen to use: Managing parallel development streams, organizing features and fixes.
Modern alternative to checkout for switching branches (Git 2.23+).
git switch main # Switch to existing branch
git switch -c feature/dashboard # Create and switch to new branch
git switch - # Switch to previous branch
git switch --detach HEAD~5 # Detach HEAD at specific commitWhen to use: Cleaner alternative to git checkout for branch operations.
Creates, lists, or deletes tags to mark specific points in history.
git tag v1.0.0 # Create lightweight tag
git tag -a v1.0.0 -m "First release" # Create annotated tag
git tag -l "v1.*" # List tags matching pattern
git tag -d v0.9.0 # Delete local tag
git push origin v1.0.0 # Push tag to remote
git push origin :refs/tags/v0.9.0 # Delete remote tagWhen to use: Marking releases, important milestones, or stable points in development.
Show reference log - a history of where HEAD and branches have been.
git reflog # Show HEAD reflog
git reflog --all # Show all references reflog
git reflog branch-name # Show specific branch reflog
git reset --hard HEAD@{2} # Reset to previous HEAD positionWhen to use: Recovering lost commits, understanding recent branch/HEAD movements, debugging repository state.
Connecting and synchronizing with other repositories
Manages connections to remote repositories.
git remote # List remotes
git remote -v # List remotes with URLs
git remote add origin https://github.com/user/repo.git # Add remote
git remote set-url origin [email protected]:user/repo.git # Change URL
git remote rename origin upstream # Rename remote
git remote remove old-remote # Remove remoteWhen to use: Setting up repository connections, managing multiple remotes, changing repository URLs.
Uploads local commits to a remote repository.
git push origin main # Push main branch
git push -u origin feature/new-ui # Push and set upstream
git push origin :old-branch-name # Delete remote branch
git push origin main --force-with-lease # Safer force push
git push origin --tags # Push all tags
git push origin :refs/tags/v1.0.0 # Delete remote tagWhen to use: Sharing your work, updating remote repositories, publishing releases.
Fetches and merges changes from a remote repository.
git pull # Pull from tracked remote branch
git pull origin main # Pull from specific remote/branch
git pull --rebase # Pull with rebase instead of merge
git pull --ff-only # Only fast-forward, fail if merge neededWhen to use: Getting latest changes from team members, staying synchronized with remote.
Downloads objects and refs from remote repository without merging.
git fetch # Fetch from default remote
git fetch origin # Fetch from specific remote
git fetch --all # Fetch from all remotes
git fetch --prune # Remove remote-tracking branches that no longer existWhen to use: Getting remote changes without affecting working directory, preparing for manual merge.
Clone a repository into a new directory.
git clone https://github.com/user/repo.git # Clone via HTTPS
git clone [email protected]:user/repo.git # Clone via SSH
git clone --depth 1 <url> # Shallow clone
git clone -b feature <url> # Clone specific branchWhen to use: Starting work on an existing project, creating local copies of remote repositories.
Exploring and understanding repository history
Shows commit history with various formatting options.
git log # Standard commit log
git log --oneline # Compact one-line format
git log --graph --decorate --all # Visual branch representation
git log origin/main..HEAD --merges --oneline # Merges between branches
git log -p # Show patches (diffs)
git log --since="2 weeks ago" # Time-based filtering
git log --author="John Doe" # Author-based filteringWhen to use: Understanding project history, finding specific changes, preparing release notes.
Shows differences between commits, branches, files, or working directory.
git diff # Working directory vs staging area
git diff --staged # Staging area vs last commit
git diff HEAD^ HEAD # Last change (previous commit vs current)
git diff main feature/branch # Compare branches
git diff v1.0.0 v1.1.0 # Compare tags
git diff --name-only # Just show changed file namesWhen to use: Reviewing changes before commit, comparing versions, understanding what changed.
Displays information about Git objects (commits, tags, trees).
git show # Show last commit
git show HEAD~2 # Show specific commit
git show v1.0.0 # Show tag information
git show --name-only # Show only changed file namesWhen to use: Examining specific commits, understanding what a tag contains.
Shows who last modified each line of a file and when.
git blame file.txt # Show blame for entire file
git blame -L 10,20 file.txt # Show blame for specific lines
git blame -w file.txt # Ignore whitespace changesWhen to use: Understanding code ownership, finding when bugs were introduced, code archaeology.
List commit objects in reverse chronological order.
git rev-list HEAD # List all commits from HEAD
git rev-list --count HEAD # Count commits from HEAD
git rev-list --since="1 week ago" HEAD # Recent commits
git rev-list --author="John" HEAD # Commits by authorWhen to use: Scripting Git operations, advanced history analysis, building custom tools.
Finding problems and understanding issues
Uses binary search to find the commit that introduced a bug.
git bisect start # Start bisecting
git bisect bad # Mark current commit as bad
git bisect good v1.0.0 # Mark known good commit
git bisect run make test # Automate bisect with test command
git bisect reset # End bisect sessionWhen to use: Finding regression bugs, identifying when problems were introduced, debugging complex issues.
Records and reuses merge conflict resolutions automatically.
git config rerere.enabled true # Enable rerere
git rerere # Show recorded resolutions
git rerere diff # Show current resolution
git rerere forget path/to/file # Forget resolution for fileWhen to use: Working with long-lived branches, repetitive merge conflicts, complex rebasing scenarios.
Powerful tools for complex workflows
Manages repositories inside other repositories.
git submodule add https://github.com/user/lib.git lib # Add submodule
git submodule init # Initialize submodules
git submodule update # Update submodules
git submodule update --remote # Update to latest remote
git submodule foreach git pull origin main # Update all submodulesWhen to use: Including external libraries, managing dependencies, modular project structure.
Resets current HEAD to specified state, powerful but dangerous.
git reset --soft HEAD~1 # Undo commit, keep changes staged
git reset --mixed HEAD~1 # Undo commit and unstage (default)
git reset --hard HEAD~1 # Undo commit and discard changes
git reset file.txt # Unstage specific fileWhen to use: Undoing commits, unstaging files, cleaning up local history before pushing.
--hard permanently destroys uncommitted changes!
Re-applies commits on top of another base tip, rewriting history.
git rebase main # Rebase current branch onto main
git rebase -i HEAD~3 # Interactive rebase last 3 commits
git rebase --continue # Continue after resolving conflicts
git rebase --abort # Abort rebase operationWhen to use: Cleaning up commit history, squashing commits, maintaining linear history.
Squashing is done through interactive rebase, not a standalone command
Combines multiple commits into a single commit, cleaning up history.
git rebase -i HEAD~3 # Interactive rebase to squash last 3 commits
# In the editor that opens:
# pick abc1234 First commit
# squash def5678 Second commit # Change 'pick' to 'squash' or 's'
# squash ghi9012 Third commit # This will combine all into first commit
git reset --soft HEAD~3 && git commit # Alternative: soft reset + new commitWhen to use: Cleaning up feature branch before merging, combining related commits, preparing clean history.
Manages multiple working trees attached to the same repository.
git worktree add ../feature feature/branch # Create new worktree
git worktree list # List all worktrees
git worktree remove ../feature # Remove worktree
git worktree prune # Clean up stale referencesWhen to use: Working on multiple branches simultaneously, testing different versions, parallel development.
Create new commits that undo the changes from previous commits.
git revert HEAD # Revert the last commit
git revert abc1234 # Revert specific commit
git revert --no-edit HEAD # Revert without opening editor
git revert HEAD~3..HEAD # Revert range of commitsWhen to use: Undoing changes in shared branches, maintaining clean history, safe rollback of features.
Working with others and sharing code
Sends patches via email for code review and contribution.
git send-email [email protected] *.patch # Send patch files
git send-email --compose [email protected] # Compose emailWhen to use: Contributing to projects that use email workflows, Linux kernel development, traditional patch-based collaboration.
Generates a summary of pending changes for pull requests.
git request-pull origin/main https://github.com/user/repo.git featureWhen to use: Creating formal pull request descriptions, summarizing changes for maintainers.
Keeping your repository healthy and optimized
Garbage collection - optimizes repository by packing and cleaning up.
git gc # Standard garbage collection
git gc --aggressive # More thorough optimization
git gc --prune=now # Remove unreachable objects immediatelyWhen to use: Regular maintenance, after large operations, optimizing repository size.
File system check - verifies repository integrity.
git fsck # Check repository integrity
git fsck --full # Complete check including pack filesWhen to use: Diagnosing repository corruption, verifying after recovery operations.
Export repository history in a format suitable for importing.
git fast-export --all > repo.dump # Export entire repository
git fast-export main > branch.dump # Export specific branch
git fast-export --since="2023-01-01" HEAD > recent.dump # Export recent historyWhen to use: Repository migration, creating backups, filtering repository history, converting between VCS.
Import repository history from fast-export stream.
git fast-import < repo.dump # Import repository from dump
cat backup.dump | git fast-import # Import from pipe
git fast-import --quiet < data.dump # Import silentlyWhen to use: Repository migration, restoring from backups, importing from other VCS, repository reconstruction.
Prune all unreachable objects from the object database.
git prune # Prune unreachable objects
git prune --dry-run # Show what would be pruned
git prune --expire=now # Prune all unreachable objects immediatelyWhen to use: Manual repository cleanup, reclaiming disk space, maintenance after complex operations.
Remove untracked files from the working tree.
git clean -f # Remove untracked files
git clean -fd # Remove untracked files and directories
git clean -n # Dry run - show what would be removed
git clean -fX # Remove only ignored filesWhen to use: Cleaning build artifacts, removing temporary files, preparing clean workspace.
Aliases are created using git config, not a standalone command
Create shortcuts for frequently used Git commands.
git config --global alias.co checkout # Create "git co" shortcut
git config --global alias.br branch # Create "git br" shortcut
git config --global alias.ci commit # Create "git ci" shortcut
git config --global alias.st status # Create "git st" shortcut
git config --global alias.unstage "reset HEAD --" # Create "git unstage"
git config --global alias.last "log -1 HEAD" # Create "git last" shortcut
git config --global alias.visual "!gitk" # External command alias
git config --global alias.graph "log --oneline --graph --decorate --all"When to use: Creating shortcuts for frequently used commands, simplifying complex Git operations, improving workflow efficiency.
Complex merge log analysis:
# Show merge commits between branches
git log origin/main..HEAD --merges --oneline
# Show commits that changed specific file
git log -p --follow -- filename.txt
# Find commits that added or removed specific text
git log -S "function_name" --onelineBranch management shortcuts:
# Delete all merged branches except main
git branch --merged | grep -v "\*\|main\|develop" | xargs -n 1 git branch -d
# Push new branch and set upstream in one command
git push -u origin feature/new-feature
# Rename and push branch in one operation (delete old, push new)
git push origin :old_name new_name # Deletes old_name, pushes new_nameGit contrib - Community contributed scripts (varies by installation):
# These may be available depending on your Git installation:
git summary # Show repository statistics
git effort # Show file effort/churn metrics
git authors # List authors and contribution stats
# Check what contrib scripts you have:
ls $(git --exec-path)/git-* # List all git commandsGit jump - Quick navigation (requires custom setup):
# Example custom git jump alias for quick branch switching:
git config alias.jump '!f() { git checkout $(git branch | grep "$1" | head -1 | sed "s/^..//"); }; f'
# Usage after setup:
git jump feature # Jumps to first branch matching "feature"Repository analysis:
git shortlog -sn # Contributor statistics
git rev-list --count HEAD # Total number of commits
git ls-files | wc -l # Count tracked files
git for-each-ref --sort=-committerdate refs/heads/ # Recently active branchesAdvanced file operations:
git show HEAD:path/to/file # Show file content from specific commit
git restore --source=HEAD~1 file.txt # Restore file from previous commit
git ls-tree -r HEAD # List all files in repositoryDebugging and analysis:
git reflog # Show reference log (recover "lost" commits)
git cherry-pick commit-hash # Apply specific commit to current branch
git stash # Temporarily save uncommitted changes
git clean -fd # Remove untracked files and directoriesUseful Git aliases to add to your ~/.gitconfig:
[alias]
co = checkout
br = branch
ci = commit
st = status
unstage = reset HEAD --
last = log -1 HEAD
visual = !gitk
pushf = push --force-with-lease
sync = !git fetch --all && git rebase origin/mainJust like elements in the periodic table have patterns and properties, Git commands also have characteristics:
- Atomic Operations: Commands that complete entirely or not at all
- State Changing: Commands that modify repository state vs. read-only operations
- Local vs Remote: Commands that work locally vs. those that communicate with remotes
- Safe vs Destructive: Commands that can lose data vs. those that cannot
| Element Group | Primary Use | Key Commands |
|---|---|---|
| Basic | Daily workflow | init, add, commit, status, config, stash, rm, mv, alias* |
| Navigation | Moving around | checkout, branch, switch, tag, reflog |
| Remote | Collaboration | remote, push, pull, fetch, clone |
| History | Understanding | log, diff, show, blame, rev-list |
| Debugging | Problem solving | bisect, rerere |
| Advanced | Power features | submodule, reset, rebase, squash, worktree, revert |
| Collaboration | Working together | send-email, request-pull |
| Maintenance | Repository health | gc, fsck, fast-export, fast-import, prune, clean |
This project is licensed under the terms of the MIT open source license. Please refer to the LICENSE file for the full terms.
Made with β€οΈ by Copilot and mbianchidev for Git Merge 2025 "git commit --amend -m 'Oops, fixed my message!' -m 'p.s. I see you reaching for VSCode's UI instead of the good ol' command line for git'"