Show Menu
Cheatography

git-cheat-sheet Cheat Sheet (DRAFT) by

Cheat sheet for git commands

This is a draft cheat sheet. It is a work in progress and is not finished yet.

Most Frequently Used Commands

git clone <ur­l>
Downloads a copy of a remote reposi­tory. Remember to use the Kerberos (KB5) URL from Gitlab.
git add [file name]
Add a modified file to the staging area. You can also add an entire directory by using the directory name instead.
git commit -m "­[commit messag­e]"
Commits all staged files to the repository with a commit message. If you accide­ntally run "git commit­" without a message, it will likely open Vim. Press "i' to enter insert mode, write a commit message, hit escape, then type":w­q" and hit enter to save the message.
git branch [branch name]
Create a new branch. This will not switch the current branch to the newly created branch. Use "git checkout [branch name]" to switch the current branch.
git checkout [branch name]
Switch the currently active branch to [branch name]. Note that "git checko­ut" does many different things depending on the exact arguments you pass to it. Switching branches is the primary use case.
git push -u [remote repo] [branch name]
Upload the current branch to the remote repo. Every time you make a new branch, you should run this command with the "­-u" option. This sets up a default so git will understand which branch on the remote repo is supposed to track the current branch in the local repo. After running this the first time, you can simply run "git push" with no other arguments.
git fetch
Download all files from the remote repo. This does not actually change your working directory, it just stores a copy of the remote repository in your .git folder.
git merge
With no arguments, merge will update your local repository with all the changes that were most recently fetched from the remote reposi­tory. This will change your working directory.
git pull
In theory, this is the same thing as running "git fetch" and then running "git merge". Most of the time, you will be running "git pull", but occasi­onally you will need to fetch and merge manually.
git merge [branch name]
Updates the current branch with all the changes commited in [branch name].
git status
Lists the current state of the working directory, staging area, and the reposi­tory. I would recommend running this very freque­ntly. You should at least run this before you commit any modifi­cat­ions.
 

Glossary

Working directory (A.K.A. "­working tree")
The directory you are currently working in. This will have all the files you are working with, regardless of their status according to git. This is not a git speciifc concept.
Staging area
This is where git stores the changes you have added but have not yet commited. You can think of this as a space thats "­inb­etw­een­" the working directory and the repository
Repository (A.K.A. "­rep­o")
This is where git stores all of your commits. You can think of this as the hidden ".gi­t" directory. Your local repository is what you will be working with directly. Its the repository on your computer.
Remote repository
A git repository that tracks the same project but exists in some other location. Typically, the Gitlab repo is the only remote repo you will be pushing to. But everyone else involved in the project will probably have their own repo that could be considered "­rem­ote­" in some sense. Every remote repo is given a name stored in your local repo. By conven­tion, the original repository you cloned from will be given the name "­ori­gin."
Commit
As a noun: A snapshot or version of your project. A point in .git history.
As a verb: to create a new commit by using the "git commit­" command.
head (lower­case)
The most recent commit of a branch.
HEAD (upper­case)
The head of the current branch.

.gitignore Syntax

Pattern
Explan­ation
# text comment
Comments in the .gitignore file that are ignored. Useful for docume­ntation
name.file
A specific file to be ignored. You can specify a path to a subdir­ectory with "­/"
*end_o­f_n­ame.file
Ignore every file that ends in "­end­_of­_na­me.f­il­e"
start_­of_­name*
Ignore every file that starts with "­sta­rt_­of_­nam­e"
direct­oy_­name/*
Ignore every file in the "­dir­ect­ory­_na­me" folder