Git cheatsheet

If you’ve got it you’ll git it.
You’ve got to git it!

# 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}

# commit your (uncommited) changes to a new branch
git checkout -b {new_branch_name}
git add {files}
git commit -m {message}
git push --set-upstream origin your-new-branch

# 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

# 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)

Accept all of their changes (when you’re already in a conflicted state):

git checkout --theirs .
git add .

Pretty drastic, you can specify filepaths instead of the dot(.)
Change --theirs with --ours to do the obvious.

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

First, create a personal access token on Github:

# If you're old remote broke. first remove it
git remote rm origin
git remote add origin https://[username]:[personal_access_token][organization]/[repo].git

Leave a Reply