Github CLI Basics - My Quick Reference Guide

Since we started using Pelican to generate the website for the non-profit I volunteer at we also started storing the code for this site in github to foster collaboration on the site. The problem arose that many of our volunteers did not know how to use git or github.

So, I figured it would be helpful to put together a quick reference guide for the most common git commands necessary for working with that website github repository.

Install Git

GitHub provides desktop clients that include a graphical user interface for the most common repository actions and an automatically updating command line edition of Git for advanced scenarios.

  • GitHub for Windows
  • GitHub for Mac
  • Git distributions for Linux and POSIX are available on the official Git SCM web site.

Configure Git

Configure user information for all local repositories using git config

  • git config --global "[name]" Sets the name you want on your commit transactions
  • git config --global "[email address]" Sets the email you want on your commit transactions
  • git config --global color.ui auto Enables helpful colorization of command line output

Create Repositories

Start a new repository or obtain one from an existing URL

  • git init [project-name] Creates a new local repository with the specified name
  • git clone [url] Downloads a project and its entire version history

Make Changes

Review edits and craft a commit transaction.

  • git status Lists all new or modified files to be committed.
  • git diff Shows file differences not yet staged.
  • git diff --staged Shows file differences between staging and the last file version.
  • git add [file|directory] Adds new files to your local repository and/or stages all changes for commit.
  • git rm [file] Deletes the file from the working directory and stages the deletion
  • git rm --cached [file] Removes the file from version control but preserves the file locally
  • git mv [file-original] [file-renamed] Changes the file name and stages it for commit
  • git reset [file] Unstages the file, but preserve its contents
  • git commit -m "[descriptive message]" Commits the tracked changes and prepares them to be pushed to a remote repository using a git push command.
  • git reset [commit] Undoes all commits after [commit], preserving changes locally
  • git reset --hard [commit] Discards all history and changes back to the specified commit

Synchronize Changes

Synchronize changes between local and remote repositories by registering a repository bookmark and exchanging version history.

  • git fetch [bookmark] Downloads all history from the repository bookmark. If no bookmark is specified it assumes the latest.
  • git merge [bookmark]/[branch] Combines bookmark’s branch into current local branch
  • git pull Downloads bookmark history and incorporates changes
  • git push [alias] [branch] Uploads all local branch commits to GitHub

Group Changes

Name a series of commits and combine completed efforts such as branching off from the main repository, merging a branch back into the main, and switching to a previous version of the repository to work with.

  • git checkout [branch-name] Switches to the specified branch and updates the working directory
  • git branch Lists all local branches in the current repository
  • git branch [branch-name] Creates a new branch
  • git branch -d [branch-name] Deletes the specified branch
  • git merge [branch] Combines the specified branch’s history into the current branch

Review History

Browse and inspect the evolution of project files.

  • git log Lists version history for the current branch
  • git log --follow [file] Lists version history for a file, including renames
  • git diff [first-branch]...[second-branch] Shows content differences between two branches
  • git show [commit] Outputs metadata and content changes of the specified commit