Git status on Unix terminal

It is often required to get git stats on terminal PS1, most solutions are based around bash which can be slow sometimes or if you are like me who programmed something in ruby also turned out to be slow. We can make things faster by porting such scripts to golang. Here is one such solution :

package main

import (

var getSymbol = map[string]string{

func execCommand(command string) string{
        gitExec := exec.Command("git", command)
        outPut, err  := gitExec.Output()
        if err != nil {
                return err.Error()
        return string(outPut)

func processStatus(status string) string{
        var noVC = regexp.MustCompile("Not a git repository")
        var unTracked = regexp.MustCompile("Untracked files")
        var notStaged = regexp.MustCompile("Changes not staged for commit")
        var exitCode = regexp.MustCompile("exit status 128")

        switch {
                case noVC.MatchString(status) :
                return getSymbol["novc"]
                case unTracked.MatchString(status):
                return getSymbol["untracked"]
                case notStaged.MatchString(status) :
                return getSymbol["changes"]
                case exitCode.MatchString(status):
                return getSymbol["noentry"]
                return getSymbol["alliswell"]


func processBranch(branchdata string) string{
        var branchMatch = regexp.MustCompile(`[(^\*)].+`)
        match := branchMatch.FindAllString(branchdata, 1)

        if match != nil{
                return "\033[38;5;112m"+ match[0][1:] + "\033[0m\n"

        return getSymbol["novc"]

func gitBranch() string{
        return processBranch(execCommand("branch"))

func gitStatus() string{
        return processStatus(execCommand("status"))

func main(){
        var args []string = os.Args[0:]

        if len(args) > 1 {
                switch args[1]{
                case "b", "branch":
                case "s", "status":
                case "p", "prompt":
                        fmt.Print(gitStatus(), " ", gitBranch(), ">>")
                fmt.Println("Command Not found")

You can download this by executing the following command on your terminal :

$ wget ''

And then compiling it with go.

go gits.go -o gits

Once that is done you should put it somewhere and included in the PATH. Also the PS1 should be modified like so.

PS1="\[\033[38;5;202m\]$(pwd)\n\[\033[38;5;135m\]\@ $(gits prompt)"

This would yield a prompt like this :


If git files are unstaged on the current path then may be like this :


Thats it folks! Happy hacking.


comments powered by Disqus