Command | Description | Example |
---|---|---|
status | See the status of files | git status |
add | Add (stage) a file to a commit (file could be a new file, or already tracked by the repository) | git add Subfolder\algorithm.cs |
commit | Commit staged files | git commit -m "Commit message" |
commit --amend | Commit staged files, amending to the previous commit | git commit --amend -m "Commit message" |
log | See the commit log. Commits are shown one page at a time, with the newest commit first. | git log |
log -L | See changes to a specific range of code, through all commits | git log -L 580,600:clsAlgorithm.cs |
log --graph | Draw a text-based graphical representation of the commit history, optionally specifying the branch to follow (master in this example) | git log --graph --decorate --pretty=oneline --abbrev-commit master git log --graph --decorate --pretty=medium--abbrev-commit master |
log --stat | Show log messages, names of updated files, and a graphical bar of the number of added and removed lines | git log --stat |
reflog | View all commits, including hidden or superseded ones | git reflog |
show | View the details of a commit | git show master git show master~1 git show 4dfbb |
show --stat --oneline | Compact view of the changes in a commit. To define an alias for git changes HASH use:git config --global alias.changes "show --stat --oneline" |
git show --stat --oneline git show --stat --oneline 82841cf1 git changes 82841cf1 |
branch -v | List branches, verbosely. Use -vv for even more info | git branch -v git branch -vv |
branch | Create a new branch (it initially points to your current location). The second example creates a new branch named bugWork back two commits from the master | git branch bugFix git branch bugWork master~2 |
branch --set-upstream-to | Set the default remote branch for the current local branch. Older versions of git use branch --set-upstream <remote-branch> |
git branch --set-upstream-to origin/master |
checkout | Switch to a branch or even a specific commit (can only switch if you have no modified or staged files). Switching to a commit that does not correspond to master or the head of a branch means that you are detaching the HEAD. | git checkout bugFix git checkout Commit_Hash |
checkout -b | Create a branch and switch to it immediately | git checkout -b bugFix |
checkout a remote branch | To checkout a remote branch you must first make a local copy of that branch, then check out the commits for the remote branch. This is because git doesn't allow you to work on someone else's branches. You can only work on your own. | git checkout -b develop origin/develop |
Ancestor checkout with ^ | Use ^ after a branch or commit hash to indicate the parent. Use ^^ to indicate the grandparent |
git checkout master^ git checkout master^^ git checkout HEAD^ |
Ancestor checkout with ~ | Use ~ after a branch or commit hash to indicate to move several commits back. |
git checkout HEAD~4 |
merge | Merge changes from another branch onto your current branch (or master) | git merge bugFix |
merge --ff-only | Merge changes only if a fast-forward is possible. Typically done to merge in changes from a development branch to master. | git checkout master git merge develop --ff-only |
merge --no-ff --no-commit | Merge changes but do not commit the changes, letting you review changes before committing. | git checkout develop git pull git checkout master git pull git merge --no-ff --no-commit develop |
rebase then merge | To assure that individual commits in a development branch appear as separate commits in the master branch, first rebase master onto develop, then switch to master and merge in the change from the develop branch | git checkout master git pull git checkout develop git pull git rebase -i master git checkout master git merge develop |
fetch | Fetch remote changes | git fetch origin |
reset | Discards changes by moving a branch reference backwards in time to an older commit (this rewrites history). Afterward, it will be as if the commit had never been made in the first place. Note that HEAD~1 is the target commit that we want to move the HEAD to. | git reset HEAD~1 |
reset (soft) | Update your files to match the remote origin. This changes the git index but does not touch local files. | git reset origin/master git reset refs/remotes/origin/master |
Undo commit | Undo the most recent commit using a soft reset. This changes the git log but will not change your local files. | git reset --soft HEAD~1 |
reset (hard) | Update your files to match the remote origin. Useful if you see message "Your branch and 'origin/master' have diverged" | git reset --hard origin/master git reset --hard refs/remotes/origin/master |
revert | Reverts changes by creating a new commit that reverses the previous commits (does not rewrite history). Note that HEAD is the commit that we want to revert. Use -n to update your working files but no create a new commit | git revert HEAD git revert -n HEAD |
revert Hash1..Hash2 | Revert changes from a range of commits. The example shown reverts commits from the fifth last commit in master (inclusive) to the third last commit in master (inclusive) | git revert -n master~5..master~2 |
rebase target | Rebasing takes a set of commits, "copies" them, and plops them down somewhere else. The example shown takes the latest commits from the bugFix branch and append them to the commit history of the master branch | git checkout bugFix git commit -m "Fix a bug" git rebase master |
rebase target source | Copy the commits at source (typically a branch) and append then to the target (typically master) | git rebase master bugFix |
Undo a simple rebase | reset , rebase and merge all save your original HEAD pointer into ORIG_HEAD, so undo any of those operations with git reset |
git reset --hard ORIG_HEAD |
Undo a complex rebase | Use git reflog to find the head commit of the branch as it was immediately before the rebase started then use git reset --hard Commit_Hash |
git reflog git reset --hard HEAD@{5} git rebase --abort |
Undo a hard reset | First use git reflog to determine the hash of the desired commit. Next issue another hard reset: git reset --hard Commit_Hash |
git reflog git reset --hard 330cf5e |
cherry-pick | Similar to rebase, but copies the specific commits to below the current HEAD (creating new commits) | git cherry-pick C2 C4 |
interactive rebase | Re-order commits and/or update commit messages (rewrites history). The following means to start rebasing after the given commit (in this case, just after the commit 4 before the HEAD). An editor is shown with the commit hashes and commit messages. Each commit is preceded by "pick".
|
git rebase -i HEAD~4 |
split a commit | Use interactive rebase to split a commit, using the "edit" keyword in the rebase commands file that appears in the text editor once the interactive rebase starts. When the edit location is reached, perform the desired commits, then git rebase --continue . |
git rebase -i HEAD~3 or git rebase -i d6fe5988 After completing the edit step, use git rebase --continue |
Undo adding a file to a commit | This command removes all traces of a committed file by searching for the file in all commits. To start at a specific commit hash, replace --all with Commit_Hash..HEAD (for example 7b3072c..HEAD ) |
git filter-branch --index-filter 'git rm --cached --ignore-unmatch path/to/fileToDelete' --tag-name-filter cat -- --all |
tag | Attach a tag (permanent marker) to a given commit (or to HEAD) | git tag Release1 Commit_Hash |
tag -d | Delete a tag | git tag -d Release1 |
describe | Describe where a commit (or HEAD) is relative to the most recent tag. The format is tag_numCommits_gHash | git describe master may report "v1_2_gC2" meaning 2 commits forward from tag v1, at hash C2git describe sideBranch may report "v2_1_gC4",meaning 1 commit forward from tag v2, at hash C4 |
rev-list | Get a listing of revisions within a range of commits (use commit hashes or relative positions). Common variants for --pretty are--pretty=oneline , --pretty=short , and --pretty=medium |
git rev-list --pretty=oneline --abbrev-commit --abbrev=7 HEAD~7...HEAD |
diff | Show changes between commits or changes from a file to a given commit | git diff HEAD clsLogger.cs to compare changes in clsLogger.cs vs. the most recent commitgit diff 9a1d7fe clsLogger.cs to compare changes in clsLogger.cs vs. an arbitrary commitgit diff 9a1d7fe 760b8e0 to compare changes between two commitsgit diff HEAD HEAD~2 to compare changes from the most recent commit to a commit two commits back |
diff --stat | List changed files between two commits, listing the number of changed lines and including histograms of the number of added and removed lines | git diff --stat HEAD~6 HEAD~10 git diff --numstat HEAD~6 HEAD~10 is like --stat but without the histogramsgit diff --shortstat HEAD~6 HEAD~10 to just show the last line from --stat, for example6 files changed, 170 insertions(+), 125 deletions(-) git diff --dirstat HEAD~6 HEAD~10 to summarize changes by subdirectory |
Last active
August 2, 2017 21:54
-
-
Save alchemistmatt/41bfbd40f3a908a8655454376c667ff6 to your computer and use it in GitHub Desktop.
Table of git commands
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment