DEV Community

Saurav Jaiswal
Saurav Jaiswal

Posted on

Automating git push with just a single bash command

Say you are working on a project or learning something or doing challenge like #100daysofcode and you are only person who is committing files to the github repository. Wouldn't it be great if we could make whole git process automated instead of typing every time same git commands to push file on to github repository.

We can do a simple hack to automate the whole process with just one bash command. Yes! you heard it right. One bash command can push entire repository from you local machine to github.

So lets start by writing a simple bash script and save it on your home directory with git-push.sh

echo "Enter your message"
read message
git add .
git commit -m"${message}"
if [ -n "$(git status - porcelain)" ];
then
 echo "IT IS CLEAN"
else
 git status
 echo "Pushing data to remote server!!!"
 git push -u origin master
fi
Enter fullscreen mode Exit fullscreen mode

Basically it is taking all the file from your folder and pushing it to the github. If condition checks it the files are already push or not. If it would have been already pushed then it will just return else it will push the files to the github repository.

Now, we will make this file an executable file by changing the permission using chmod command.

chmod +x git-push.sh

or

chmod 755 git-push.sh

Once the script is executable we will need to copy it to a directory that in our system expects to contain executable scripts and code. On most systems we will have a choice between two directories. If we are the only user of our system you can copy our script to either /usr/bin or /usr/local/bin. If you share your system with other people it's best to copy your script to /usr/local/bin. You will most likely need super-user privileges to copy our script to either of these directories so most likely we need to use the sudo command.

sudo cp git-push.sh /usr/bin/git-push.sh
sudo cp git-push.sh /usr/local/bin
Enter fullscreen mode Exit fullscreen mode

This will make our script accessible globally so that we can use it from anywhere and anytime we want.

Alt Text

Moreover, if you want to make push scheduled over particular time you can use crontab job scheduler to do so.

Top comments (25)

Collapse
 
craigewert profile image
CREEE

I am mystified. What was wrong with

git push

?

Collapse
 
sauravjaiswalsj profile image
Saurav Jaiswal • Edited

I did this because I have been doing 100 days of code for quite a long time and repeatedly pushing file with same command was quite irritating for me. So I thought to abstract the whole process into a single bash command. Hence I did all this. I found it the process useful so I thought to share with the community.

Collapse
 
guzzur profile image
Felix Razykov

This one does add all -> commit with message -> push

Collapse
 
craigewert profile image
CREEE

git commit -a -m "message here" & git push

And if you do it once, its always in your history

Thread Thread
 
evankapantais profile image
Evan Kapantais

That's a better approach.

Thread Thread
 
craigewert profile image
CREEE

Actually I dont do that myself. I always leave off the -m and let it pop up vim for me to enter a commit message. Usually I want to review what files are getting committed, and often my commit message is a short story.

Thread Thread
 
evankapantais profile image
Evan Kapantais

Yeah, I see your point. The way I see it, even setting up aliases makes is much easier than having to condense everything in a single line.

Collapse
 
thorstenhirsch profile image
Thorsten Hirsch

Did you really hide my comment? I even told you about a bug in your script that you've fixed now.

Collapse
 
sauravjaiswalsj profile image
Saurav Jaiswal

Yes I did!! It was a typo not a bug

Collapse
 
thorstenhirsch profile image
Thorsten Hirsch

I find this very unkind. My comment was not offending by any means.

Thread Thread
 
sauravjaiswalsj profile image
Saurav Jaiswal

It was aggressive and it did offend me. I think it would be nice of you of you appreciate things instead saying what you prefer.

Thread Thread
 
thorstenhirsch profile image
Thorsten Hirsch

It's not about things I prefer. I started a conversation whether the recommendations you give in your blog are best practice.

Thread Thread
 
sauravjaiswalsj profile image
Saurav Jaiswal

Alright. I might have misread. I will take your suggestions. Thank you for that :)

Collapse
 
sauravjaiswalsj profile image
Saurav Jaiswal

And I believe it would be great if you politely read what I have written on the blog.

Collapse
 
thorstenhirsch profile image
Info Comment hidden by post author - thread only accessible via permalink
Thorsten Hirsch

There are probably not many developers out there who favour the CLI over a Git UI, but at the same time are too lazy to type 3 short commands + commit message instead of 1 short command + commit message.

However my workflow doesn't even include git add . every time I want to commit something. It could add temporary/build files that I don't want being tracked by git, so git -a -m '...' is a safer approach, by which we're down to 2 short commands + commit message (w/o your script).

Oh, and please don't start start a "TLDR" with "Basically this is a self explanatory code", because TLDR makes the reader expect a summary. And telling your reader to read the complete code is the opposite of a summary. Btw: it has to be if [ -n $(...) ] instead of if [ n $(...) ].

Collapse
 
abhishekuniyal profile image
Abhishek Uniyal

Maybe add your password or ssh key in the env too 😝

Collapse
 
darkain profile image
Vincent Milum Jr

shouldn't need to if your ssh-agent is setup properly ;)

Collapse
 
sauravjaiswalsj profile image
Saurav Jaiswal • Edited

Yes! We can. But i avoided it on purpose and thank you for suggesting

Collapse
 
baso53 profile image
Sebastijan Grabar

Everything is #devops nowadays haha.

I can see how this is good to have for your particular use case, but it's exactly that, just for one case. Wouldn't want to accidentaly run this on a local branch other that master though.

Collapse
 
sauravjaiswalsj profile image
Saurav Jaiswal

Yes, it has.But when you are working on something like 100daysofcode or you are only person working on a repository and you know in and out of it. Then, this could be useful instead of every time typing same git commands to push files.

Collapse
 
sauravjaiswalsj profile image
Saurav Jaiswal

I did this because I have been doing 100 days of code for quite a long time and repeatedly pushing file with command was quite irritating for me. So I thought to abstract the whole process into a single bash command. Hence I did all this. I found it the process useful so I thought to share with the community.

Collapse
 
gblbjj profile image
gblbjj

Top!

Collapse
 
sauravjaiswalsj profile image
Saurav Jaiswal

Thanks!

Collapse
 
mandalawine profile image
Abdellatif ABAZINE

Great, a dynamic branch, would be a nice to have.

Collapse
 
sauravjaiswalsj profile image
Saurav Jaiswal

Yes, Abdellatif.

Some comments have been hidden by the post's author - find out more