git cheatsheet for developers

May 23, 2025 (3w ago)

Setup & Configuration

  1. Initialize a new Git repository:

    git init
  2. Set your global Git username:

    git config --global user.name "Your Name"
  3. Set your global Git email:

    git config --global user.email "you@example.com"

Staging & Committing

  1. Stage a specific file:

    git add <file>
  2. Stage all changes:

    git add .
  3. Commit staged changes with a message:

    git commit -m "message"
  4. Stage and commit tracked files in one step:

    git commit -am "message"

Branching

  1. List all branches:

    git branch
  2. Create a new branch:

    git branch <branch-name>
  3. Switch to a different branch:

    git checkout <branch-name>
  4. Create and switch to a new branch:

    git checkout -b <branch-name>

Merging & Rebasing

  1. Merge another branch into the current branch:

    git merge <branch-name>
  2. Reapply commits from your branch onto another:

    git rebase <branch-name>
  3. Pull changes from remote with rebase:

    git pull --rebase

Viewing History

  1. View full commit history:

    git log
  2. View commit history in a compact form:

    git log --oneline
  3. Show current staging status:

    git status
  4. Show changes in working directory vs last commit:

    git diff

Remote Operations

  1. Clone a remote repository:

    git clone <repo-url>
  2. Show configured remotes:

    git remote -v
  3. Push local commits to remote:

    git push
  4. Fetch updates from remote without merging:

    git fetch
  5. Fetch and merge remote changes:

    git pull

Undoing Changes

  1. Unstage a file:

    git reset <file>
  2. Discard local changes to a file:

    git checkout -- <file>
  3. Create a new commit that undoes a previous one:

    git revert <commit-hash>
  4. Reset to a specific commit and erase all changes:

    git reset --hard <commit-hash>

Clean Up

  1. Temporarily save uncommitted changes:

    git stash
  2. Reapply the last stashed changes:

    git stash pop