I've used a number of various Git cheatsheets over the years, usually duing a moment of "how do I do __ again?" and sometimes during a moment of panic like "Shit! Undo that commit!"
Recently, I (finally) "removed my training wheels" and uninstalled the UI client I had for Git, for two reasons:
- I was not actually using its features anymore*; and
- It was constantly getting in the way of Git updates.
* Save for the occasional git clone action
The combination of features now more or less baked into VSCode (plus a few useful extensions) and my consistent terminal (WSL or Git bash) use marked the death knell for the UI tool. The only thing I miss from the tool is a visual identifier in my Repos folder. As I have a very small number of "parent" folders containing repo clones, it's helpful to be able to see if any of them contain uncommitted changes or the like. However, in the scheme of things, this is a pretty minor "nice to have" deal.
For Future Me's sake, below are the Git commands I most frequently use or for which I forget their syntax. I have not included links to documentation or explanations of the commands.
Cloning and Branching
git clone https://github.com/username/reponame.git
Create empty branch (e.g. gh-pages
)
git checkout --orphan empty-branch
git rm -rf .
git commit --allow-empty -m "root commit"
Normal Branching
git branch branchname
git checkout [-b] branchname
git branch -al
Modifying or Renaming Branches
Add remote (e.g. point local repo to freshly-minted GitHub repo)
git remote add origin https://github.com/username/reponame.git
git remote -v
Rename branch from master
to main
(starting locally)
git branch -m master main
- Push change to GitHub
git push -u origin main
- Delete branch in GitHub, then
git fetch --prune
Switching Between SSH and HTTPS
Sometimes, depending on environments, I might have cloned something with HTTPS and need to change to SSH (for using key authentication, for example) or vice-versa:
git remote -v
git remote set-url origin git@github.com:username/reponame.git
(change to SSH)
git remote set-url origin https://github.com/username/reponame.git
(change to HTTPS)
Committing and Undoing
Undo last commit (ideally before push
):
Normal commit
& push
:
Synchronizing and Updating
git status
git push
git pull
[--prune
]
git fetch
[--prune
]
Updating Git for Windows
git update-git-for-windows
Far From Complete
This is my own personal short list and is far from complete. Most of these commands are now muscle-memory based, but having a reference to my commonly-used commands will hopefully make Future Me's life better.