Skip to content

Latest commit

 

History

History
433 lines (286 loc) · 5.26 KB

File metadata and controls

433 lines (286 loc) · 5.26 KB

Git Cheat Sheet

This file is a practical reference for everyday Git usage when working with local projects and GitHub.


Recommended file nameing

  • git-cheat-sheet.md

Lowercase, hyphen separated, and descriptive is common practice in repositories.


Setup (run once per machine)

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

git config --global core.autocrlf true   # recommended on Windows

Check config:

git config --global --list

Create and publish a new repository

Initialize local repo and create first commit:

git init
git add .                 # stage all files in project
git commit -m "Initial commit"

Connect to GitHub and push:

git branch -M main        # ensure branch name is main
git remote add origin https://github.com/USERNAME/REPO.git
git push -u origin main   # push and set upstream
git init
git add .
git commit -m "Initial commit"

git branch -M main
git remote add origin https://github.com/USERNAME/REPO.git
git push -u origin main

Daily workflow

Check current state of your working directory (what is modified, staged, or untracked):

git status

Stage files (add changes to the next commit):

git add file.py     # stage only one file
git add .           # stage ALL files in the current folder and subfolders

Commit staged changes with a short message:

git commit -m "Short clear message"

Push your commits to GitHub:

git push

Pull latest changes from GitHub and merge into your branch:

git pull

Branches

List local branches (current branch has *):

git branch

Create and switch to a new branch:

git checkout -b feature-x

Switch to an existing branch:

git checkout main

Merge another branch into current branch:

git merge feature-x

Delete branch after merge:

git branch -d feature-x

Check status:

git status

Stage files:

git add file.py
git add .

Commit:

git commit -m "Short clear message"

Push:

git push

Pull latest changes:

git pull

Branches

List branches:

git branch

Create and switch:

git checkout -b feature-x

Switch branch:

git checkout main

Merge branch into main:

git checkout main
git merge feature-x

Delete branch:

git branch -d feature-x

Undo and recovery (very important)

Discard local changes in a file (before commit):

git restore file.py     # restore single file
git restore .           # restore ALL files to last commit

Unstage file (keep changes, just remove from staging area):

git restore --staged file.py

Undo last commit but keep files (commit message or staging mistake):

git reset --soft HEAD~1

Undo last commit and discard files (dangerous, permanent):

git reset --hard HEAD~1

Restore file exactly as it was in last commit:

git checkout -- file.py

Undo changes in file (before commit):

git restore file.py
git restore .

Unstage file:

git restore --staged file.py

Undo last commit but keep files:

git reset --soft HEAD~1

Undo last commit and discard files:

git reset --hard HEAD~1

Restore file from last commit:

git checkout -- file.py

View history and changes

Commit history:

git log
git log --oneline --graph --decorate

Show changes:

git diff
git diff --staged

Show details of commit:

git show COMMIT_HASH

Remote repositories

Show configured remotes:

git remote -v

Change remote URL (for example when switching GitHub accounts):

git remote set-url origin https://github.com/USERNAME/REPO.git

Remove remote completely:

git remote remove origin

Show remotes:

git remote -v

Change remote URL:

git remote set-url origin https://github.com/USERNAME/REPO.git

Remove remote:

git remote remove origin

Clean working directory

Preview what will be deleted (safe):

git clean -n

Remove untracked files only:

git clean -f

Remove untracked files and folders (dangerous):

git clean -fd

Remove untracked files:

git clean -f

Remove untracked files and folders:

git clean -fd

Preview first:

git clean -n

.gitignore basics

Create file:

touch .gitignore

Typical Python ignores:

__pycache__/
*.pyc
.env
.venv/

Emergency commands

Abort merge:

git merge --abort

Reset to remote state:

git fetch origin
git reset --hard origin/main

Good commit message style

  • Add python math examples
  • Fix README file
  • Refactor sorting logic
  • Update folder structure

Avoid:

  • update
  • fix stuff
  • changes

Pro tips

  • Commit often, small changes
  • Push at least once per day
  • Use branches for experiments
  • Never be afraid to break things locally, Git can restore almost everything

If you want, you can later split this into:

  • Git basics
  • GitHub workflow
  • Advanced recovery

and turn this repo into a learning resource as well.