Git Cheat Sheet
The Git commands I look up most: remotes, history, branches and authentication.
Overview
| Task | Command |
|---|---|
| Show remotes and their URLs | git remote -v |
| History from the oldest commit | git log --reverse |
| The root (first) commit(s) | git rev-list --max-parents=0 HEAD |
| Number of commits on all branches | git rev-list --count --all |
| Delete a merged local branch | git branch -d <branch> |
| Force-delete an unmerged one | git branch -D <branch> |
| Delete a branch on the remote | git push origin --delete <branch> |
| Stop tracking a file but keep it | git rm --cached <file> (see gitignore Only Applies to Files Git Does Not Track Yet) |
Details
Use SSH Instead of HTTPS Everywhere
git config --global url."git@github.com:".insteadOf "https://github.com/"
Every GitHub HTTPS URL, including those in submodules and package managers, is rewritten to SSH, so Git authenticates with your key instead of asking for credentials.
Branch Clean-Up
Teams differ: some delete branches once merged, others keep them on the remote. Locally, git fetch --prune removes references to branches already deleted on the remote, and git branch --merged main lists those safe to delete.
Pushing to Two Remotes
Pushing one repository to GitHub and GitLab at once is covered in Mirroring Your Code to GitHub and GitLab Simultaneously.
References
- Git documentation: git config, url.<base>.insteadOf - URL rewriting.
- Git documentation: git rev-list -
--max-parentsand--count. - Git documentation: git branch -
-d,-Dand--merged. - DevOps and Infrastructure as Code MOC - hub for DevOps and Git notes.
Review cards
Flashcard How do you delete a merged local branch in Git?
Use git branch -d <branch> to delete a merged local branch.
Flashcard How do you make Git use SSH for every GitHub HTTPS URL?
git config --global url."git@github.com:".insteadOf "https://github.com/": Git rewrites the URL and authenticates with your SSH key.