You are on page 1of 2

Git Cheat Sheet

Git Basics Rewriting Git History


git init Create empty Git repo in specied directory. Run with no arguments to Replace the last commit with the staged changes and last commit
git commit --amend
<directory> initialize the current directory as a git repository. combined. Use with nothing staged to edit the last commits message.

Clone repo located at <repo> onto local machine. Original repo can be git rebase <base> Rebase the current branch onto <base>. <base> can be a commit
git clone <repo>
located on the local lesystem or on a remote machine via HTTP or SSH. ID, a branch name, a tag, or a relative reference to HEAD.
+
git config Dene author name to be used for all commits in current repo. Devs Show a log of changes to the local repository's HEAD. Add --relative-
git reflog
user.name <name> commonly use --global ag to set cong options for current user. + date ag to show date info or --all to show all refs.

git add Stage all changes in <directory> for the next commit. Replace <directory>
with a <le> to change a specic le.
Git Branches
<directory>

git commit -m Commit the staged snapshot, but instead of launching a text editor, use List all of the branches in your repo. Add a <branch> argument to
git branch
"<message>" <message> as the commit message. create a new branch with the name <branch>.
git checkout -b Create and check out a new branch named <branch>. Drop the -b
git status List which les are staged, unstaged, and untracked.
<branch> ag to checkout an existing branch.
Display the entire commit history using the default format. For
git log
customization see additional options. + git merge <branch> Merge <branch> into the current branch.

git diff Show unstaged changes between your index and working
Remote Repositories +
directory +
git remote add Create a new connection to a remote repo. After adding a remote, you
Undoing Changes <name> <url> can use <name> as a shortcut for <url> in other commands.

git revert Create new commit that undoes all of the changes made in git fetch Fetches a specic <branch>, from the repo. Leave off <branch> to
<commit> <commit>, then apply it to the current branch. <remote> <branch> fetch all remote refs.

Remove <le> from the staging area, but leave the working directory git pull <remote> Fetch the specied remotes copy of current branch and immediately
git reset <file>
unchanged. This unstages a le without overwriting any changes. + merge it into the local copy. +
Shows which les would be removed from working directory. Use the -f git push <remote> Push the branch to <remote>, along with necessary commits and
git clean -n
ag in place of the -n ag to execute the clean. <branch> objects. Creates named branch in the remote repo if it doesnt exist. +

Visit atlassian.com/git for more information, training, and tutorials page 1


Additional Options +
git config git diff
git config --global
user.name <name> Dene the author name to be used for all commits by the current user. git diff HEAD Show difference between working directory and last commit.

git config --global


user.email <email>
Dene the author email to be used for all commits by the current user. git diff --cached Show difference between staged changes and last commit.

git config --global


Create shortcut for a Git command. E.g. alias.glog "log --graph
alias.<alias-name>
--oneline" will set "git glog" equivalent to "git log --graph --oneline" git reset
<git-command>
git config --system Set text editor used by commands for all users on the machine. <editor>
core.editor git reset Reset staging area to match most recent commit, but leave the working
<editor>
arg should be the command that launches the desired editor (e.g., vi). directory unchanged.
git config --global
Open the global conguration le in a text editor for manual editing. Reset staging area and working directory to match most recent commit
git reset --hard
--edit and overwrites all changes in the working directory.

Move the current branch tip backward to <commit>, reset the staging
git log git reset <commit>
area to match, but leave the working directory alone.

git log -<limit> Limit number of commits by <limit> . E.g. "git log -5" will limit to 5 git reset --hard Same as previous, but resets both the staging area & working directory to
commits <commit> match. Deletes uncommitted changes, and all commits after <commit>.

git log --oneline Condense each commit to a single line. git rebase

git log --stat Include which les were altered and the relative number of lines that git rebase -i Interactively rebase current branch onto <base>. Launches editor to enter
were added or deleted from each of them. <base> commands for how each commit will be transferred to the new base.

git log -p Display the full diff of each commit. git pull
git log git pull --rebase Fetch the remotes copy of current branch and rebases it into the local
--author="<pattern>" Search for commits by a particular author. <remote> copy. Uses git rebase instead of merge to integrate the branches.
git log
--grep="<pattern>"
Search for commits with a commit message that matches git push
<pattern>.
git log Show commits that occur between <since> and <until>. Args can be a git push <remote> Forces the git push even if it results in a non-fast-forward merge. Do not
<since>..<until> commit ID, branch name, HEAD, or any other kind of revision reference. --force use the --force ag unless youre absolutely sure you know what youre doing.

git log -- <file> git push <remote>


Only display commits that have the specied le. Push all of your local branches to the specied remote.
--all

git log --graph Tags arent automatically pushed when you push a branch or use the
--graph ag draws a text based graph of commits on left side of commit git push <remote>
--decorate --tags
msgs. --decorate adds names of branches or tags of commits shown. --all ag. The --tags ag sends all of your local tags to the remote repo.

Visit atlassian.com/git for more information, training, and tutorials page 2

You might also like