This repository contains instructions and information on how to use github.
Git is the open source distributed version control system that facilitates GitHub activities on your laptop or desktop. This cheat sheet summarizes commonly used Git command line instructions for quick reference.
- Git schema
- Git files
- Start a new Git repository for an existing code base
- Removing folder from remote repository
- Git cheat sheet
- Useful options
- Git Setup
- Git Configuration
- Git Aliases
- Definitions
- Best practices
- Atom
Note that files are not being transfered but logs of changes (commits). This is one of the most interesting things about Git. A commit is a set of changes to the code.
- .git -> The Git repository is stored in the same directory as the project itself, in a subdirectory called .git.
- readme.md -> Markdown file that generates the html summary you see at the bottom of projects in github
- .gitignore -> Specifies intentionally untracked files to ignore such as sensible data or caches
- license -> Allows an open source license in the repository to make it easier for other people to contribute
- .gitconfig -> Located at glocal or local level, establishes global configuration options
cd /path/to/my/codebase
git init
git add .
git commit
Creates an empty .git file in current repository or reinitialize an existing one.
git init
Add all existing files to the index.
git add .
Record the pristine state as the first commit in the history.
git commit -m "First commit"
Add remote repository to local repository.
git remote add origin remote repository URL
# Sets the new remote
git remote -v
# Verifies the new remote URL
This will not remove the folder from your local repository
git rm -r --cached FolderName
git commit -m "Removed folder from repository"
git push origin master
Use .gitignore
to ignore that folder in future commits
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 an existing project and its entire version history
Review edits and craf a commit transaction
git status
lists all new or modified files to be commited
git diff
shows changes between commits
git add [file]
snapshots the file in preparation for versioning
git diff --staged
shows file differences between staging and the last file version
git reset [file]
unstages the file, but preserve its contents
git commit -m "[descriptive message]"
records file snapshots permanently in version history
git revert [commit 01] [commit 02] ...
reverts specified commits\
Name a series of commits and combine completed efforts
git branch
lists all local branches in the current repository
git branch [branch-name]
creates a new branch
git checkout [branch-name]
switches to the specified branch and updates the working directory
git merge [branch]
combines the specified branch’s history into the current branch
git branch -d [branch-name]
deletes the specified branch
Relocate and remove versioned files
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 prepares it for commit
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
Register a repository bookmark and exchange version history
git fetch [bookmark]
downloads all history from the repository bookmark
git merge [bookmark]/[branch]
combines bookmark’s branch into current local branch\
git push [alias] [branch]
uploads all local branch commits to GitHub
git pull
downloads bookmark history and incorporates changes
git-clean
removes untracked files from the working tree
git-gc
cleans up unnecessary files and optimize the local repository
git-gui
is a portable graphical interface to Git
git-merge
joins two or more development histories together
git-rm
removes files from the working tree and from the index
git-worktree
manages multiple working trees
Option | Description |
---|---|
--version | Prints the Git suite version that the git program came from. |
--help | Prints the synopsis and a list of the most commonly used commands. |
--force | Useful when repo refuses to update a remote repo. Usually due to ref. |
--hard | Clears staging area and rewrites working tree from a commit when used with reset. |
--verbose | Be a little more verbose. |
--global | Applies to all repositories. |
--local | Write/read to the repository .git/config (default) |
--stats | Show all commit logs with indications when used with log. |
--follow | Shows a file changes even across renames when used with log. |
git config --global user.name "[firstname lastname]"
sets a name that is identifiable for credit when review version history
git config --global user.email "[valid.email]"
sets an email address that will be associated with each history marker
git config --global color.ui auto
sets automatic command line coloring for GIt for easy reviewing
SSH, also known as Secure Socket Shell, is a network protocol that provides administrators with a secure way to access a remote computer.
SSH is typically used to log into a remote machine and execute commands, but it also supports tunneling, forwarding TCP ports and X11 connections; it can transfer files using the associated SSH file transfer (SFTP) or secure copy (SCP) protocols.
- Create ssh key
ssh-keygen
- Press the Enter or Return key to accept the default location.
- Enter and re-enter a passphrase when prompted.
- Add the key to github
- Add the key to the ssh-agent
eval `ssh-agent`
ssh-add ~/.ssh/<private_key_file>
- Use -k option to store git keys indefinitely
ssh-add -k ~/.ssh/id_rsa
- Switch GitHub Remote from HTTPS to SSH
- Check git ssh access
ssh -T [email protected]
- Check repository protocol currently being used. If link returned started by https then HTTPS protocol is being used for that repo
git remote -v
- Change remote url to ssh url
git remote set-url origin <copy-link-here>
--replace-all
Default behavior is to replace at most one line. This replaces all lines matching the key (and optionally the value_regex).
--add
Adds a new line to the option without altering any existing values. This is the same as providing ^$ as the value_regex in --replace-all
Git doesn’t automatically infer your command if you type it in partially.
Luckly, aliases can easily be set up for each command using git config.
git config --global alias.co checkout
git config --global alias.br branch
git config --global alias.ci commit
git config --global alias.st status
This means that, for example, instead of typing git commit, you just need to type git ci.
git config --global alias.lg "log --oneline --decorate --all --graph"
git lg
git config --global alias.s "status -s -b"
git s
Check Git configuration:
git config --global -l
Alias | Command |
---|---|
g | git |
l | log --pretty=oneline -n 20 --graph --abbrev-commit |
lg | log --oneline --decorate --all --graph |
st | status -sb |
br | branch |
patches | log -p |
cr | clone --recursive |
ci | commit |
cp | cherry-pick |
ca | !git add -A && git commit -m |
cu | !git add -u && git commit -m |
co | !f() { git checkout -b "$1" 2> /dev/null |
sl | stash list |
sa | stash apply |
ss | stash save |
tags | tag -l |
branches | branch -a |
remotes | remote -v |
amend | commit --amend --reuse-message=HEAD |
removebranches | !git branch --merged |
ds | diff --staged |
A branch is a parallel version of a repository. It is contained within the repository, but does not affect the primary or master branch allowing you to work freely without disrupting the "live" version. When you've made the changes you want to make, you can merge your branch back into the master branch to publish your changes.
A clone is a copy of a repository that lives on your computer instead of on a website's server somewhere, or the act of making that copy. With your clone you can edit the files in your preferred editor and use Git to keep track of your changes without having to be online. It is, however, connected to the remote version so that changes can be synced between the two. You can push your local changes to the remote to keep them synced when you're online.
A commit, or "revision", is an individual change to a file (or set of files). It's like when you save a file, except with Git, every time you save it creates a unique ID (a.k.a. the "SHA" or "hash") that allows you to keep record of what changes were made when and by who. Commits usually contain a commit message which is a brief description of what changes were made.
A contributor is someone who has contributed to a project by having a pull request merged but does not have collaborator access.
Fetching refers to getting the latest changes from an online repository without merging them in. Once these changes are fetched you can compare them to your local branches (the code residing on your local machine).
A fork is a personal copy of another user's repository that lives on your account. Forks allow you to freely make changes to a project without affecting the original. Forks remain attached to the original, allowing you to submit a pull request to the original's author to update with your changes. You can also keep your fork up to date by pulling in updates from the original.
Git is an open source program for tracking changes in text files, and is the core technology that GitHub, the social and user interface, is built on top of.
Issues are suggested improvements, tasks or questions related to the repository. Issues can be created by anyone (for public repositories), and are moderated by repository collaborators. Each issue contains its own discussion forum, can be labeled and assigned to a user.
Markdown is a simple semantic file format, not too dissimilar from .doc, .rtf and .txt. Markdown makes it easy for even those without a web-publishing background to write prose (including with links, lists, bullets, etc.) and have it displayed like a website. GitHub supports Markdown.
Merging takes the changes from one branch (in the same repository or from a fork), and applies them into another. This often happens as a pull request (which can be thought of as a request to merge), or via the command line. A merge can be done automatically via a pull request via the GitHub web interface if there are no conflicting changes, or can always be done via the command line.
Pull refers to when you are fetching in changes and merging them. For instance, if someone has edited the remote file you're both working on, you'll want to pull in those changes to your local copy so that it's up to date.
Pull requests are proposed changes to a repository submitted by a user and accepted or rejected by a repository's collaborators. Like issues, pull requests each have their own discussion forum.
Pushing refers to sending your committed changes to a remote repository, such as a repository hosted on GitHub. For instance, if you change something locally, you'd want to then push those changes so that others may access them.
This is the version of something that is hosted on a server, most likely GitHub. It can be connected to local clones so that changes can be synced.
A repository is the most basic element of GitHub. They're easiest to imagine as a project's folder. A repository contains all of the project files (including documentation), and stores each file's revision history. Repositories can have multiple collaborators and can be either public or private.
SSH keys are a way to identify yourself to an online server, using an encrypted message. It's as if your computer has its own unique password to another service. GitHub uses SSH keys to securely transfer information to your computer.
When talking about a branch or a fork, the primary branch on the original repository is often referred to as the "upstream", since that is the main place that other changes will come in from. The branch/fork you are working on is then called the "downstream".
Good commit messages serve at least three important purposes:
- To speed up the reviewing process.
- To help us write a good release note.
- Write the summary line and description of what you have done in the imperative mode. Start the line with "Fix", "Add", "Change" instead of "Fixed", "Added", "Changed".
- Use a capital letter at the beggining of each commit
- Don't end the summary line with a period: it's a title and titles don't end with a period.
- If it seems difficult to summarize what your commit does, it may be because it includes several logical changes.
- Always leave the second line blank.
- Avoid lazy commit messages at any cost
- Avoid whitespace changes together with code changes
- Commit early and often.
Atom is included since it is github's code editor.
Useful packages:
- atom-beautify
- autocomplete-python
- hydrogen
- markdown-writer
- markdown-preview
- html-preview