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
| What | Command |
|---|---|
| Copy a repo to this server | git clone https://github.com/org/repo.git |
| Start a repo in the current folder | git init |
| What changed? | git status |
| Show the changes line by line | git diff (staged: git diff --staged) |
| Stage a file / everything | git add file / git add -A |
| Commit | git commit -m "Increase nginx worker count" |
| Stage all tracked files and commit | git commit -am "message" |
| Get the latest changes | git pull |
| Upload your commits | git push |
Branches
| What | Command |
|---|---|
| List branches | git branch (incl. remote: git branch -a) |
| Create and switch | git switch -c feature-x |
| Switch | git switch main |
| Merge a branch into the current one | git merge feature-x |
| Delete a branch | git branch -d feature-x |
| Push a new branch | git push -u origin feature-x |
History
| What | Command |
|---|---|
| Short history | git log --oneline |
| As a graph with branches | git log --oneline --graph --all |
| History of one file | git log -p -- path/file |
| What did a commit change? | git show COMMIT |
| Who changed which line? | git blame file |
| Find commits by message | git log --grep="nginx" |
| Changes since yesterday | git log --since=yesterday |
COMMIT is the hash from git log (the first 7 characters are enough).
Undo
| Situation | Command |
|---|---|
| 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 changes | git reset --soft HEAD~1 |
| Undo a pushed commit safely | git revert COMMIT (adds a new commit that reverses it) |
| Put changes aside for a moment | git stash, later git stash pop |
| Throw away all local changes | git reset --hard (careful: no undo) |
| Get a file back from an older commit | git restore --source=COMMIT file |
Warning: Never rewrite history that others already pulled (
reset,--amendon pushed commits,push --force). Usegit revertinstead.
.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.