Skip to content

Instantly share code, notes, and snippets.

@unrivaledcreations
Last active February 28, 2022 19:17
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save unrivaledcreations/85932a1b90afc127bea2905124794716 to your computer and use it in GitHub Desktop.
Save unrivaledcreations/85932a1b90afc127bea2905124794716 to your computer and use it in GitHub Desktop.
Git Cookbook: Reference cheat sheet of my most-used, most-needed Git commands, organized into categories: Initial setup tasks, repository management, branches, tags, and stashes.

Git Cookbook

Initial Setup Items

Task Command
Initial git setup git config --global user.name {your name}
git config --global user.email {your email}
git config --global core.fileMode false
git config --global push.default simple
echo .DS_Store >> ~/.gitignore_global
git config --global core.excludesfile ~/.gitignore_global
ssh create key ssh-keygen -t rsa
ssh list keys ssh-add -l
ssh convert to .pem openssl rsa -in ~/.ssh/id_rsa -outform pem > id_rsa.pem
chmod 600 id_rsa.pem
ssh convert public to .pem ssh-keygen -f ~/.ssh/id_rsa.pub -m 'PEM' -e > public.pem
chmod 600 public.pem
add ssh key ssh-add ~/.ssh/id_rsa
ssh remove keys ssh-add -d ~/.ssh/id_notused_rsa
RSA fingerprint ssh-keygen {-E md5} -lf id_rsa.pub

File and Repository Management

Task Command
create repository git init
add to repository git add {file}
is it tracked? git ls-files {name}
commit changes git commit -a -m '{message}'
remove one change git reset HEAD {name}
remove ALL changes git reset --hard HEAD
remove after commit git checkout -- {file}
revert git revert --no-commit {hash}..HEAD (e.g., a1b2c3d4)
git revert -continue -or- git revert --abort
remove/delete file(s) from repository git rm --cached {name}
`find . -name .DS_Store -print0
add local to remote 1. git remote add origin
git@bitbucket.org:unrivaledmhall/{project}.git
2. git push --set-upstream origin master
3. git push -u origin --all
4. git push origin --tags
change origin (rename) git remote set-url origin {url}
really clone repository mkdir repo
cd repo
git clone --bare {path/to/repo.git} .git
git config --unset core.bare -or- git config --bool core.bare false
git reset --hard
ignore filemode git config {--global} core.fileMode false
refresh submodules git submodule update --init –recursive -or- git clone --recursive git://repo.git

Code Maintenance: Branches, Tags, and Stashes

Branches

Task Command
add a branch git checkout -b {branch name}
-or-
git branch {branch name}
git checkout {branch name}
-then-
git push --set-upstream origin {branch name}
change active branch Same as above (if creating a branch)
-or-
git checkout {name}
list branches git branch
git ls-remote --heads {remote-name}
list tracked files git ls-tree -r HEAD --name-only
show changes git status -uno
view log (history) git log --oneline --decorate
merge with master
(aka "fast forward")
git checkout master
git merge {branch name}
delete branch git branch -d {branch name}
git push origin --delete {branch name}
rename git branch git checkout {old name}
git branch -m {new name}
git push

Tags

Task Command
add tag git tag -a {tag name} [commit number] -m '{tag annotation}'
remove tag git tag --delete {tag name}
git push --delete origin {tag name}
push tag git push origin {tag name}
remove tag git tag -d {tag name}
add tag, remote git push origin {tag name}
remove tag, remote git push origin --delete {tag name}

Stashes

Task Command
"stash" working state git stash
"un-stash" working state + remove git stash pop
show stashes git stash list
"un-stash" working state git stash apply {stash name} --index (e.g., stash@{2})
remove a stashed state git stash drop stash@{0}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment