git Cheatsheet

Command line basics

Developing some familiarity with the command line will come in very handy.

pwd
print the path of the directory you are in
cd PATH
change to the directory at a given PATH
cd
without a PATH cd takes you to your home directory
rm PATH
delete a file
rm -r PATH
delete a folder (the 'r' stands for recursive)
cp SOURCE DESTINATION
copy a file
cp -r SOURCE DESTINATION
copy a folder
mkdir PATH
create an empty folder
mv SOURCE DESTINATION
move a file or folder

When using paths on the command line you will come across some conventions.

.
a single dot is the current directory
..
two dots is the containing directory
~
a tilde is your home directory e.g. /home/alice (Linux etc.) or /Users/bob (Mac OS)
/
any path starting with a forward slash will be referring to the root of our entire filesystem
So given a file path of /home/alice/Desktop/code/2024-zebrafish-plots if you are in the Desktop you could move into the plots directory in many different ways:
cd /home/alice/Desktop/code/2024-zebrafish-plots
cd ~/Desktop/code/2024-zebrafish-plots
cd ./code/2024-zebrafish-plots
cd ../alice/Desktop/code/2024-zebrafish-plots
cd ./code/../code/../code/../../Desktop/code/2024-zebrafish-plots

The last is very silly on purpose to show you relative path movements.

You will come across the concept of absolute and relative file paths.

/home/alice/Desktop/code/2024-zebrafish-plots
an absolute file path as it starts at the root of the filesystem
/home/alice/Desktop/code/2024-zebrafish-plots
an absolute file path as it starts at the root of the filesystem

Setting up git

Installing git

To install git follow the official instructions for your operating system:

Basic git configuration

If you haven't used git on your computer before.

git config --global user.name "FIRSTNAME LASTNAME"
git config --global user.email "YOUR-EMAIL"

Whenever you make a commit your name and email are used for the 'author' metadata.

You can also run the following to make git command output easier to read.

git config --global color.ui auto

Using version control

Starting from scratch

Inside the folder you want to version control: git init

This will create the hidden .git directory that git uses as storage.

Cloning an existing repository

Cloning creates a copy of an existing repository on your machine. Depending on the address you use when cloning it will use a different form of authentication.

git clone git@github.com:erkannt/crop-and-rotate.git
This will use SSH key based authentication
git clone https://github.com/erkannt/crop-and-rotate.git
This will use username/password based authentication

Committing changes

The workflow is always:

  1. make your changes
  2. stage the changes you want to commit
  3. commit your changes with a message
git add .
stage all of your changes
git add PATH
stage only the changes inside PATH
git add -p
interactively stage files or parts of files
git commit -m "MESSAGE"
commit staged changes with MESSAGE
git commit
opens a text editor for you to write your commit message before committing
git restore PATH
discard changes to a specific file
git restore --staged PATH
un-stage a change (does not discard the change)
git reset --hard
discards all changes you have made since the last commit

Viewing state and history

There are a variety of ways to see what has happened in the past and what your current state is.

git status
shows you what you have changed and staged (if in doubt run this)
git log
see a history of your commits
git log --graph --oneline --decorate --branches --all
a compact view of history
git diff
see your current changes
git show
see the changes the last commit made
git show SHA
see the changes a specific commit made
(you can find the SHA by using git log)

Collaborating with others

git pull
pull changes from the remote to bring your repository up to date
git push
push your changes to the remote
git pull --rebase
useful if you and the remote both have changes
will pull the remote changes and apply yours on top
git remote -v
list the remotes configured for your local repository