GIT is a popular source control repository supporting a local + remote architecture for storing independent but linked copies of the source code. This pattern assists offline development activity, as changes can still be committed to the local store, and then later merged into a central server store, once connectivity is available.
Source (Cheat Sheet)
GIT Basics
The most common commands which are used in normal development situations.
These are worth remembering by heart
Command |
Description |
git init <directory> |
Create an empty GIT repository |
git clone <repo> |
Create a local copy of a repository |
git config user.name <name> |
Set local username (per repostory) |
git add <directory> |
Recursively add the directory and its content to the set of staged files, but do not commit |
git commit -m <message> |
Commit the staged files to the current repository branch |
git status |
Get the current status of the GIT repository, listing the staged files and list all current modified files, including tracked and untracked files |
git log |
Display complete history of all commits |
git diff |
Show unstaged changes between indexed and working directory |
GIT Branches
Commands used to create, merge and list branches
Command |
Description |
git branch |
List all branches in repository |
git fetch |
Fetches all branches from the repository. |
git fetch <branch> |
Fetches specific branch <branch> from the repository.</code> |
git checkout -b <branch> |
Create and check out a new branch named <branch>. Drop the -b flag to checkout an existing branch. |
git merge <branch> |
Merge <branch> into the current branch. |
GIT Changes
Commands used to see changes, and alter those changes to the working directory
Command |
Description |
git status |
Get the current status of the GIT repository, listing the staged files and list all current modified files, including tracked and untracked files |
git diff |
Show unstaged changes between indexed and working directory |
git revert commit |
Create new commit that undoes all of the changes made in <commit> , then apply it to the current branch. |
git reset <file> |
Remove <file> from the staged changes, without undoing changes |
git clean -n |
Lists the files in the current working directory which would be removed if a clean took place |
git clean -r |
Perform a clean on the current working directory |
GIT History
Commands to see commit history, and revise it as required
Command |
Description |
git log |
Display complete history of all commits |
git commit --amend |
Replace the last commit with the staged changes and last commit combined. When nothing is staged, this will edit the last commit message only. |
git rebase <base> |
Rebases the current branch onto <base>. In this scenario, <base> can be a commit ID, a branch name, a tag, or a relative reference to HEAD |
git reflog |
Show a log of changes to the local repository’s HEAD. Add –relative-date flag to show date info or –all to show all refs. |
GIT Remote Repositories
Commands which assist in the maintenance, creation and removal or remote repository resources
Command |
Description |
git remote add <name> <url> |
Create a new connection to a remote repository. Afterwards, <name> becomes a reusable alias to <url> |
git fetch <remote> |
Fetch a local copy all branches from <remote> |
git fetch <remote> <branch> |
Fetch a local copy of the remote <branch> from <remote> |
git pull <branch> |
Fetch a copy of the current branch from the <remote> and immediately merge it locally, but do not commit it |
git push <remote> <branch> |
Pushes all committed changes from the current branch <branch> local repository to the remote repository |
Further Detail
GIT Config
Command |
Description |
git config --global user.name <name> |
Define the author name to be used for all commits by the current user. |
git config --global user.email <email> |
Define the author email to be used for all commits by the current user. |
git config --global alias.<alias-name> <git-command> |
Create shortcut for a Git command. E.g. alias.glog log –graph –oneline will set git glog equivalent to git log –graph –oneline. |
git config --system core.editor <editor> |
Set text editor used by commands for all users on the machine. <editor> arg should be the command that launches the desired editor (e.g., vi). |
git config --global --edit |
Open the global configuration file in a text editor for manual editing. |
git config credential.helper store |
Stores the credentials to be used later, in ~/.git-credentials in the form https://<username>:<password>@github.com in clear text |
git config credential.helper 'store --file=<filepath>' |
Looks up and stores the credentials to be used later, in <filepath> in the form https://<username>:<password>@github.com in clear text |
git config credential.helper cache |
Caches the credentials to be used later, in ~/.git-credentials for 900 seconds in the form https://<username>:<password>@github.com in clear text |
git config credential.helper 'cache --timeout=<timeout>' |
Caches the credentials to be used later, for <timeout> seconds in the form https://<username>:<password>@github.com in clear text |
GIT Log
Command |
Description |
git log -<limit> |
List the last <limit> commits |
git log --oneline |
List commits in condensed one line format |
git log -p |
Display full diff with each commit |
git log --stat |
List commits, including a list of which files were modified and modified line count |
git log --author=<author> |
List all commits by a specified author |
git log --grep=<pattern> |
List all commits which contain the <pattern> in the message |
git log <from>...<to> |
List all commits, in the range <from> to <to>, where <from> and <to> may be commit ID, branch name, HEAD, tag, or revision reference |
git log -- <file> |
Only display commits to the specified file |
git log --graph --decorate |
Draw a text-based graph, and –decorate adds branch names and tags of commits |
GIT Diff
- Show changes between commits, commit and working tree, etc
- Specification
Command |
Description |
git diff HEAD |
Show difference between working directory and last commit. |
git diff --cached |
Show difference between staged changes and last commit |
GIT Reset
Command |
Description |
git reset |
Reset staging area to most recent commit, leaving working directory unchanged |
git reset -hard |
Reset staging area, overwriting all changes in the working directory |
git reset <commit> |
Move the current working directory to the <commit>, leaving the current working directory unchanged |
git reset --hard <commit> |
Move the current working directory to the <commit>, overwritting all changes in the working directory |
GIT Rebase
Command |
Description |
git rebase -i <base> |
Interactive rebase current branch onto branch, using a launched editor to enter commands for how each commit will be transferred to the new base. |
GIT Pull
- Fetch from and integrate with another repository or a local branch
- Specification
Command |
Description |
git pull --rebase <remote> |
Fetch the remote’s copy of current branch and rebases it into the local copy. Uses git rebase instead of merge to integrate the branches. |