Git Cheatsheet
If you’ve got it you’ll git it.
You’ve got to git it!
Basics
It might help using Git by understanding how Git works by reading this excellent article by John Wiegley https://jwiegley.github.io/git-from-the-bottom-up
# Most used basic commands
git pull # Please do this (or a `fetch` and `merge`) regularly when working together on a repo
git diff # Check what you've changed exactly _before_ adding and committing
git status # Check which files have been modified/added since the last commit
git add . # Add _all_ modified/added files to your next commit
git commit -m "{Short but descriptive message what you've done}" # Commit. Always type a message
git push # Push your commit(s) to the remote server
# Preventing `pull`s auto merge/rebase (When working on the same files/in large teams)
git fetch
git diff origin/main # Check what you're about to merge
git merge
git stash # Stash brain farts (temporarily)
git fetch; git reset --hard origin/main # Prevent `pull` side effects (auto merge/rebase). Only after `stash` or `add` and `commit`!
Branches
# list all branches
git branch -a
# switch to (checkout) a branch
# for old git versions (before 1.6.6) use "git fetch origin"
git fetch # check for new remote branches
git checkout {branch_name}
# create a new branch and switch to it
git checkout -b {branch_name}
# push the current branch and set the remote as upstream
git push --set-upstream origin {branch_name}
# merge a branch
# first switch to the branch/main you want to merge into
git checkout {main|branch_name_to_merge_into}
git merge {branch_name_to_be_merged}
git push
# abort merging for now (for conflict avoiding personalities)
git merge --abort
# when in a feature/fix branch, instead of pulling,
# a rebase of the parent branch will prevent conflicts & merge commits.
# PR commits can then be applied cleanly to a linear history.
git status # will tell you what's up(stream)
git rebase origin/{branch_name} # instead of git pull
# delete branch remote
git push origin --delete {branch_name}
# delete branch local
git branch -d {branch_name}
# rename a branch (local & remote)
# pull first or lose all remote changes!
git checkout old_branch # Clone/checkout and pull
git pull # or lose all remote changes!
git branch -m old_branch new_branch # Rename branch locally
git push origin :old_branch # Delete the old branch
git push --set-upstream origin new_branch # Push the new branch, set local branch to track the new remote
# Cleanup remote stale branches
git remote prune origin # (They won't show up with git branch -a anymore)
Conflicts, merge hell
Accept all of their changes (when you’re already in a conflicted state):
git checkout --theirs .
git add .
Github 2FA on Ubuntu(18.04)
sudo apt-get update
sudo apt-get install libsecret-1-0 libsecret-1-dev
cd /usr/share/doc/git/contrib/credential/libsecret
sudo make
sudo git config --global credential.helper /usr/share/doc/git/contrib/credential/libsecret/git-credential-libsecret
Github 2FA in your bash/terminal
# If you're old remote broke. first remove it
git remote rm origin
git remote add origin https://[username]:[personal_access_token]@github.com/[organization]/[repo].git
References
Everything (else) you need to know about Git can be found here: https://git-scm.com/doc