DEV Community

Cover image for Bash ~ never forget to git commit again 💥
Chris Bongers
Chris Bongers

Posted on • Originally published at daily-dev-tips.com

Bash ~ never forget to git commit again 💥

Are you:

  • ✅ Tired of failing to git commit
  • ✅ Sick of having staged changes
  • ✅ Colleagues complaining they're missing code
  • ✅ Angry because your laptop fried, and you didn't commit?

Then this article is for you!

Today we will be making a bash script that we can run at the end of our day.
It will loop through our project directory and tell us the following statistics per project:

  • Is it a git repo
  • Did you forget to commit something
  • Do we have unstated changes

Have a look at hacking your morning routine!

It will look like this:

Bash to show git status

Bash git commit prompt script

Today we'll be looking at a single bash script.
I'm going through this section by section. At the end I'll link it on GitHub for you to download.

We will start by defining our variables

Change the DIR to your project folder.

DIR=~/www/
GITBASED=.git
Enter fullscreen mode Exit fullscreen mode

Then we need to loop over each subdirectory in our projects folder.

for dir in $DIR*
do
    // Loop here
done
Enter fullscreen mode Exit fullscreen mode

Then inside the loop, we need first to check if it's a directory we are checking:

if [[ -d $dir ]]; then
    // Yes I'm a directory
fi
Enter fullscreen mode Exit fullscreen mode

You can see we check the dir based on the -d (directory)

If it's a directory, we can work with it:

We'll cd into the directory and define an empty message.

cd $dir
MSG="";
Enter fullscreen mode Exit fullscreen mode

Then we check if it's a git project.
If it's not a git project we change our message.

if [ -d "$GITBASED" ]; then
        // Git based!
else 
    // Not a valid git project
    MSG=": Not a valid git project 👀"
fi
Enter fullscreen mode Exit fullscreen mode

If it is a git project, we will first define a test variable that will execute git status.

TEST=$(git status $dir);
Enter fullscreen mode Exit fullscreen mode

Our variable TEST now contains the return value of git status now we will use some if...else statements to check if it contains certain substrings:

if [[ $TEST == *"nothing to commit"* ]]; then
    MSG=": No changes ✅"
// Check if git status has unstaged changes
elif [[ $TEST == *"Changes not staged for commit"* ]]; then
    MSG=": Unstaged changes 🤷‍♂️"
// Check if git status has uncommitted changes
elif [[ $TEST == *"Untracked files"* ]]; then
    MSG=": You forgot to commit some files 😡"
fi 
Enter fullscreen mode Exit fullscreen mode

And lastly, we will echo the message prefixed with he project name and change back a directory.

echo ${dir##*/}$MSG
cd ..
Enter fullscreen mode Exit fullscreen mode

That's it!

If we run our bash.sh script, we will get all lines per project folder with the status.

Run it with: sh bash.sh

No more reasons to forget your commits!

Find the project on my GitHub afterwork.

Thank you for reading, and let's connect!

Thank you for reading my blog. Feel free to subscribe to my email newsletter and connect on Facebook or Twitter

Top comments (6)

Collapse
 
vlasales profile image
Vlastimil Pospichal • Edited

I use git status in $PS1.

Drop the `MSG=": No changes ". This produce an electronic smog.

Collapse
 
dailydevtips1 profile image
Chris Bongers

True the no changes is perhaps a overkill haha

Collapse
 
sujaykundu777 profile image
Sujay Kundu • Edited

Nice ! By Project Folder, did you mean the top level directory where all the projects reside ? Thanks for the trick

Collapse
 
dailydevtips1 profile image
Chris Bongers

Yes I have one www folder with al kinds of sub folders indeed.

Collapse
 
m_ghorbani94 profile image
Mohammad-GHorbani

So, What difference has this by "git status" ?

Collapse
 
dailydevtips1 profile image
Chris Bongers

This does it for all folders at once, it is basically using git status, but makes it quicker to check all your projects at once.