Git Cheatsheet

an interaction from NDP Software

(c) Andrew Peterson 2009-2022 All Rights Reserved.

de en es fr it 한국어 pt ru vn 简中 繁中

local repository
upstream repository
Displays paths that have differences between the index file and the current HEAD commit, paths that have differences between the workspace and the index file, and paths in the workspace that are not tracked by git.
Displays the differences not added to the index.
diff commit or branch
View the changes you have in your workspace relative to the named <em>commit</em>. You can use HEAD to compare it with the latest commit, or a branch name to compare with the tip of a different branch
add file... or dir...
Adds the current content of new or modified files to the index, thus staging that content for inclusion in the next commit. Use <code>add --interactive</code> to add the modified contents in the workspace interactively to the index.
add -u
Adds the current content of modified (NOT NEW) files to the index. This is similar to what 'git commit -a' does in preparation for making a commit.
rm file(s)...
Remove a file from the workspace and the index.
mv file(s)...
Move file in the workspace and the index.
commit -a -m 'msg'
Commit all files changed since your last commit, except untracked files (ie. all files that are already listed in the index). Remove files in the index that have been removed from the workspace.
checkout files(s)... or dir
Updates the file or directory in the workspace. Does NOT switch branches.
reset HEAD file(s)...
Remove the specified files from the next commit. Resets the index but not the working tree (i.e., the changed files are preserved but not marked for commit) and reports what has not been updated.
reset --soft HEAD^
Undo the last commit, leaving changes in the index.
reset --hard
Matches the workspace and index to the local tree. WARNING: Any changes to tracked files in the working tree since commit are lost. Use this if merging has resulted in conflicts and you'd like to start over. Pass ORIG_HEAD to undo the most recent successful merge and any changes after.
checkout branch
Switches branches by updating the index and workspace to reflect the specified branch, <em>branch</em>, and updating HEAD to be <em>branch</em>.
checkout -b name of new branch
Create a branch and switch to it
merge commit or branch
Merge changes from <em>branch name</em> into current branch.<br>Use <code>&#8209;&#8209;no-commit</code> to leave changes uncommitted.
rebase upstream
Reverts all commits since the current branch diverged from <em>upstream</em>, and then re-applies them one-by-one on top of changes from the HEAD of <em>upstream</em>.
cherry-pick commit
Integrate changes in the given commit into the current branch.
revert commit
Reverse commit specified by <em>commit</em> and commit the result. This requires your working tree to be clean (no modifications from the HEAD commit).
diff --cached commit
View the changes you staged vs the latest commit. Can pass a <em>commit</em> to see changes relative to it.
commit -m 'msg'
Stores the current contents of the index in a new commit along with a log message from the user describing the changes.
commit --amend
Modify the last commit with the current index changes.
Show recent commits, most recent on top. Options:<br><code>&#8209;&#8209;decorate</code> with branch and tag names on appropriate commits<br><code>&#8209;&#8209;stat</code> with stats (files changed, insertions, and deletions) <br><code>&#8209;&#8209;author=<em>author</em></code> only by a certain author<br><code>&#8209;&#8209;after="MMM DD YYYY"</code> ex. ("Jun 20 2008") only commits after a certain date<br><code>&#8209;&#8209;before="MMM DD YYYY"</code> only commits that occur before a certain date <br><code>&#8209;&#8209;merge</code> only the commits involved in the current merge conflicts
diff commit commit
View the changes between two arbitrary commits
List all existing branches. Option -r causes the remote-tracking branches to be listed, and option -a shows both.
branch -d branch
Delete an specified branch. Use -D to force.
branch --track new remote/branch
Create a new local branch that tracks a remote branch.
clone repo
Download the repository specified by <em>repo</em> and checkout HEAD of the master branch.
pull remote refspec
Incorporates changes from a remote repository into the current branch. In its default mode, <code>git pull</code> is shorthand for <code>git fetch</code> followed by <code>git merge FETCH_HEAD</code>.
reset --hard remote/branch
Reset local repo and working tree to match a remote branch. Use <code>reset &#8209;&#8209;hard origin/master</code> to throw away all commits to the local master branch. Use this to start over on a failed merge.
fetch remote refspec
Download objects and refs from another repository.
update the server with your commits across all branches that are *COMMON* between your local copy and the server.Local branches that were never pushed to the server in the first place are not shared
push remote branch
Push new (or existing) branch to remote repository
push remote branch:branch
Push new branch to remote repository with a different name
branch -r
List remote branches
push remote :branch
Remove a remote branch. Literally &quot;push nothing to this branch&quot;
Cleans the working tree by recursively removing files that are not under version control, starting from the current directory.
stash save msg
Save your local modifications to a new stash, and run git reset &#8209;&#8209;hard to revert them. The <em>msg</em> part is optional and gives the description along with the stashed state. For quickly making a snapshot, you can omit both "save" and <em>msg</em>.
stash apply stash
Move changes from the specified stash into the workspace. The latest stash is the default.
stash pop
Applies the changes from the last (or specified) stash and then removes the given stash.
stash list
List the stashes that you currently have.
stash show stash
Show the changes recorded in the stash as a diff between the stashed state and its original parent. When no <em>stash</em> is given, shows the latest one.
stash drop stash
Remove a single stashed state from the stash list. When no <em>stash</em> is given, it removes the latest one.
stash clear
Remove all the stashed states. Note that those states will then be subject to pruning, and may be impossible to recover.
stash branch branchname stash
Creates and checks out a new branch named <em>branchname</em> starting from the commit at which the <em>stash</em> was originally created, applies the changes recorded in <em>stash</em> to the new working tree and index. <br>If that succeeds, and <em>stash</em> is a reference of the form stash@{<em>revision</em>}, it then drops the <em>stash</em>. When no <em>stash</em> is given, applies the latest one. <br>This is useful if the branch on which you ran git stash save has changed enough that git stash apply fails due to conflicts. Since the stash is applied on top of the commit that was HEAD at the time git stash was run, it restores the originally stashed state with no conflicts.