how to automate the "commit-and-push" process? (git)

You can very easily automate this using Bash scripting.

git add .

echo 'Enter the commit message:'
read commitMessage

git commit -m "$commitMessage"

echo 'Enter the name of the branch:'
read branch

git push origin $branch

read

store the above code as a .sh file(say gitpush.sh)

And since you have to make this sh file an executable you need to run the following command in the terminal once:

chmod +x gitpush.sh

And now run this .sh file.

Each time you run it, It will ask you for the commit message and the name of the branch. In case the branch does not exist or the push destination is not defined then git will generate an error. TO read this error, I have added the last read statement. If there are no errors, then git generates the pushed successfully type-of message.


That is not a lot of typing to do but the process can be simplified by the usage of aliases.

edit $HOME/.gitconfig to add some shortcuts, e.g. to use git p as an alias for git push origin master, use:

[alias]
        p = push origin master

If you're on a descent shell like bash, you can use the history feature. Everything you need to know is in the man page for bash.

Read more about aliases here

On a side-note, git add . is probably not the best approach since it adds every file in the folder to the staging area. Better to use git add -u to only add those files already in the index.


Commiting your work once a day as a big chunk is not an effective use of version control. It's far better to make small, regular, atomic commits that reflect steps in the development of the code base (or whatever else is being tracked in the repository)

Using a shell script is perhaps overkill - most people use aliases to automate git commands, either as shell aliases or git aliases.

In my case I use shell aliases - a couple of which are:

alias gp='git push'
alias gc='git commit'
alias gca='git commit -a'
alias gcm='git commit -m'
alias gcam='git commit -am'

so for me to do what you're asking would be:

gcam "My commit message";gp

This isn't a lot of typing and is hardly boring, I do this and similar git operations literally dozens of times a day (I know - because I check my shell history).

The reason for using small aliases rather than a script is so that I have the flexibility to use different ways of committing, If I used a script I would lose that flexibility.

Tags:

Git