Malcolm Low | Practical AI, Computing & Quantum Research

Singapore-based practical guides, tutorials and experiments in AI, computing, modelling, simulation, optimisation and quantum computing, with research notes and hands-on workflows.

,

Quick Guide to GIT

Yes, Git tracks developer code history. Running unverified rebase commands can overwrite shared team commits. Master essential branch & merge workflows →

·

Written by

Quick Reference Guide

Quick Guide to Git

A practical reference for Git version control. Covers setup, staging, committing, branching, remotes, stashing, undoing changes, and .gitignore.

Note: This guide was originally published in 2020 and has been updated in May 2026 with corrected commands, additional sections, and improved formatting, with assistance from Claude (Anthropic).

See Also — Other Quick Guides

Quick guide to n8n  |  Quick guide to NSCC ASPIRE2A  |  Quick guide to Docker  |  Quick guide to CVS  |  Quick guide to nawk  |  Quick guide to Emacs  |  Quick guide to GNUPlot

Setup and Configuration

Set your identity once globally. These details appear in every commit you make:

git config --global user.name "Your Name"
git config --global user.email "[email protected]"
git config --global core.editor emacs     # or vim, nano, code
git config --global color.ui true
git config --list                         # verify all settings

Config is stored in ~/.gitconfig. Omit --global to set options for the current repository only.

Creating and Cloning Repositories

# Initialise a new repo in the current directory
git init

# Clone an existing remote repository
git clone https://github.com/user/repo.git
git clone https://github.com/user/repo.git my-folder  # into a named folder

git init creates a hidden .git/ directory. git clone also sets up the remote automatically as origin.

Core Workflow — Stage and Commit

Git has three states: working directory (edited files), staging area (files queued for the next commit), and the repository (committed history).

git status                        # show state of working dir and staging area

git add filename.py               # stage a specific file
git add .                         # stage all changes in current directory
git add -p                        # interactively stage hunks

git commit -m "Your message"      # commit staged changes
git commit -am "Your message"     # stage all tracked files and commit in one step
git commit --amend -m "New msg"   # rewrite the most recent commit message

Keep commit messages short and imperative: “Fix off-by-one in loop”, not “Fixed the bug.”

Viewing Changes and History

# Diff
git diff                          # changes in working dir (unstaged)
git diff --staged                 # changes staged for next commit
git diff --color-words            # highlight changed words inline
git diff abc123..def456           # diff between two commits
git diff main..feature-branch     # diff between two branches

# Log
git log                           # full commit history
git log --oneline                 # compact one-line format
git log --oneline --graph --all   # visual branch graph
git show abc123 --color-words     # full diff of a specific commit

Navigating diff / log output (uses less):

KeyAction
f / bForward / backward one page
EnterNext line
-SToggle line wrap
qQuit

Undoing Changes

# Discard unstaged changes in a file
git restore filename.py
git checkout -- filename.py       # older equivalent
git checkout -- .                 # discard all unstaged changes

# Unstage a file (keep changes in working dir)
git restore --staged filename.py
git reset HEAD filename.py        # older equivalent

# Revert a committed change (creates a new undo commit)
git revert abc123

# Check out an old version of a file
git checkout abc123 -- filename.py

# Remove untracked files
git clean -n                      # dry run: list files to be removed
git clean -f                      # actually remove them

Reset — moving HEAD backwards:

CommandEffect
git reset --soft HEAD~1Undo last commit; keep changes staged
git reset --mixed HEAD~1Undo last commit; keep changes unstaged (default)
git reset --hard HEAD~1Undo last commit and discard all changes permanently

Branches

git branch                        # list local branches
git branch -a                     # list all branches including remote
git branch feature-x              # create a new branch
git checkout feature-x            # switch to a branch
git checkout -b feature-x         # create and switch in one step (classic)
git switch -c feature-x           # modern equivalent

git merge feature-x               # merge feature-x into current branch
git branch -d feature-x           # delete a branch (safe; checks merge status)
git branch -D feature-x           # force delete
git branch -m old-name new-name   # rename a branch

Always work on a feature branch; merge into main only when the work is ready. If a merge produces conflicts, Git marks the files with <<<<<<< markers. Edit the files to resolve, then git add and git commit.

Remote Repositories

# Add a remote
git remote add origin https://github.com/user/repo.git
git remote -v                     # list remotes

# Push and pull
git push origin main              # push local main to remote
git push -u origin main           # set upstream; future: just git push
git pull                          # fetch + merge from upstream
git fetch                         # fetch only (no merge)
git fetch --prune                 # fetch and remove deleted remote branches

# Tracking
git branch -u origin/main         # set upstream for current branch
git push origin --delete feature-x  # delete a remote branch

Prefer git fetch followed by a manual merge or rebase over a blind git pull to keep better control over what gets merged.

Stashing

Stash lets you shelve uncommitted work temporarily so you can switch branches with a clean slate:

git stash                         # stash current changes
git stash push -m "WIP: login"    # stash with a label
git stash list                    # list all stashes
git stash pop                     # apply most recent stash and remove it
git stash apply stash@{2}         # apply a specific stash (keep it in list)
git stash drop stash@{0}          # delete a specific stash
git stash clear                   # delete all stashes

Tags

Tags mark specific commits, typically for releases. Annotated tags (with -a) store a message and tagger info; lightweight tags are just a pointer.

git tag                           # list all tags
git tag v1.0.0                    # lightweight tag on current commit
git tag -a v1.0.0 -m "Release"   # annotated tag
git push origin v1.0.0            # push a specific tag
git push origin --tags            # push all tags
git tag -d v1.0.0                 # delete a local tag
git push origin --delete v1.0.0  # delete a remote tag

.gitignore

Create a .gitignore file in your repository root to exclude files from tracking:

# Comments start with #
*.pyc                 # ignore all .pyc files
__pycache__/          # ignore a directory (trailing slash)
log/*.txt             # ignore .txt files inside log/
!important.txt        # exception: do NOT ignore this file
*.[oa]                # wildcard: ignore .o and .a files
*?[0-9]               # regex-style patterns

To ignore files globally across all repositories:

git config --global core.excludesfile ~/.gitignore_global

GitHub maintains a collection of ready-made templates at github.com/github/gitignore.

Quick Command Reference

CommandWhat it does
git initInitialise a new repository
git cloneCopy a remote repository locally
git statusShow working dir and staging area state
git addStage files for the next commit
git commitSave staged changes to history
git diffShow unstaged changes
git log --onelineCompact commit history
git branch -bCreate and switch to a new branch
git mergeMerge a branch into the current branch
git pullFetch and merge from remote
git pushPush commits to remote
git stashShelve uncommitted changes temporarily
git revertCreate an undo commit for a previous commit
git tag -aCreate an annotated release tag

For the complete reference, see the official Git documentation or run git help <command> for any command.

Comments

Leave a Reply