Git Command Reference

Git Config

# Set name and email you want attached to your commit transactions
git config --global user.name "[name]"
git config --global user.email "[email address]"

# Enables helpful colorization of command line output
git config --global color.ui auto

# Configure kdiff3 as the merge tool on Windows.
git config --global merge.tool kdiff3
git config --global mergetool.kdiff3.path "C:/Program Files/KDiff3/kdiff3.exe"
git config --global mergetool.kdiff3.trustExitCode false

Git Branch

git branch -d localBranchName               # Delete branch locally
git push origin --delete remoteBranchName   # Delete branch remotely
git remote prune origin                     # Delete all local branches not on remote
git push --set-upstream origin BRANCH_NAME  # Push local branch to remote; will create a pull request.
git branch --sort=-committerdate            # Print branches ordered by the commit dates.
git branch -vv                              # Show the last local and remote commit on each branch.

Git Checkout

git checkout @{-N}  # Go back N branches.
git checkout -      # Go to the last branch (shorthand to  for the previous command)

Git Diff

git diff                                   # changed but not staged.
git diff --staged                          # staged and ignore local changes.
git diff --cached                          # same as staged.
git difftool                               # use external diff tool.
git diff $startCommit..$endCommit -- FILE  # Compare changes in one file from startCommit to endCommit.
git diff $startCommit..$endCommit          # Compare two commits.

Git Commit

git commit                  # open external editor for commit message
git commit -m "message"
git commit -a -m "message"  # ignore staging and commit all modified files
git commit --amend          # Fix last commit message.

# Add a file missed in the last commit.
git add missed_file.txt
git commit --amend

# Remove a file after committing:
git reset --soft HEAD~1
git reset accidently_added_file.jpg
rm accidently_added_file.jpg
git commit

Git Stash

git stash        # Store the current changes in stash.
git stash pop    # Apply the stashed changes and then drop it.
git stash drop   # Remove the stashed changes.
git stash apply  # Apply the stashed changes and leave them in the stash.

Git Log

git log
git log -p -2 (--patch)
git log --stat
git log --pretty=oneline
git log --pretty=format:"%h - %an, %ar : %s"

Git Reset

git reset --hard HEAD        # Reset all files to the HEAD of the branch:
git checkout HEAD -- FILE    # reset a single file
git reset --soft HEAD~1      # Revert committed changes.

Git Ignore Patterns

The rules for the patterns you can put in the .gitignore file are as follows:

Miscellaneous


No comments yet

Write a comment, your thoughts are welcome: