Github for Web Designers

Git Quick Reference

As I was learning Git one of the things that frustrated me the most was how references and tutorials would either assume you would know exactly what a command meant or would simply show you a command to enter without explaining what it was doing. Because of this I wanted it to create a quick reference for common Git commands. This list is by no means exhaustive, but it should provide you with a solid reference to the most common commands you encounter.

Installation

Helpful installation links. I recommend installing the Github Client. Not only does it give you an intuitive GUI interface for common Git tasks, it also does a stable installation of Git as well. For Windows it installs a shell emulator that gives you access to the command line. On the Mac be sure to choose to install the Command Line tools as part of the client install. Without them some path references might not resolve.

Git installer for Mac

Git installer for Windows

Gitub Client for Mac

Github Client for Windows

Setting up Git

Useful config options

In addition to the global configuration options that Github provides in its getting started article there are some very useful configuration options that you might be interested in.

Config help
git config --help

Will list available config options.

Setting the default editor
git config --global core.editor <tool>

If you want to use a different editor than Vim or Vi you can set that by adding your tool of choice here.

Global ignore file

git config --global core.excludesfile ~/.gitignore_global

.gitignore files can help you decide which files are tracked or not in local repositories. However if you ever want files to be ignored globally across all repositories you can use this command. Just create a plain text file, name it what you’d like and point to it after the core.excludesfile command. Here the example “.gitignore_global” is used. Github has a link to common configurations that can help get you started.

Setting aliases

git config --global alias.ci commit
git config --global alias.st status
git config --global alias.co checkout
git config --global alias.br branch
git config --global alias.mr merge

If you want to save some time typing, you can create aliases for common commands. After setting these typing in the alias is the same as typing in the command itself. Above is a list of common aliases although you’re free to create your own. Simply add the alias you want after the “alias.” command and then type the command it represents.

Starting a project

Clone

Copies a repository into a new directory. Usually done to copy a remote directory locally. Unlike fetch, clone copies an entire repository and all its branches, and then sets up tracking for local and remote branches.

git clone [url]

Where URL points to the remote repository

git clone -o [url] <name>

Clones a remote repo and changes the named reference from “origin” to the one you specify.

git clone -b [url]  <branch>

Clones the repo and checks out the named branch locally.

Init

Initializes a new Git repository.

git init

When run the current directory is initialized as a Git repository.

Staging and committing

Status

Gives the current status of a repository and its files.

git status

Displays the current state of the repository

git status -s

Displays the output in short format.

git status -b

Displays the branch and tracking info in short format.

git status -u

Displays untracked files.

Add

Stages files to be committed.

git add <path to file>

Add the individual file. Additional files may be added by using multiple filenames separated by a single whitespace.

git add <directory name>

Will add all files in the directory.

git add .

Adds all untracked and modified files, even those in subdirectories.

git add *.txt

Wildcards can be used to add all files of a specific type, in this instance text files.

git add -f <filename> 

Force, will add the file specified even if it's contained in a .gitignore file

git add -u <filename>

Updates a previously staged instance of the file that hasn't yet been committed.

Commit

Creates a snapshot of the current stage and adds it to the projects history.

git commit

Creates a commit and opens text editor for the commit message.

git commit -m “message”

Creates a commit and bypasses the text editor by supplying a short form message.

git commit -a

Stage and commit all modified and removed files. Does not affect untracked files.

git commit --amend

Allows you to add staged files to a previous commit. Helpful if you forget to add a file to the previous commit and don’t want to add another commit to the history.

Reset

Allows you to revert to earlier commits. How reset works is altered dramatically by the parameters you pass to it. Since space here doesn’t allow for a full discussion of that, please see Scott Chacon’s article on reset on the Git blog.

Diff

Shows changes between commits

git diff

Will compare the changes in modified files with the last commit. Will not show differences between working directory and staged files.

git diff --staged

Shows the differences between staged files and the last commit.

Branching & Merging

Branch

Allows you to list existing branches, create new branches, or delete existing branches

git branch

Displays a list of existing branches and indicates the current branch.

git branch <name>

Creates a new branch with the specified name.

git checkout -b <name>

Creates a new branch with the specified name and checks it out so that it is the current branch.

git branch -d <name>

Deletes branch unless the branch has never been merged.

git branch -D <name>

Deletes the branch regardless of its merged status.

git branch -r

List remote branches.

git branch --merged

Only list branches that have been merged.

git branch --no-merged

Only list branches that have not been merged.

Checkout

Allows you to switch between branches and load earlier files and commits

git checkout <branchname>

Switches to the specified branch.

git checkout <commit>

Detaches the commit and sets to working directory to that snapshot. Changes may be made and then committed, thereby resetting files to an earlier state.

git checkout <branch>~[n] 

Reverts the current commit for that branch to the number specified in [n]. git checkout master~3, for example would reset the index to three commits back for the master branch. You can add specific filenames or directories to the end of this to reset specific files. This detaches the files so they must be recommitted in order to be added to the commit history.

Merge

Joins two or more commits together into a single commit.

git merge <branchname>

Merges the specified branch into the current branch. Additional branches can be added as a series of space separated branch names.

git merge --no-commit <branchname>

Merges the specified branches but does not commit the result. This allows you to inspect the merge before you finalize it by committing.

Log

Show commit history

git log

List commit history

git log --no-merges

List commit history but ignore merges.

git log --pretty=oneline

List commit history using short hash identifier and a single line message.

git log <file>

See all commits that affect the specified file.

Reverting & Resetting

Checkout

Allows you to switch load earlier files and commits

git checkout <commit>

Detaches the commit and sets to working directory to that snapshot. Changes may be made and then committed, thereby resetting files to an earlier state.

git checkout <branch>~[n] 

Reverts the current commit for that branch to the number specified in [n]. git checkout master~3, for example would reset the index to three commits back for the master branch. You can add specific filenames or directories to the end of this to reset specific files. This detaches the files so they must be recommitted in order to be added to the commit history.

Reset

Allows you to revert to earlier commits. How reset works is altered dramatically by the parameters you pass to it. Since space here doesn’t allow for a full discussion of that, please see Scott Chacon’s article on reset on the Git blog.

Rebase

Flattens commit histories into a linear sequence

git rebase <commit>

Moves the current branch to the specified commit. The practical result of this is that it creates a linear commit history from multiple branches. Commits within a branch are moved to the end of the current branch. This is typically followed up by a merge which moves the current commit forward to the last added commit.

Working with remote repos

Remote

Manges remote repositories

git remote

Lists the currently tracked remote repos

git remote add [name] [url]

Adds new remote repos. The name establishes an alias for the repo, while the URL establishes the location.

git remote -rm [name]

Removes specified remote repo.

Fetch

Updates local repositories from remote repos. These changes are not merged by default and must be merged manually. This allows you to pull down remote changes, inspect them, and then choose which changes to update locally.

git fetch

Will fetch from the remote repository tracked to the current branch.

git fetch origin

Copies all branches from the remote repo and stores them locally.

Pull

Similar to fetch except for the fact that remote objects are both downloaded and then merged into the local repository. This has the effect of automatically updating the local repository to match the remote, while fetch will download the files but not update your local copies.

git pull

Pulls the current remote repository and merges it.

git pull origin <branchname>

Pulls the remote branch specified and merges it.

Clone

Copies a repository into a new directory. Usually done to copy a remote directory locally. Unlike fetch, clone copies an entire repository and all its branches, and then sets up tracking for local and remote branches.

git clone [url]

Where URL points to the remote repository

git clone -o [url] <name>

Clones a remote repo and changes the named reference from “origin” to the one you specify.

git clone -b [url]  <branch>

Clones the repo and checks out the named branch locally.

Push

Updates remote repos with local content.

git push

Pushes the current branch to the remote repository.

git-push [remote] [branch]

Pushes the contents of the specified branch to the remote’s version of that branch.

git push [remote] :[branch]
Deletes the specified branch on the remote server.