Git Cheat Sheet

This cheat sheet features the most important and commonly used git commands for easy reference. Git Tips and Tricks (A collection of useful tips and tricks for Git)

External Resources

Table of contents

  1. Configurations
    1. Setup & Configuration
  2. Creating and Managing Repositories
    1. Initialize a Repository
    2. Check Repository Status
  3. Working with Files
    1. Adding and Removing Files
    2. Viewing Changes
  4. Committing Changes
    1. Basic Commit
    2. Skipping Staging Area
  5. Branching
    1. Basic Branch Commands
  6. Merging and Rebasing
    1. Merging
    2. Resolving Merge Conflicts
    3. Rebasing
  7. Stashing
    1. Saving Temporary Changes
  8. Viewing History
    1. Log Commands
  9. Undoing Changes
    1. Discard Changes
    2. Resetting Commits
  10. Synchronizing with Remote
    1. Managing Remotes
    2. Fetch, Pull, and Push
  11. Tags
    1. Working with Tags
  12. Collaboration
    1. Viewing and Working on Changes
    2. Resolving Conflicts
  13. Advanced Commands
    1. Clean Untracked Files
    2. Cherry Picking
    3. Squashing Commits
  14. Hooks
    1. Common Hooks
  15. Troubleshooting
    1. Unfinished Operations
    2. Fix a Broken Push
  16. Rewriting History
    1. Modifying the Most Recent Commit
    2. Changing the Message of an Older Commit
    3. Squashing Commits (Combining Commits)
    4. Removing a Commit from History
    5. Reverting Changes in a Commit
    6. Rewriting History Across the Entire Repository
    7. Force Push to Rewrite Remote History
  17. Advanced Stashing
    1. Save Changes to Stash
    2. List Stashes
    3. Apply or Pop Stashes
    4. Stashing Specific Files
    5. View Stash Contents
    6. Create a Branch from Stash
    7. Drop or Clear Stashes
  18. Key Differences: Apply vs. Pop
  19. Additional Tips for History Rewriting
    1. Remove Sensitive Data from History

Configurations

Setup & Configuration

git config --global user.name "Your Name"           # Set your name
git config --global user.email "you@example.com"   # Set your email
git config --list                                  # List all settings
git config --global core.editor "code --wait"      # Set default editor (e.g., VS Code)
git config --global core.autocrlf true             # Handle line endings

Creating and Managing Repositories

Initialize a Repository

git init                        # Create a new local repository
git clone <url>                 # Clone a remote repository

Check Repository Status

git status                      # Check status of files

Working with Files

Adding and Removing Files

git add <file>                  # Stage a specific file
git add .                       # Stage all changes in the current directory
git rm <file>                   # Remove a file from working directory and stage it
git rm --cached <file>          # Remove file from staging but keep in working directory

Viewing Changes

git diff                        # View unstaged changes
git diff --staged               # View staged changes

Committing Changes

Basic Commit

git commit -m "Your commit message"        # Commit staged changes
git commit --amend                        # Amend the last commit

Skipping Staging Area

git commit -a -m "Your commit message"    # Add all tracked files and commit

Branching

Basic Branch Commands

git branch                              # List branches
git branch <branch_name>                # Create a new branch
git checkout <branch_name>              # Switch to a branch
git checkout -b <branch_name>           # Create and switch to a new branch
git branch -d <branch_name>             # Delete a branch
git branch -D <branch_name>             # Force delete a branch

Merging and Rebasing

Merging

git merge <branch_name>                  # Merge a branch into the current branch

Resolving Merge Conflicts

# Edit the conflicting files, then:
git add <conflicted_file>
git commit

Rebasing

git rebase <branch_name>                 # Rebase onto another branch
git rebase --continue                    # Continue rebase after conflict
git rebase --abort                       # Abort rebase

Stashing

Saving Temporary Changes

git stash                                # Stash current changes
git stash list                           # List stashes
git stash apply                          # Apply the most recent stash
git stash pop                            # Apply the stash and delete it
git stash drop                           # Delete the stash

Viewing History

Log Commands

git log                                  # View commit history
git log --oneline                        # Compact commit history
git log --graph --decorate               # Visualize branch history
git log -p                               # Show changes in each commit

Undoing Changes

Discard Changes

git restore <file>                       # Discard changes in working directory
git restore --staged <file>              # Unstage a file

Resetting Commits

git reset --soft <commit_hash>           # Reset to a previous commit (keep changes staged)
git reset --mixed <commit_hash>          # Reset to a previous commit (unstage changes)
git reset --hard <commit_hash>           # Reset to a previous commit (discard changes)

Synchronizing with Remote

Managing Remotes

git remote add origin <url>              # Add a remote repository
git remote -v                            # List remotes
git remote remove <name>                 # Remove a remote

Fetch, Pull, and Push

git fetch                                # Download changes from the remote
git pull                                 # Fetch and merge changes from remote
git push                                 # Push changes to remote repository
git push -u origin <branch_name>         # Push branch and set upstream

Tags

Working with Tags

git tag                                  # List all tags
git tag <tag_name>                       # Create a new tag
git tag -a <tag_name> -m "Message"       # Create an annotated tag
git push origin <tag_name>               # Push a tag to remote
git push origin --tags                   # Push all tags to remote

Collaboration

Viewing and Working on Changes

git blame <file>                         # Show who changed each line of a file
git show <commit_hash>                   # Show details of a commit

Resolving Conflicts

# After resolving conflicts:
git add <file>
git commit

Advanced Commands

Clean Untracked Files

git clean -f                             # Remove untracked files
git clean -fd                            # Remove untracked files and directories

Cherry Picking

git cherry-pick <commit_hash>            # Apply a specific commit to the current branch

Squashing Commits

git rebase -i HEAD~<number_of_commits>   # Squash commits interactively

Hooks

Common Hooks

# Located in .git/hooks/
pre-commit                               # Script that runs before a commit
pre-push                                 # Script that runs before a push

Troubleshooting

Unfinished Operations

git merge --abort                        # Abort a merge
git rebase --abort                       # Abort a rebase
git cherry-pick --abort                  # Abort a cherry-pick

Fix a Broken Push

git push --force                         # Force push changes

Rewriting History

Modifying the Most Recent Commit

git commit --amend                           # Amend the last commit (e.g., update message or files)
git commit --amend --no-edit                 # Amend without changing the commit message

Changing the Message of an Older Commit

  1. Start an interactive rebase:
    git rebase -i HEAD~<number_of_commits>
    
  2. Mark the commit to edit by replacing pick with edit.
  3. Modify the commit:
    git commit --amend
    
  4. Continue the rebase:
    git rebase --continue
    

Squashing Commits (Combining Commits)

  1. Start an interactive rebase:
    git rebase -i HEAD~<number_of_commits>
    
  2. Change pick to squash (or s) for the commits you want to merge.
  3. Edit the commit message when prompted.

Removing a Commit from History

git rebase -i HEAD~<number_of_commits>       # Mark the commit with `drop` to remove it

Reverting Changes in a Commit

git revert <commit_hash>                     # Create a new commit that undoes a specific commit

Rewriting History Across the Entire Repository

Use with caution!

git filter-branch --tree-filter 'rm -rf <file_or_directory>' HEAD

Force Push to Rewrite Remote History

git push --force                             # Push rewritten history to remote

Advanced Stashing

Save Changes to Stash

git stash                                    # Save changes to stash
git stash save "Message"                     # Save with a message

List Stashes

git stash list                               # Show all stashes

Apply or Pop Stashes

git stash apply                              # Apply the latest stash (without removing it)
git stash apply stash@{2}                    # Apply a specific stash
git stash pop                                # Apply and remove the latest stash
git stash pop stash@{2}                      # Apply and remove a specific stash

Stashing Specific Files

git stash push -m "Message" <file>           # Stash specific files

View Stash Contents

git stash show -p                            # Show changes in the most recent stash
git stash show -p stash@{2}                  # Show changes in a specific stash

Create a Branch from Stash

git stash branch <branch_name>               # Create a new branch with the stash applied

Drop or Clear Stashes

git stash drop                               # Remove the latest stash
git stash drop stash@{2}                     # Remove a specific stash
git stash clear                              # Remove all stashes

Key Differences: Apply vs. Pop

  • git stash apply:Applies the stash but keeps it in the stash list.
  • git stash pop:Applies the stash and removes it from the stash list.

Additional Tips for History Rewriting

Remove Sensitive Data from History

If a file with sensitive data (e.g., passwords.txt) was committed:

git filter-branch --force --index-filter \
  'git rm --cached --ignore-unmatch passwords.txt' \
  --prune-empty --tag-name-filter cat -- --all

Then force-push to update the remote:

git push --force --all

If you need additional details on any command, use:

git help <command>