ubuntu, updated

git cheat sheet

The git commands an admin needs: clone, commit, branch, pull and push, look at history, undo mistakes, and keep /etc under version control.

The idea in four places

Git tracks changes to files. A change moves through four places:

flowchart LR
    W["Working folder
your files"] -->|git add| S["Staging
next commit"] S -->|git commit| L["Local repo
history"] L -->|git push| R["Remote
GitHub, GitLab..."] R -->|git pull| W

A commit is a saved snapshot with a message. A branch is a named line of commits. origin is the usual name of the remote.

First-time setup

sudo apt install git
git config --global user.name "Anna Muster"
git config --global user.email "anna@example.com"
git config --global init.defaultBranch main

Daily work

WhatCommand
Copy a repo to this servergit clone https://github.com/org/repo.git
Start a repo in the current foldergit init
What changed?git status
Show the changes line by linegit diff (staged: git diff --staged)
Stage a file / everythinggit add file / git add -A
Commitgit commit -m "Increase nginx worker count"
Stage all tracked files and commitgit commit -am "message"
Get the latest changesgit pull
Upload your commitsgit push

Branches

WhatCommand
List branchesgit branch (incl. remote: git branch -a)
Create and switchgit switch -c feature-x
Switchgit switch main
Merge a branch into the current onegit merge feature-x
Delete a branchgit branch -d feature-x
Push a new branchgit push -u origin feature-x

History

WhatCommand
Short historygit log --oneline
As a graph with branchesgit log --oneline --graph --all
History of one filegit log -p -- path/file
What did a commit change?git show COMMIT
Who changed which line?git blame file
Find commits by messagegit log --grep="nginx"
Changes since yesterdaygit log --since=yesterday

COMMIT is the hash from git log (the first 7 characters are enough).

Undo

SituationCommand
Discard changes to a file (not staged)git restore file
Unstage a file (keep the changes)git restore --staged file
Fix the last commit message (not pushed yet)git commit --amend -m "new message"
Undo the last commit, keep the changesgit reset --soft HEAD~1
Undo a pushed commit safelygit revert COMMIT (adds a new commit that reverses it)
Put changes aside for a momentgit stash, later git stash pop
Throw away all local changesgit reset --hard (careful: no undo)
Get a file back from an older commitgit restore --source=COMMIT file

Warning: Never rewrite history that others already pulled (reset, --amend on pushed commits, push --force). Use git revert instead.

.gitignore

List files git should never track, one pattern per line:

*.log
.env
node_modules/
secrets/

Never commit passwords or keys. If it happened: change the secret. Deleting it in a new commit doesn’t remove it from the history.

Keep /etc under version control: etckeeper

etckeeper puts /etc into a git repo and commits automatically, before and after every apt run and once a day. You see what changed in the config and when.

sudo apt install etckeeper                # creates the repo and the first commit
sudo etckeeper commit "Changed SSH port"  # commit your own change with a message
sudo git -C /etc log --oneline            # history of /etc
sudo git -C /etc diff                     # uncommitted changes in /etc

The repo stays local. Don’t push it anywhere public: /etc contains password hashes and keys.

§ home
~/knubertus/home/
1,1 Top