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 (< 1.6.6) use &amp;amp;quot;git fetch origin&amp;amp;quot;
$ git fetch
$ git checkout &amp;amp;amp;amp;amp;amp;amp;amp;lt;branch_name&amp;amp;amp;amp;amp;amp;amp;amp;gt;

# create a new branch and switch to it
$ git checkout -b &amp;amp;amp;amp;amp;amp;amp;amp;lt;branch_name&amp;amp;amp;amp;amp;amp;amp;amp;gt;
# push the current branch and set the remote as upstream
$ git push --set-upstream origin &amp;amp;amp;amp;amp;amp;amp;amp;lt;branch_name&amp;amp;amp;amp;amp;amp;amp;amp;gt;

# merge a branch
# first switch to the branch/master you want to merge into
$ git checkout &amp;amp;amp;amp;amp;amp;amp;amp;lt;master|branch_name_to_merge_into&amp;amp;amp;amp;amp;amp;amp;amp;gt;
$ git merge &amp;amp;amp;amp;amp;amp;amp;amp;lt;branch_name_to_be_merged&amp;amp;amp;amp;amp;amp;amp;amp;gt;
$ git push

# abort merging for now (for conflict avoiding personalities)
$ git merge --abort

# delete branch remote
$ git push origin --delete &amp;amp;amp;amp;amp;amp;amp;amp;lt;branch_name&amp;amp;amp;amp;amp;amp;amp;amp;gt;
# delete branch local
$ git branch -d &amp;amp;amp;amp;amp;amp;amp;amp;lt;branch_name&amp;amp;amp;amp;amp;amp;amp;amp;gt;

# rename a branch (local &amp;amp;amp;amp;amp;amp;amp;amp;amp; 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: https://help.github.com/en/articles/creating-a-personal-access-token-for-the-command-line

# 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
# Parent-project &amp;amp;quot;parent-repo&amp;amp;quot; imports a seperate library/repo &amp;amp;quot;my-submodule&amp;amp;quot;
$ git remote add my-submodule git@github.com:gijsweer/my-submodule.git
$ git subtree add —-prefix=vendor/ my-submodule master

Leave a Reply