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
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
This will make our script accessible globally so that we can use it from anywhere and anytime we want.
Moreover, if you want to make push scheduled over particular time you can use crontab job scheduler to do so.
Top comments (25)
I am mystified. What was wrong with
git push
?
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.
This one does add all -> commit with message -> push
git commit -a -m "message here" & git push
And if you do it once, its always in your history
That's a better approach.
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.
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.
Did you really hide my comment? I even told you about a bug in your script that you've fixed now.
Yes I did!! It was a typo not a bug
I find this very unkind. My comment was not offending by any means.
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.
It's not about things I prefer. I started a conversation whether the recommendations you give in your blog are best practice.
Alright. I might have misread. I will take your suggestions. Thank you for that :)
And I believe it would be great if you politely read what I have written on the blog.
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, sogit -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 ofif [ n $(...) ]
.Maybe add your password or ssh key in the env too 😝
shouldn't need to if your ssh-agent is setup properly ;)
Yes! We can. But i avoided it on purpose and thank you for suggesting
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.
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.
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.
Top!
Thanks!
Great, a dynamic branch, would be a nice to have.
Yes, Abdellatif.
Some comments have been hidden by the post's author - find out more