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.

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 bundle of changes

Stage

Mark changes as "I want to keep these".

Branch

A bundle of commits (A bundle of bundles with changes)

Hash

A series of letters 0-F, usually looks like "25bb8e26be86e67c235f47fc37e5" (also come as the shortened version "25bb8e26"

Remote

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

Upstream

The "main" repository, usually on someone else's computer. (This is a form of remote, but it's your "main man", the guy you go to when everyone else leaves you.)

Make a Git repo

$ git init

# Give in the URL to your upstream repo

$ git remote set-url origin "https://gitlab.ida.liu.se/liuid123/reponame"

Show staged changes (to be committed)

$ git diff --staged

Show unstaged changes

$ git diff

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

Checkout a specific commit

# Copy the hash for the commit you want

$ git log

# Paste what you copied in the last command

$ git checkout 25bb8e26be86e67c235f47fc37e578a699c69a1e

Remove all uncommitted work

$ git reset

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, usually "master"

$ git checkout branch-to-merge-from

# Merge in the new branch

$ git checkout topic-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 ">>>>>>>>>> HEAD" to show where conflicts are

# When done simply 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