Is it possible to configure the ls
command so that, when I use ls -l
, it appends the git branch name after the directory name, if the directory is a git repository?
This way I can quickly find git repositories under the current directory as well as their working status.
Something like this:
-rw-rw-r-- 1 michael michael 8 Aug 26 02:07 a-file
drwxrwxr-x 3 michael michael 4096 Aug 26 02:07 a-repo/ (master)
drwxrwxr-x 2 michael michael 4096 Aug 26 02:07 not-a-repo/
Note: I'm not looking to show git branch for the current directory or as part of the shell prompt, which I already know how.
Best Answer
I wrote a little Bash function using mainly
awk
to process the output ofls
and append git branch names to directories, if they are part of repositories.Installation:
To install the function, simply copy the line below and append it to the end of your
~/.bashrc
file. You have tosource .bashrc
after that or restart the shell session for the change to take effect.After that, you will have a new command
lg
available that behaves like the defaultll
alias (actuallyls -alF
), but with the appended current git branch.Example usage:
Here is some example output, not the branch names in braces behind
git1/
andgit2/
:The
lg
command still accepts all kinds of arguments, just likels
does. You can e.g. runlg -h
,lg ~/projects
,lg ..
etc.Updates:
#
.Known bugs and drawbacks:
ls
output (e.g. directories are blue, executables green, symlinks cyan, ...).ls
output will always be correct and display information about the path you specify as argument (if any, otherwise current directory as default). However, the branch information for the./
and../
entries is always relative to the current working directory, not the specified directory.If you encounter more problems or happen to know a solution for one of the listed issues, feel free to leave a comment.