Codementor Events

Automating push with just a single bash command

Published Jul 12, 2020Last updated Aug 07, 2020
Automating 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

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.

Discover and read more posts from Saurav Jaiswal
get started
post commentsBe the first to share your opinion
Martin Málek
4 years ago

Now I read the cron part. No, git is no replacement for some backup tool. It’s not how it should be used for development, this is completely wrong approach.

Martin Málek
4 years ago

Why should anyone use this script? It doesn’t make any sense, especially how VCS should be used. And git ass well. You should not commit all the changes at once. Each commit should be atomic, you should check what you are committing.
For git, it’s not that necessary to push with every commit. But it’s not against anything. You will just not be able to amend/rebase without force push.

Laszlo Marai
4 years ago

Nah… you don’t need to ‘automate’ git push as that’s just a single command: git push. That’s it. What you wanted to do is probably automating push after every commit, which might be fair enough of a goal but it does more pretending that those operations are always the same. But they aren’t

First of all, always adding every file to every commit from the current directory? That’s insane. You’d add all your temp data files (that you haven’t added to git ignore, of course), all files even though you may want to add some of them in separate commits. Then the push will push to master. Which will screw you up if you were by accident working on a branch. (Which you should, at least sometimes, even if you are the sole developer on a project.)

Not to mention that you can run git from any directory in a repository and in that case (when you are not in the repo root) git add . will miss the files that you may have wanted to check in.

Automating push after the commit could pretty simply be achieved by using a bash alias or a git command alias. You could also alias commit (or git commit) and push to be a one or two letter command (git commit -> gc or git c, etc.) to save a few keystrokes.

Show more replies