DEV Community

Cover image for you may need this - bash script
Abhishek Pathak
Abhishek Pathak

Posted on

you may need this - bash script

I created a bash script for handling

git add., git commit -m "" and git push

Don't ask why,
Yes, I will tell
For a developer, these 3 commands are the most important to work on on a daily basis.

But it's a little bit time-consuming—not much, but 2 seconds, I can say.

So I created this script.
Follow me.

Create a file

vim gitpush.sh
Enter fullscreen mode Exit fullscreen mode

The Code

#!/bin/bash
git add .
git commit -m "$1"
git push
Enter fullscreen mode Exit fullscreen mode

Each line explanation:

#! -> bash shebang

git add . -> add all the files to staging

git commit -m "" -> write changes permanently.

git push -> push to the remote origin.

Exit the vim

a. Enter `Esc`

b. Enter `:`

c: Enter `wq`

![exit vim](https://imgur.com/XA9IOdC.png)
Enter fullscreen mode Exit fullscreen mode

The entire file should be like this:

entire process

Now we need to give execute permission to the file.
Follow the command.

chmod 700 gitpush.sh
Enter fullscreen mode Exit fullscreen mode

Command breakdown
chmod: This is used to change the file permissions.
700: 700 is divided into 3 groups: a) 7, b) 0, and c) 0.

a) refers to the current user.
b) refers to the group
c) refers to other users in the system.

Group a is 7, which means the current user has all the permissions. You can understand this by

[
4: for read,
2 for write,
1 for execute
]

So, 7 = 4 + 2 + 1, so the current user will have all the permissions.

0 means no permissions.

The entire steps will look like:

entire step

The gameplay:

gameplay

As you can see, I have used the absolute path of the file with just the commit message. Liek simple.

But,
In bash, we can use alias to avoid typing the file path every time.

Follow me:

Go to the Home directory

cd ~
Enter fullscreen mode Exit fullscreen mode

Open your shell script; I'm using zsh.

vim .zshrc
Enter fullscreen mode Exit fullscreen mode

If you are using bash, it must be .bashrc. You can check that by

echo $SHELL
Enter fullscreen mode Exit fullscreen mode

The core command

alias -g gitpush='/home/scor32k/blogs/scripts/gitpush.sh $1'
Enter fullscreen mode Exit fullscreen mode

restart the shell (close the terminal and open again). This will globally set the alias.

Now the fun
Follow

final

I have only used

git push "commit message"
Enter fullscreen mode Exit fullscreen mode

and all three imp git commands are done.

This was just a fun project. I thought of creating

Top comments (2)

Collapse
 
cappe987 profile image
Casper

Automating things with scripts always feels nice. But in this case I am personally a bit opposed to it due to

  1. always committing all files. I often have changes I don't want to include, or things should be split up into multiple commits.
  2. using -m. Commits should be descriptive, and using -m limits you to one long line. I think it's much better to open the commit in an editor and carefully write a good commit message. Sometimes a short one is enough, but not always.

In the long run, especially in larger, company-wide, projects it's important to write good commit messages. You can read my longer explanation here: Don't use 'git commit -m'.

And as a side-note, git add . only adds the files from the current directory and down. If you are not in the project root you may miss some files. If you truly want all changed files you should use git add -A

Collapse
 
scorcism profile image
Abhishek Pathak

Hey Casper,
Your all the points are valid.
I also follow the method you suggested, but for personal use the script is cool.
This was just a fun project.
Your blogs are awesome Casper. Love it