Categories
Git Reference

Pro Git Commands

Command Description
git add [file]
  • adds [file] to the index (stages the file).
git add -i
  • runs the interactive staging script.
git branch
  • lists branches.
git branch -a
  • lists all branches (both local and remote).
git branch [branch]
  • creates a new branch.
git branch -d [branch]
  • deletes a branch.
git branch –contains
  • shows which branches contained the named commit (HEAD if not specified).
git branch –merged
  • shows which branches are already merged into the branch you’re currently on.
git branch –no-merged
  • shows all of the branches that contain work that you haven’t yet merged in.
git branch –set-upstream-to origin/[branch]
  • sets this branch to track a remote branch. Remote branch must exist first.
git branch -v
  • shows the last commit on each branch.
git branch -vv
  • shows the tracking branches you have setup.
git cat-file -p HEAD
  • shows details (files) about the HEAD snapshot.
git checkout – <file>
  • unmodifies a modified file. Discards your changes. Reverts your file back to last commit.
git checkout [branch]
  • switches to another branch.
git checkout -b [branch]
  • creates and checks out a new branch.
git checkout -b [branch] [remote]/[branch]
  • creates and checks out a new branch, and also sets up a remote tracking branch.
git checkout –track [remote]/[branch]
  • creates and checks out a new branch, and also sets up a remote tracking branch.
git checkout –track origin/[branch]
  • creates a local tracking branch from a remote branch.
git clone <URL>-
  • clones a repository.
git commit –amend
  • adds more files to the last commit (that you still haven’t pushed).
git commit -m ‘message text’
  • commits staged files.
git config –list
  • lists your Git configuration.
git diff
  • shows changes in the working tree that are not yet staged.
git diff HEAD
  • shows changes in the working tree since your last commit.
git diff –cached
  • shows changes between the index and your last commit.
git diff –check
  • identifies possible white space errors, and lists them.
git diff –staged
  • same as git diff –cached.
git fetch [remote]
  • downloads all of the changes on the server that you don’t yet have.
git fetch –all
  • fetches all remotes.
git grep -n [search term]
  • finds the files that contain the search term. The -n switch prints the line numbers of matches.
git grep –count [search term]
  • finds the files that contain the search term, and how many times in each file.
git help <verb>
  • shows help on a particular command.
git init
  • creates a new, empty git repository.
git log
  • shows the history of commits, in revere chronological order.
git log –graph
  • shows a graphical representation (in ASCII) of your commit history (showing branches).
git log -p -2
  • generates a patch between the last two commits.
git log –pretty=oneline
  • shows commits one line at a time, pretty.
git log –since=2.weeks
  • shows the commits from the last two weeks.,
git log –stat
  • shows details for each commit.
git log [branch1]..[branch2]
  • shows what’s in branch1 that hasn’t yet been merged into branch2.
git log [remote]/[branch]..
  • shows the commits in your current branch, that aren’t in [origin]/[branch].
git log refA refB ^refC
  • shows all commits that are reachable from refA or refB, but not refC.
git log [branch1]…[branch2]
  • shows the commits that are not common to both branches.
git log -S [search term]
  • shows when the search term was introduced.
git log -L :[function name]:[file name]
  • shows the history of a function or line of code.
git ls-files -m
  • show files. In this case (-m) modified files. Also -s for staged files.
git ls-tree -r HEAD
  • lists the files in the HEAD tree, along with each one’s commit ID.
git merge @{upstream}
  • if this is a tracking branch, merges in remote tracking branch.
git merge [branch]
  • merges branch into current branch.
git merge –abort”
  • aborts the current merge.
git mergetool
  • runs a three-way merge (between local, remote, and common ancestor).
git mv <file_from> <file_to>
  • renames a file.
git pull [remote]
  • fetches and merge.
git push [remote] [branch name]
  • pushes to remote branch.
git push [remote] –delete [branch]
  • deletes a remote branch.
git push [remote] [tag name]
  • pushes a particular tag upstream.
git push [remote] –tags
  • pushes all tags.
git push –set-upstream [remote] [branch]
  • pushes the current branch, and creates the same branch on the remote, and tracks it.
git remote show [remote]
  • lists the details about a particular remote.
git remote
  • lists your remotes.
git reset <file>
  • unstages a file (essentially copies <file> from HEAD to index.
git reset [commit]
  • Moves HEAD to point to [commit].
git reset HEAD~
  • Removes the last commit. But also unstages files in the Index.
git reset –hard HEAD~
  • Removes the last commit. Cleans the index and working tree (nothing changed, nothing staged). Note: This is the dangerous version of the reset command – because you’ll lose your work in progress.
git reset –mixed HEAD~
  • Same as git reset HEAD~.
git reset –soft HEAD~
  • Undoes the last commit, without changing the working tree or the index.
git rev-parse branch
  • shows the commit that a particular branch points to.
git rm <file>
  • removes a file from the working tree and the index.
git show
  • shows details of the last commit.
git show HEAD@{n}
  • shows the commit that HEAD pointed to “n” commits ago.
git show [branch]@{yesterday}
  • shows the commit on a particular branch that HEAD pointed to yesterday.
git show HEAD@{2.months.ago}
  • shows the commit that HEAD pointed to two months ago.
git show HEAD^
  • shows the parent of HEAD.
git show d921970^2
  • shows the second parent of the specified commit. This only works for merge commits.
git show HEAD~n
  • shows the nth first parent. Equivalent to HEAD^^^ (for n = 3).
git stash
  • saves your staged and modified files on the stack.
git stash list
  • lists your stashes.
git stash apply
  • applies the last stash on your current index and working directory.
git stash apply index
  • also restages any files that are staged, but updates them with the stash.
git stash drop stash@{n}
  • deletes the specified stash.
git stash pop
  • applies a stash, and then deletes it from the stack.
git status
  • shows the status of your working tree.
git status -s
  • the short version of git status.
git tag v1.4
  • tags HEAD as v1.4.
git tag
  • lists all of the tags.

Leave a Reply