The github repository for this script -> Script
Hey there fellow coders! 🚀
Ever found yourself doing the same repetitive git commands every time you want to push some changes? I know, it can be a bit annoying. But guess what? Bash scripts to the rescue! I know how you feel if you are remembering the repetative push and commits while reading these lines. Simply put, we have all been there. So, I decided that why not make a script of our own. From then on, I whipped up a neat little script that does the heavy lifting for you. Let's dive right in!
What Does This Script Do? 🤔
This script aims to automate three core git actions:
- Switching to the branch you specify.
- Checking if there are any changes to commit.
- Committing those changes with a message you provide and pushing to the specified branch.
Sounds fun, right?
Breaking Down The Code 🛠
#!/bin/bash
The shebang! This tells the system that our script should run with the Bash shell.
Now, the script first checks if you've given it the required inputs: a branch name and a commit message.
if [ "$#" -ne 2 ]; then
echo "Usage: $0 <branch_name> <commit_message>"
exit 1
fi
The magic $#
gives us the count of arguments you've passed in. If it's not two, the script will remind you how to use it.
branch_name="$1"
commit_message="$2"
Here we're just storing the arguments in nicely named variables to make things clearer later.
Now, let’s define our helper functions:
1. check_changes()
: This checks if there are any changes to commit. If there aren't, it'll let you know and end the script.
check_changes() {
if [ -z "$(git status --porcelain)" ]; then
echo "No changes to commit."
exit 0
fi
}
2. commit_and_push()
: It does exactly what the name says. First, it stages all changes. Then, it commits with the message you provided. Lastly, it pushes to the branch.
commit_and_push() {
git add .
git commit -m "$commit_message"
git push origin "$branch_name"
}
Alrighty, the main show! 🎪
The script first checks if you're inside a Git repository.
if ! git rev-parse --is-inside-work-tree >/dev/null 2>&1; then
echo "Error: Current directory is not a Git repository."
exit 1
fi
If you are, it switches to the branch you mentioned:
git checkout "$branch_name"
Then, our good friend check_changes
checks if there are any... well, changes.
And finally, commit_and_push
comes into play and, well, commits and pushes the changes.
How Do I Use This Script? 🚀
Clone the given Github repository and navigate into the script directory. To use it:
- Make it executable:
chmod +x git-auto.sh
- Run the script:
./git_commit_and_push.sh <your-branch-name> "Your commit message over here"
Voila! Less hassle, more coding! 🎉
Wrapping Up
This script is a neat way to save some keystrokes and automate repetitive git actions. Feel free to expand upon it or tailor it to better fit your workflow. Happy coding, and may the force of automation be with you! 🤓
P.S. Always remember to have backups and test new scripts in a safe environment before using them on critical projects!
Top comments (12)
Seem good but if you can add chatgpt to review and resume to get main message is good idea ✌️😁
OMG. That's a great idea. I will make sure to implement it and once I do, I'll share the track here with y'all. Thanks for the idea @g33knoob . I really appreciate it.
tried. it works. I wonder if such sh can be added to the bash environment. That helps commanding as
git_commit_and_push
"Your commit message over here"Oh wow. This is some great stuff.
Thanks for the support!! 😁
Great idea! But rather than making it in an executable script, I'd recommend putting it in your ~/.bashrc. This makes the functions readily available as commands on your shell.
Do you know husky?
Aliases can provide quick shortcuts too, e.g.,
snyk.io/blog/10-git-aliases-for-fa...
Heres a couple more for you:
nice article, well explained and I like the proposal but I came to say I read the title as The Beatles song.. all you need is bash pa parararaa
Very good, nice and simple script that can be useful at times. I'd just change the order of the check if the branch has any change to commit. I there's no change, the commit message could be avoided.
Some comments may only be visible to logged-in visitors. Sign in to view all comments. Some comments have been hidden by the post's author - find out more