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:
- Blank lines or lines starting with # are ignored.
- Standard glob patterns work, and will be applied recursively throughout the entire working
tree. - You can start patterns with a forward slash (/) to avoid recursivity.
- You can end patterns with a forward slash (/) to specify a directory.
- You can negate a pattern by starting it with an exclamation point (!).
- An asterisk (*) matches zero or more characters
- [abc] matches any character inside the brackets (in this case a, b, or c)
- a question mark (?) matches a single character
- brackets enclosing characters separated by a hyphen ([0-9]) matches any character between them (in this case 0 through 9).
- You can use two asterisks to match nested directories; a/**/z would match a/z, a/b/z, a/b/c/z.
Miscellaneous
- @ is an alias for HEAD
- git status -sb # Easy to read status
- git push –follow-tags # Push git tags to remote.