Files
cheatsheets/git-cheatsheet.md
2025-08-31 23:24:28 +02:00

2.5 KiB

Git Cheatsheet

This is a quick reference for common Git commands.

Configuration

  • git config --global user.name "Your Name": Set your username
  • git config --global user.email "your.email@example.com": Set your email
  • git config --list: View configuration

Creating Repositories

  • git init: Initialize a new Git repository in the current directory
  • git clone <repository-url>: Clone an existing repository

Basic Snapshotting

  • git status: Show the working tree status
  • git add <file>: Add a file to the staging area
  • git add .: Add all changes in the current directory to the staging area
  • git commit -m "commit message": Commit staged changes with a message
  • git commit -a -m "commit message": Commit all tracked changes with a message (skips staging)

Branching and Merging

  • git branch: List all 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 merge <branch-name>: Merge a branch into the current branch
  • git branch -d <branch-name>: Delete a branch

Remote Repositories

  • git remote add <name> <url>: Add a remote repository (e.g., origin)
  • git push <remote> <branch>: Push changes to a remote branch
  • git pull <remote> <branch>: Pull changes from a remote branch
  • git fetch: Fetch changes from remote without merging

Inspection and Comparison

  • git log: Show commit history
  • git log --oneline: Show commit history in one line per commit
  • git diff: Show changes between commits, commit and working tree, etc.
  • git show <commit>: Show details of a commit

Undoing Changes

  • git reset --hard: Reset to the last commit, discarding changes
  • git revert <commit>: Create a new commit that undoes a previous commit
  • git checkout -- <file>: Discard changes in a file

Stashing

  • git stash: Stash changes in a dirty working directory
  • git stash apply: Apply stashed changes
  • git stash list: List stashed changes

Advanced Commands

  • git rebase <branch>: Rebase current branch onto another
  • git cherry-pick <commit>: Apply a commit from another branch
  • git reflog: Show reference log
  • git bisect: Binary search for bug introduction

Submodules

  • git submodule add <repo> <path>: Add submodule
  • git submodule update --init: Initialize submodules

For more details, refer to the official Git documentation: https://git-scm.com/docs and https://www.geeksforgeeks.org/git-cheat-sheet/"