Git Cheatsheet
If you’ve got it you’ll git it.
You’ve got to git it!
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
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/master you want to merge into
git checkout {master|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