List staged files
git --no-pager diff --name-only --cached
List repo root dir
$(git rev-parse --show-toplevel)"
Hooks can either be configured locally on a per repository basis (
$GIT_DIR/hooks/*) or globally for all git repositories (
git config core.hooksPath/*). To use this hook:
- Copy it to
- Ensure it’s executable with
chmod +x .git/hooks/post-merge
A list of available hooks can be found here: https://git-scm.com/docs/githooks
#!/usr/bin/env python import sys import subprocess diff_requirements = 'git diff ORIG_HEAD HEAD --exit-code -- requirements.txt' exit_code = subprocess.call(diff_requirements.split()) if exit_code == 1: print 'The requirements file has changed! Remember to install new dependencies.' else: print 'No new dependencies.'
List git aliases
$ git config --get-regexp alias # alias.st status
In this case
git st is the same as
Show Only Commits That Touch Specific Lines
When you run
git log, you are listing all commits in reverse-chronological order for the current branch. There are ways of filtering the commits that get output from
git-log. As of Git 1.8.4,
git-log output can be filtered by commits that touch a range of line numbers.
This is done with the
For instance, if I want to see all commits that touched the 13th line of my
README.md file, then I can do this:
$ git log -L13,13:README.md
I can alter the command to show commits that touched a range of lines like so:
$ git log -L19,45:README.md
I used the
-L flag recently to find when a dependency was added to my
package.json file even though the most recent changes to that line were version bumps.
Work with a Gist locally
I like Gists, those fun-size Git repositories I fill with coding demos, scripts, and WIP markdown files. Today I learned you can edit these files locally and push them to a remote, just like any Git repo.
Grab your Gist URL:
Alter it slightly, and clone:
$ git clone email@example.com:5c61ee3fe0083.git $ cd 5c61ee3fe0083/
Make changes, commit, and push away. Your commits will show up under the
/revisions tab of your Gist.
Show Shortlog of Commiters and Their Commits
You can use
git shortlog to get a quick summary of committers and their commits on a project.
$ git shortlog Andrew Vogel (10): Initial commit Add local zshrc, vimrc, and tmux.conf Add README Remove unneeded stuffs Fix Markdown links Add installation info and image to README Update local zsh config Add local vim plugins Add lightline to vimrc Update README
Show List Of Most Recently Committed Branches
The standard way to list your branches is with the
git branch command. If you use branches extensively for feature work and bug fixes, you may find yourself overwhelmed by the list of branches trying to visually parse through them for the one that you had worked on recently.
git for-each-ref command, we can produce a better list of branches.
$ git for-each-ref --sort=-committerdate --count=10 --format='%(refname:short)' refs/heads/
The command itself will iterate over all of the repository’s refs and print them out as a list. The
--sort=-committerdate option will ensure that list is sorted by refs mostly recently committed to. The
--count=10 option limits the list output to 10 refs. The
format flag cleans up the output a bit, only showing the shortname of the ref. Lastly, the
refs/heads/ argument ensures that only local refs are included in the output, thus ignoring remote refs.
The result is a list of local branches ordered by recency which generally corresponds to relevance.
man git-for-each-ref for more details.
Include Some Stats In Your Git Log
git log command is going to give you a concise set of information for each commit. Usually it is enough info. When it’s not,
git log can provide additional information with the right flags. To include overall and per-file stats on the number of insertions and deletions, use the