things i want to remember, things i want to share

Développeur Python et adepte Linux depuis 2005, Core développeur Kivy, passionné par beaucoup trop de choses.
Profil Github gpg signature bitcoin address

Entries tagged “git”

Monitoring your git status.

written by tshirtman, on 2/20/14 1:58 AM.

It’s not uncommon to query various git commands to know your current state in your repository, before doing the commands that do actual things.

To avoid doing some of them needlessly, or missing important information, it’s a common solution to customize a shell prompt to add the most important information in it.

However, that’s usually quite limited, and when developing on a project, or doing some refactoring or even rebasing, you’ll often do the same commands over and over.

Doing something over and over is bad, it makes you tired and mistake prone, and computers are good at doing repeated actions for you, so you just know to know the right tools.

The watch linux command allow to execute a command every nth second (n being 2 by default), so you can let that run in a shell next to the one you work in.

A first step then is:

watch git status --short

not very advanced, but actually helpful, however, there is a few over commands you’ll want to see, for example, the existing branches and the one you are on, the git log is cool too, and git reflog, being one of my favorites.

watch "git status --short && git branch && git log --graph --oneline && git reflog"

It’s nice to see on what commit a branch is, too, so let’s add that to the branch command.

watch "git status --short && git branch -v&& git log --graph --oneline && git reflog"

We have a problem, though, log and reflog will usually be way longer than our terminal can afford, and you’ll not see reflog at all.

watch "git status --short && git branch -v&& git log --graph --oneline | head -n 10 && git reflog"

(it’s actually not needed to cut the tail of reflog, as it’s the last command)

But all of this is a bit lacking in colors, lets add them!

watch can allow colors to be used, but we still have to force git to show
them. To avoid things getting to messy, we’ll define a few git aliases:

git config --global alias.cstatus "-c color.status=always status --short"
git config --global alias.bc "branch --color -v"
git config --global alias.ll "log --graph --pretty=format:"%C(yellow)%h %C(blue)%ci%C(green) %an %C(red)%d %Creset%s"

then our watch command can be

watch -c "git cstatus && git bc && git ll | head -n 10 && git reflog --color"

This is starting to look good, but there is some need of a separation between the various commands, i found it best to simply leave an empty line.

watch -c "git cstatus && echo &&git bc &&echo && git ll | head -n 10 &&echo && git reflog --color"

There is still one ugly detail though, and that’s the header produced by the watch command, thanksfully, it can be easily discarded using the -t option:

watch -ct "git cstatus && echo &&git bc &&echo && git ll | head -n 10 &&echo && git reflog --color"

The result is probably that you want to add a few things to .gitignore, delete a few branches hanging around for no reasons, and slap people doing too long commit titles :P. Which is probably a good task for the forthcoming spring :).

Bonus points if you use a tiling windows manager, and can give this a chunk of the side of your screen ;).

git, show stash list after checkout.

written by tshirtman, on 2/4/14 12:13 AM.

Sometime you start doing something in your codebase, and then you have to go do something else on another branch, so you stash them, later you come back, and you may not remember you started, them, and you forget to check your stash list.

Git has a very general solution for these kind of things: hooks

hooks are actions that you can “hook” (heh!) to events, it’s common to use the pre-commit hook to check for various errors before validating it, or to update refs after a fetch. Here, we want to display the list of stashes done on the current branch, after you checkout it.

first, we need to take the current branch.

 branch=$(git branch | grep "^\*"|sed -s s/'* '//)

then, we get the list of stash, and filter it on this branch:

 git stash list | grep "WIP on $branch"

put that in .git/hooks/post-checkout of your repository, and make it executable.

 chmod +x .git/hooks/post-checkout

But now, you have to do that in all your git repositories, but then there is this nice trick that allow you to easily use that hook in all your repositories, with just an unintrusive git init each time.

edit 2014-03-13: adding the exit 0 at the end of the script, because it prevented rebase. the code is now:

#!/usr/bin/env sh
branch=$(git branch | grep "^\*"|sed -s s/'* '//)
git stash list | grep "WIP on $branch"
exit 0

Git resources + random tips

written by tshirtman, on 1/13/14 1:04 AM.

In the last few years, git has gained a lot of popularity in open source projects, howether, newcommers, and even seasoned contributors, often seems to struggle with its interface.

There is a simple reason to that, git has beautiful internals, but you don’t care about that, you want to use it, the issue is that its interface only make (some) sense (it’s not completly consistent) in the light of its beautiful internal model.

On top of that, the initial git documentation was, to say the least, indigest, technically correct, but not really helpful, again, because it explained how things worked, not how to use them.

Howether, a good number of resources have poped up while git was gaining popularity, here is my shortlist of favorite resource, in order of complexity, to get you accustomed with it.

git - the simple guide (i usually find this one by googling “git no deep shit”, easy to remember), this colorful guide can be read in 5-10 minutes, and will get you started, sure, it glosses over details, but it’s a good startpoint, and the format makes it easy to read.

git for 4 and up This video will show you how git works with toys and manual construction of commit trees (the famous DAG) it’s a great way to understand the model, and behavior of git, you’ll see it’s, in fact pretty simple.

think-like-a-git This one will use, and really deserve, one or two hours of your time, it’s not the best start point, but it will make things a lot clearer if you have been using git for some time and still feel uneasy when less-common situations, it’ll make clearer to you that, really, nothing is ever an unrecoverable mistake with git.

Pro Git Ok, this one is now more or less the official documentation, and i rarely go for it, but when you want a definite answer, or face a complex situation, it’s a really, really, complete resource, and it’s translated in a lot of languages, like even human languages, isn’t that great?

On top of that, i’ll add a few of my own tips:

-p flag

Look for the “-p” flag in commands, you can use it on add/commit/reset/stash and others, its exact behavior of course depend of the situation, but the general idea is that when you want to see/review each part of the change, it’s what you want. I basically never use “git add” without “-p”, this way i see exactly what i’m about to commit.


git reflog

this one will show you where you’ve been, regarding commit ids, recently, if you are lost after a rebase/reset/checkout, and just want to go back, it can save you a lot of trouble.


If you do something often, or if you don’t do it often enough to remember the right options, do an alias for it, here are a few of my most useful ones.

    st = status --short
    ci = commit
    co = checkout
    br = branch
    ll = log --graph --pretty=format:\"%C(yellow)%h %C(blue)%ci%C(green) %an %C(red)%d %Creset%s\"
    lla = log --graph --pretty=format:\"%C(yellow)%h %C(blue)%ci%C(green) %an %C(red)%d %Creset%s\" --all
    nopushed = log --branches --not --remotes --simplify-by-decoration --decorate --oneline
    nomerged = branch -a --no-merged
    latest = for-each-ref --sort=-committerdate --format='%(committerdate:short) %(refname:short) [%(committername)]'
    rc = rebase --continue

creating an alias is easy

git config --global alias.aliasname "some git command"

or you can just edit ~/.gitconfig


Learn the difference! It can save your life^Wfile. ;) You should be comfortable with both pretty soon, they are very useful.

And ask questions, either to your local git guru, or on the ‘net, learn how they think about git, it’s not magical, it’s just a way to think, you just need to get the hang of it :).


#FIXME 3G absurd ad_sense alterway aléatoire android animation anonymity atheism bach backlog bash bitcoins blog blogging boldness book books boulot bricolage bépo C canvas captcha captures carte SD censure christianity chroot CI CLI cli cloudwatt code colors comfort zone command line community company life conferences contest copwatch copwatchnord-idf core-devs cours ct705 culture deb debian debug deformation dehors dessin dev distribute distribution docker débutant déploiement développement ebooks eeepad eeepc effect ego empty en escher event firefly flappy bird flask fosdem foss fr fun game garden gdb geek culture git github goals graphics grrr gödel hack hackathon hacked hacking health hooks i3 images IMAP inspirational install isync java jenkins jeu jeu video jinja2 jni keyboard keynav kivy kv lame learning lib libre life linux lol macosx magnet mail mailing-list mails maths mbsync meetings memory leak mesh meta mint mirroir MIT module motivational mouse museomix mutt nexus7 no-mouse notmuch nottoomuch offlineimap onycroit opencourseware osc packaging paris passphrase password patch pentacatyl people perte de données ping pip planning plugin positioning pr procrastination programmation progress project projet property proudhon proxy psf publisher/consumer pull-down pygame pyjnius pypi python pythonar qtile raid rapsberry pi reading recorder references release religion responsive review reviews réseau réseaux sociaux résurection salon screenshots script self service shows shutil shyness sizing solib sortie sousous!!! spam spritz stash status systeme système templating terminal texture texture coordinates Thomas Paine thread thème tiling time time management. tip tips tools transformer tutorial tv twitter typematrix typing ubuntu ubuntu-fr ultimate-smash-friends unity update upload images useless usf utils value VDM video vie/mort vim virtualenv visite widget windows wm wmii work workflow workflow. zine études