Git Cheat Sheet
This page provides a cheat sheet for people to use as a quick reference for the commands used in this document.
NOTE:
The cheat sheet should not be used in place of understanding why you use specific commands. Misunderstanding of the concepts described in this document can easily cause issues within the workflow.
The cheat sheet is colored as follows:
important -- Items colored in red mean they are important, and should not be ignored.
one time -- Items colored in green are commands to be issued once per machine.
repo once -- Items colored in orange are commands to be issued once per local repository.
common -- Items colored in bold black are commands that will be commonly used.
Setup Commands:
Command | Use |
git config --global user.name “First Last” | Setup the first and last name that will be used in commits |
git config --global user.email “user@domain.com” | Setup the email that will be used in commits |
git config --global core.editor ${EDITOR} | Setup the default editor for editing messages |
git config --global color.ui true | Enable coloring of output from git commands |
git config --global http.proxy http://proxy/server:port | Enable a proxy for git commands |
Repository Setup Commands:
Command | Use |
git clone protocol://path/to/repo.git | Clone a remote repository into a local repository |
git clone git@github.com:E3SM-Project/E3SM.git | Specific clone command for ACME |
Commit Commands:
Command | Use |
git add path/to/file/in/repo | Stage a new file for the next commit. This file will be tracked in future commits |
git commit | Commit all staged files |
git commit -a | Commit all changes to tracked files |
Branch Related Commands:
Command | Use |
git branch github-username/component/feature acme-vXX | Create a new branch from a tested tag named github-username/component/feature |
git branch github-username/component/bug-fix commit-with-bug | Create a new branch from a specific commit named github-username/component/bug-fix |
git checkout github-username/component/feature | Change the current working branch to github-username/component/feature |
git checkout -b github-username/component/feature master | Create a branch from master named github-username/component/feature and change the current working branch to it |
git branch | List all local branches |
git branch -r | List all remote tracking branches |
git branch -a | List all local and remote tracking branches |
git merge-base github-username/component/my-feature ACME-Climate/ACME/master | Determine the last shared commit between github-username/component/my-feature and ACME-Climate/ACME/master |
Working with remotes:
Command | Use |
git remote add remote-name protocol:address/to/repo | Create an alias “remote-name” to communicate with a remote repo at the address specified |
git remote remove remote-name | Delete an alias named “remote-name” |
git push <remote-name> <local-branch>:<remote-branch> | Push the local branch named <local-branch> (and all associated commit) to the remote named <remote-name>. :<remote-name> will rename the branch on the remote. |
git push origin feature:github-username/component/feature | Pushing a local branch named feature to the branch github-username/component/feature on the remote origin |
git fetch <remote-name> | Fetch the history from the remote named <remote-name>. Don’t store this history in any local branches |
git fetch --all -p | Fetch the history from all remotes, and prune any branches that were deleted on the remote. |
git pull <remote-name> <branch-name> | Fetch the history of <branch-name> from the remote named <remote-name> and merge it into the current working branch. This command is for advanced developers. |
History Manipulation Commands:
Command | Use |
git cherry-pick <commit-ish> | Apply a commit or a range of commits onto the current working branch |
git merge [commit-ish1] [commit-is2] … | Merge a branch or set of branches into the current working branch |
git rebase -i new-base | Apply all commits from the current working branch that do not occur in the history of new-base onto new-base This command is for advanced developers. |
git rebase -i $(git merge-base HEAD ACME-Climate/ACME/master) | Apply all commits in the current working branch onto the base of the current working branch, but allow modification. This command is for advanced developers. |
History Viewing Commands:
Command | Use |
git log --oneline --graph --decorate <commit-ish> | Display the history of <commit-ish> as a graph with one line per commit. |
git log --branches=*pattern* | Display the history of all local branches that match pattern |
git log --remotes=*pattern* | Display the history of all remote tracking branches that match pattern |
git log --first-parent <commit-ish> | Display all of the history of <commit-ish> only showing first parent commits |
git log --name-only --follow -- path/to/file | Display all of the history of a specific file, including renames. |
git log --pretty=format:"%h%x09%an%x09%ad%x09%s" | Display one line summary of commits with date. Use with ---first-parent on master to see merges to master. |