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
- https://windows.github.com
- GitHub for Mac
- https://mac.github.com
- Git distributions for Linux and POSIX are available on the official Git SCM web site.
- http://git-scm.com
Configure Git
Configure user information for all local repositories using git config
git config --global user.name "[name]"
Sets the name you want on your commit transactionsgit config --global user.email "[email address]"
Sets the email you want on your commit transactionsgit 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 namegit 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 deletiongit rm --cached [file]
Removes the file from version control but preserves the file locallygit mv [file-original] [file-renamed]
Changes the file name and stages it for commitgit reset [file]
Unstages the file, but preserve its contentsgit 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 locallygit 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 branchgit pull
Downloads bookmark history and incorporates changesgit 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 directorygit branch
Lists all local branches in the current repositorygit branch [branch-name]
Creates a new branchgit branch -d [branch-name]
Deletes the specified branchgit 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 branchgit log --follow [file]
Lists version history for a file, including renamesgit diff [first-branch]...[second-branch]
Shows content differences between two branchesgit show [commit]
Outputs metadata and content changes of the specified commit