The ultimate git cheat sheet (of doom)

git can be very confusing and hard to grasp. This is a small little cheat sheet with things you might need. If you need further help... ask someone who looks smart.

Table of contents

Why should I bother learning git?

Many of the most powerful tools are hard to learn - git is no different. Learning git will let you work with people more easily on large code bases, move changes over the network, organize your work, structure your team and save you from the horrible effects of hard drive failures. (It will also make you objectively cooler. <3)

Terminology

Repository/Repo

A project

Commit

A single snapshot of your files.

Staged

Mark changes as "I'm going to keep these".

Branch

Keeps track of a series of commits.

Hash

A unique identifier, often used to refer to a specific commit. Consists of the hexadecimal digits 0-F, which usually looks like "25bb8e26be86e67c235f47fc37e5". Can also be a shortened version, like "25bb8".

Remote

The repository, but on a different computer you can interact with.

Make a git repo

$ git init

# Set the URL to your upstream repo

$ git remote add origin "https://gitlab.liu.se/liuid123/reponame"

Show unstaged changes

$ git diff

Show staged changes (to be committed)

$ git diff --staged

Push changes

# Tell git what files/directories you want to keep

$ git add file dir/ *.py

# Make sure the right files are staged

$ git status

# Create the commit and describe it

$ git commit -m "Remove bug"

# Send them to someone else's computer

$ git push

Unstage all staged files (opposite of git add)

$ git reset

Throw away all unstaged changes

$ git checkout -- .

Create and switch to a new branch

$ git checkout -b descriptive-branch-name

Merge a branch

# Move to the branch you want to have both changes on, usually "master"

$ git checkout branch-to-merge-into

# Merge the other branch

$ git merge --no-ff branch-to-merge

Solve a merge conflict

# See which files have conflicts

$ git status

# Edit the files and make sure they look like you want.

# git adds diff markers (">>>>>>>>>> HEAD") to show where conflicts are

# When done, add them and commit

$ git add file dir/ *.py

$ git commit -m "Descriptive message"

Move un-commited changes to another branch

# Put the changes in the stash

$ git stash

# Check out the branch to move to

$ git checkout better-branch

# Move the changes from the stash

$ git stash apply