Git Cheat Sheet

Branching

Creation

Creating a new branch from current location.

$ git checkout -b <branchName>

Deletion

Deleting a branch and its remote. In the last line, do not include prefixes like origin/<branchName>. Warning: do not delete the branch you have checked out!

$ git checkout master $ git branch -d <branchName> $ git push origin --delete <branchName>

Committing

Adding files

To add (or track the changes of) files. You can do it individually, but this will add all the files that have been changed.

$ git add -A

Commit

The backbone of git. Be descriptive and succinct.

$ git commit -m "<myTitle>"

If a one-line title is not enough, then one can open vim (git's default terminal editor).

$ git commit

This will likely open vim. If you do not know the hotkeys, you should hit insert which will let you start typing like normal. It should be formatted like the following.

Title for my commit My description that gives a bit more detail, or lots more!

To save your message and quit first hit esc then type :wq! and hit enter.

Merging

There are two fundamental ways to combine branches: merge and rebase. Warning: there are warnings about rebase on the internet, but they seem... rare. Be careful.

Ideal merging situation is a fast-forward merge. Essentially there is a well-defined set of steps for git to update one branch to another. The other is recursive, which requires examining three commits on the tree. This has the potential to create merge conflicts. Merge conflicts need to be dealt with and will be displayed in status.

A merge conflict changes the files conflicted. In particular, the lines of code that are affected have the following structure.

<<<<<<< HEAD Other code ======= Your code >>>>>>> <branchName>

Merge

Suppose you want to merge branch myBranch onto some other branch mainBranch. You may have to resolve merge conflicts. Afterwards, you will need to commit the resolutions.

$ git checkout mainBranch $ git fetch $ git merge myBranch

To verify you merged everything, you should see myBranch listed.

$ git checkout mainBranch $ git branch --merged

Rebase

Suppose the branch movedBranch has moved ahead of your branch myBranch. To rebase your branch from where the other one ends.

$ git checkout myBranch $ git fetch $ git rebase movedBranch myBranch

Misc.

Log

A handy tool to determine where you are located on the repo tree. Hands down most useful command here.

$ git log --graph --oneline

Stash

Made changes to files on a branch you didn't want to make changes to? Use stash. Suppose you want to take file changes from branch wrongBranch to correctBranch.

$ git stash $ git checkout correctBranch $ git stash pop

Status

A handy tool to see the status of your location, e.g. files staged for commit and how far ahead from origin/<branchName> you are.

$ git status

Updating repo

Pulling

To just download all updates to your repo.

$ git fetch

Downloading all updates and merge to your current branch. This runs fetch and merge. The default is origin <branchName>.

$ git pull

Pushing

To push all your commits to your current branch.

$ git push origin <branchName>