-
Notifications
You must be signed in to change notification settings - Fork 146
Tips and tricks
To enable tab-completion of git commands in the shell, consider git-completion.bash
. This script enables
completion of branch names, tags, remote names, common --long-options
, etc.
To decorate the log with branch names and to abbreviate commit hashes to seven characters, use --decorate=full
and --abbrev-commit
, respectively, or add these options to the git configuration file to enable by default:
git config --global log.abbrevCommit true
git config --global log.decorate full
To summarize one commit per line and show the commit tree:
git log --oneline --graph
To list files that have changed in each commit, along with diff statistics:
git log --stat
To show the detailed changes (patches) in each commit:
git log --patch
To include all the ignored files (from .gitignore
rules) in the status output:
git status --ignored
To highlight white space "errors" in the terminal, use a reverse diff:
git diff -R
To ignore end-of-line white space changes:
git diff --ignore-space-at-eol
To get the commit hash of the most recent common ancestor between two commit refs (hanshes, branch names, etc.):
git merge-base <ref1> <ref2>
This can be combined to view the diff since the current ref branched out:
git diff $(git merge-base <ref1> <ref2>)
To display detailed information about local and remote branches:
git branch -avv
To delete a branch regardless of uncommitted changes:
git branch -D <branchname>
Note that deleting a branch merely removes the named reference to a particular commit. Hence the commit (and its ancestors) still exists, and can be recovered by a known hash value or from git reflog
. In that sense deleting a branch is a rather safe operation. Unreachable commits do get pruned periodically, but there is normally plenty of time to recover unintentional branch deletions.
To clean untracked files and directories:
git clean --dry-run --force -d` # without --dry-run flag to remove files
To also remove ignored files, add -x
:
git clean --dry-run --force -d -x` # without --dry-run flag to remove files
To only remove ignored files, add -X
:
git clean --dry-run --force -d -X` # without --dry-run flag to remove files
Use --single-branch --branch <branchname>
to clone a single branch of a repository, for example:
git clone --single-branch --branch develop [email protected]:nrc-cnrc/EGSnrc.git EGSnrc-dev
Further restrict the amount of historical commits to include in the clone with the --depth
flag, for example:
git clone --depth=20 --branch develop [email protected]:nrc-cnrc/EGSnrc.git EGSnrc-dev
When pulling changes on a branch on which you have new local commits, use the --rebase
flag to rebase your local commits on the updated tip of the remote branch, for example:
git pull --rebase origin develop
- Overview
- Install on VirtualBox
- Install on Linux
- Install on macOS
- Install on Windows
- Quick installation
- Upgrading