GIT Cheat Sheet


July 2020

I recently started coding on two different computers, as my trusty Surface Book 2 has been joined by a Mac Mini. Among other advantages, this new setup has made me more disciplined about using GIT properly and consistently, so I can always have my code in sync between the two local environments.

The result of this newfound regimen is the following cheat-sheet.

Adding a Local Project to GIT

cd name-of-my-local-project-folder
git init
git remote add origin https://github.com/stepicker/name-of-my-project.git

Updating a Github Repo with Local Changes

git add .
git commit -m "Description of the commit"
git push origin master

Using Branches

git branch # See available branches, and which one we are on
git branch new-branch # Create a new branch
git checkout new-branch # Switch to new-branch
git add .
git commit -m "Description of the commit"
git push origin new-branch # Push new-branch to the remote repo

After that, there are two main ways to merge a branch into master.

Locally:

git checkout master # Switch to the master branch
git merge new-branch # Merge new-branch into the current branch (in this case: master)
git push origin master

On the Github web site, where I usually pick "squash & merge" after opening a pull request. After that, either one of my machines can get the latest code with:

git pull origin master

I must admit that — after experiencing some early pain with merge conflicts during my Bootcamp — I spent a few months relishing the simplicity of only using master and having full control over it.

However, as the complexity and size of my projects grew, I now take full advantage of branches to clearly identify and manage feature sets. Yes, it makes sense even on individual projects without collaborators.

© 2020 Stefano Picker