cluster deboistech.in Get started
Cheatsheet

Git Basics

The Git commands you use daily: record changes, look at history, branch and merge, share with a remote, and undo mistakes.

Practice this path

Set up

  • git config --global user.name "Your Name"

    Your name on every commit

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

    Your email on every commit

  • git config --global init.defaultBranch main

    Name new repositories main

  • git init

    Start a repository in the current folder

  • git clone https://github.com/org/repo.git

    Copy a remote repository to your machine

Record changes

Working folder -> staging area -> commit.

  • git status -sb

    Short status with the branch

  • git add file.txt  |  git add -p

    Stage a file, or choose pieces of a file to stage

  • git commit -m "Fix login redirect"

    Save the staged changes as a commit

  • git diff  |  git diff --staged

    See unstaged changes, or what is about to be committed

  • git restore file.txt

    Throw away unstaged changes in a file

  • git restore --staged file.txt

    Unstage a file but keep the changes

  • git rm --cached secret.env

    Stop tracking a file but keep it on disk

Look at history

  • git log --oneline --graph --all

    A compact picture of all branches

  • git log -p -- path/file

    The changes made to one file over time

  • git show abc1234

    What one commit changed

  • git blame file.py

    Who last changed each line

  • git log --author="Ann" --since="2 weeks ago"

    Filter the history

  • git log -S"needle"

    Find commits that added or removed a piece of text

Branches

  • git switch -c feature/login

    Create a branch and move to it

  • git switch main

    Move to another branch

  • git branch -vv

    List branches with their upstream and last commit

  • git merge feature/login

    Bring a branch into the current one

  • git rebase main

    Replay your commits on top of the latest main

  • git branch -d feature/login

    Delete a branch that is already merged

  • git cherry-pick abc1234

    Copy one commit onto the current branch

Remotes

  • git remote -v

    Show the remotes

  • git fetch --prune

    Download new work and forget deleted branches, without merging

  • git pull --rebase

    Fetch and replay your commits on top

  • git push -u origin feature/login

    Publish a branch and remember it as the upstream

  • git push --force-with-lease

    Overwrite a remote branch, but only if nobody else pushed

Undo things

  • git commit --amend

    Change the last commit (message or content) before you share it

  • git reset --soft HEAD~1

    Undo the last commit, keep the changes staged

  • git reset --hard HEAD~1

    Undo the last commit and delete its changes

  • git revert abc1234

    Add a new commit that undoes an old one (safe for shared history)

  • git stash push -u -m "wip"

    Put work in progress aside, including new files

  • git stash pop

    Bring the stashed work back

  • git reflog

    Every place HEAD has been: the way back from a bad reset

Conflicts and tags

  • git merge --abort  |  git rebase --abort

    Give up on a merge or rebase that went wrong

  • git checkout --ours file  |  --theirs file

    Take one side of a conflicted file

  • git add file && git rebase --continue

    Finish a rebase step after fixing a conflict

  • git tag -a v1.2.0 -m "Release 1.2.0"

    Mark a release

  • git push origin v1.2.0

    Publish a tag

  • echo "node_modules/" >> .gitignore

    Never track a folder or file pattern

Cheatsheet for the Git Basics path · Deboistech · All cheatsheets