Skip to content

Instantly share code, notes, and snippets.

@gregsexton
Last active August 29, 2015 14:15
Show Gist options
  • Save gregsexton/c4ce7ba0823b560a6e79 to your computer and use it in GitHub Desktop.
Save gregsexton/c4ce7ba0823b560a6e79 to your computer and use it in GitHub Desktop.
git-tldr
#!/usr/bin/env bash
if ! git rev-parse --git-dir > /dev/null 2>&1; then
echo "Not in a git repository."
exit 1
fi
head_log_line=$(git log --pretty='%d %s' HEAD -1)
if current_branch=$(git symbolic-ref -q HEAD); then
echo "[X] On ${current_branch##refs/heads/}:$head_log_line"
else
echo "[ ] Dangling HEAD:$head_log_line"
fi
if test -n "$(git status --porcelain)"; then
echo "[ ] Repo is not squared away (run git status)"
fi
while read branch; do
upstream=$(git rev-parse --abbrev-ref $branch@{upstream} 2>/dev/null)
if [[ $? == 0 ]]; then
local=$(git rev-parse $branch)
remote=$(git rev-parse $branch@{upstream})
base=$(git merge-base $branch $branch@{upstream})
if [ $local = $remote ]; then
echo "[X] $branch -- $upstream"
elif [ $local = $base ]; then
echo "[ ] $branch -- $upstream (need to pull)"
elif [ $remote = $base ]; then
echo "[ ] $branch -- $upstream (need to push)"
else
echo "[ ] $branch -- $upstream (diverged)"
fi
else
echo "[ ] $branch -- (no tracking branch)"
fi
done < <(git for-each-ref --format='%(refname:short)' 'refs/heads/*')
@gregsexton
Copy link
Author

This is a very simple script for getting a quick view of the status of a git repo. It outputs only the info that I think is useful and does so in a very dense and scannable form. This tool is highly opinionated and suited to my particular workflow. I also tried git-wtf.

For best results loop over your git repos first with git fetch --all and then with git tldr.

These were the design notes I made before writing it:

* I only care about local branches. If I haven't created a branch
  for it, then it's not important right now.
* Need to quickly get the following info, in a format that is
  easily scannable. Should be able to output this for many repos
  and just look down the list for anything out of place.
    * What branch am I on?
    * What local branches do I have? Which of them are up to date with
      their tracking branch?
    * Which branches do I need to push?
    * Which branches do I need to pull?
    * Does the repo have uncommited changes?

Sample output:

[X] On master: Some commit message for the current commit
[ ] Repo is not squared away (run git status)
[ ] branch1 -- share/branch1 (you should pull)
[X] master -- origin/master
[ ] branch2 (no tracking branch)
[ ] branch3 -- share/branch3 (you should push)

or

[ ] Dangling HEAD: Some commit message for current commit
[ ] branch -- share/branch (you should merge)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment