How to use Git and GitHub

You can find the corresponding udacity course here.

Table of Contents

  1. Command-line basics
  2. Git
  3. GitHub

Command-line basics

Command What does it? Did you know?
mkdir name Create new folder “name”
subl filename.txt Create new file, opened with sublime
pwd Shows what directory you are in pwd = print working directory
ls List the files in this directory

Git

There exist 3 locations for a file:

Octopus = strategy Git uses to combine many different versions together.

How often to commit?

Basic commands

Command What does it?
git log Show the log of the current branch
git log --oneline Show short log for the current branch
git log --stat Show the log with changed files log
git log -p 4a60beb Start to show changes at specific log
git show 4a60beb Show only one commit
git diff file1 file2 Compare two versions of a file line by line

All file content in 1 line will always diff ⇒ keep lines short! (max. length of 80 - 120 characters)

Command What does it?
git checkout versionId Go to a specific commit version
git status Show all files changed since the last commit

Run git status frequently!

Command What does it?
git init Make a repository out of a folder
git add Add files to staging area
git commit -m “Message” Bring files from staging area to the repository
git diff --staged Show diff between staging area and repository
git diff Show delta between working directory and staging area
git reset --hard Delete all changes in working directory and staging area. Use carefully
git tag -a v1.0 a87984 Create annotated tag at commit a87984. Always use annotated tags!
git tag v1.0 Create lightweight tag, that have no extra informations.
git log --decorate Show log with tags

Commit message structure

type: Subject

body

footer

The type can be one of these

Type Description
feat: a new feature
fix: a bug fix
docs: changes to documentation
style: formatting, missing semi colons, etc.: no code change
refactor: refactoring production code
test: adding tests, refactoring tests; no production code change
chore: updating build tasks, package manager configs, etc; no production code here

Subject < 50 characters

It has to start with a capital letter and no period at the end. Use imperative tone to describe: “Change” not “changed” or “changes”.

Body (optional)

Explain the what and why of a commit. Don’t explain the how! The length of each line should be < 72 characters.

Reference issue tracker IDs here. Ex.:

Resolves: #123
See also: #456, #789

Branches

Command What does it?
git branch Show all branches
git branch name-of-branch Create branch
git log --graph --oneline branchname1 branchname2 Generate diagram = Show log of 2 branches graphically
git log --oneline --decorate --graph --all Show all branches and commits in the repository

Use always - instead of spaces in branch names.

Every commit (o) only knows his precursor (←o←o). Means that if one commit is lost, you will lose all the commits that have been before this one.

Command What does it?
git checkout -b new-branch-name Corresponds to git branch new-branch-name and git checkout new-branch-name
git merge master coins Merge coins branch into master branch

Always checkout one of the two branches you are planning on merging before doing the merge.

Command What does it?
git merge --abort Restore file to state before start of merge
git merge coins Merge branch coins into active branch

git merge master coins and git merge coins are equal if you are on master branch.

Command What does it?
git branch -d coins Delete branch “coins”
git log -n 1 Give the log a fix number of commits. Here 1 commit.

GitHub

Command What does it?
git fetch Update local copy (working directory)
git push Bring staging area to the repository
git pull git fetch and then git merge
git remote add origin link@website Create remote
git remote -v Show remotes with url for fetch and push

Forking is a fair and better way to start an own work based on an existing repository. It is a clone from GitHub to GitHub. Fork the repository and then clone your fork and start working.

Command What does it?
git clone link@website Clone a repository

Parto Karwat
A pull request is a GitHub specific request to pull a branch into the master branch. If it is your repository and you got the pull request, then you can use the button merge pull request to complete.

Written with in Kiel.