Git
Table of Contents
Distributed version control system. Every developer has a full copy of the repository including its complete history. The standard tool for source code management — used for both personal projects and team collaboration.
Related: Version Control DevOps
Setup
# Global config — set once per machine git config --global user.name "Your Name" git config --global user.email "you@example.com" git config --global core.editor "emacs" # Show current config git config --list
Starting a Repository
# Initialise a new repo git init git init my-project && cd my-project # Clone an existing repo git clone https://github.com/user/repo.git git clone https://github.com/user/repo.git my-folder # clone into named dir git clone --depth 1 https://github.com/user/repo.git # shallow clone, latest commit only
Core Workflow
# Check status git status git status -s # short format # Stage changes git add file.py git add . # stage everything git add -p # interactive — stage hunks selectively # Commit git commit -m "descriptive message" git commit -am "stage tracked files and commit" # skip git add for tracked files git commit --amend # modify the last commit (message or content) # View history git log git log --oneline # compact one-line-per-commit view git log --oneline --graph # show branch graph git log -p # show diffs in log git log --author="Alice" git log --since="2 weeks ago" # View changes git diff # unstaged changes git diff --staged # staged changes (ready to commit) git diff HEAD # all changes since last commit git diff main..feature # diff between two branches
Branches
# List branches git branch # local git branch -r # remote git branch -a # all # Create and switch git switch -c feature/login # create and switch (modern syntax) git checkout -b feature/login # equivalent older syntax # Switch branch git switch main git checkout main # Delete branch git branch -d feature/login # safe delete (checks merged) git branch -D feature/login # force delete # Rename current branch git branch -m new-name
Merging and Rebasing
# Merge feature into current branch git switch main git merge feature/login # Merge with explicit commit (no fast-forward) git merge --no-ff feature/login # Abort a conflicted merge git merge --abort # Rebase current branch onto main git switch feature/login git rebase main # Interactive rebase — squash, reword, reorder last N commits git rebase -i HEAD~3 # Abort rebase git rebase --abort
Prefer merge for shared branches, rebase for cleaning up local work before pushing.
Remote Operations
# Show remotes git remote -v # Add remote git remote add origin https://github.com/user/repo.git # Fetch (download without merging) git fetch origin git fetch --all # Pull (fetch + merge) git pull git pull origin main # Push git push origin main git push -u origin feature/login # set upstream tracking git push --force-with-lease # safer force push (checks for new remote commits) # Delete remote branch git push origin --delete feature/login
Undoing Things
# Discard changes in working directory (unstaged) git restore file.py git restore . # discard all unstaged changes # Unstage (keep changes in working dir) git restore --staged file.py git reset HEAD file.py # equivalent older syntax # Undo last commit (keep changes staged) git reset --soft HEAD~1 # Undo last commit (keep changes unstaged) git reset --mixed HEAD~1 # Undo last commit (discard changes entirely) git reset --hard HEAD~1 # Revert a specific commit (creates a new undo commit — safe for shared branches) git revert abc1234 # Discard all local changes and reset to remote git fetch origin git reset --hard origin/main
Stashing
Save work in progress without committing:
git stash # stash current changes git stash push -m "wip: auth" # stash with description git stash list # list all stashes git stash pop # apply and remove most recent stash git stash apply stash@{2} # apply specific stash without removing git stash drop stash@{0} # delete a stash git stash clear # delete all stashes
Tagging
# Create tag git tag v1.0.0 git tag -a v1.0.0 -m "Release 1.0.0" # annotated tag (preferred) # List tags git tag # Push tags git push origin v1.0.0 git push origin --tags # push all tags # Delete tag git tag -d v1.0.0 git push origin --delete v1.0.0
Useful One-Liners
# Show what changed in a specific commit git show abc1234 # Search commit messages git log --grep="fix" # Find which commit introduced a string git log -S "password" --source --all # Show file at a specific commit git show abc1234:path/to/file.py # List files changed in last commit git diff --name-only HEAD~1 HEAD # Cherry-pick a single commit from another branch git cherry-pick abc1234 # Find the commit that introduced a bug (binary search) git bisect start git bisect bad # current commit is bad git bisect good v1.0.0 # last known good state # Git checks out midpoints — mark each as good or bad git bisect reset # when done
.gitignore
# Common patterns *.pyc __pycache__/ .env .venv/ node_modules/ *.log .DS_Store # Security — never commit these .env.local *.pem *.key id_rsa secrets.yml
# Check what is ignored git status --ignored git check-ignore -v filename # why is this file ignored?
Security Relevance
# Search a repo's full history for secrets — common post-exploitation find git log -p | grep -i "password\|secret\|api_key\|token" git log --all -p -- .env # history of a specific sensitive file # Check git config for credentials stored in plaintext cat .git/config cat ~/.gitconfig # Find all branches including remote (may reveal dev/staging branches) git branch -a # Exposed .git directory on a web server is a critical finding # An attacker can reconstruct the full source code: # https://target.com/.git/config — check if accessible
Tips
git restoreis the modern replacement forgit checkout -- fileandgit reset HEAD file— use it for clarity--force-with-leaseis always safer than--forceon pushes — it fails if someone else has pushed since your last fetch- Stage with
-p(patch mode) when you want fine-grained control over what goes into each commit rather than committing everything - Commit messages should complete the sentence "If applied, this commit will…" — keeps history readable
- The
.gitdirectory being accessible on a web server is a Critical finding — it allows full source code reconstruction including history which may contain removed secrets