Table of Contents
1. What exactly is the problem?
2. Git commands are not much a hassle
3. Method 1: Creating a bash script
3.1. How to implement this solution?
4. Method 2: Using git alias command
4.1. How to implement this?
4.2. Some useful blogs and resources
What exactly is the problem?
Well, we all have been using git commands a lot, while working on projects, I bet everyone has spammed git commit
and git add .
more than they've realized it.
Sometimes, writing long git commands can take up time, and writing them multiple times can build up frustration. Even some smaller commands can take time, which on compounding can show that you may have wasted hours by not using these shortcut tricks.
This blog post helps you to solve the exact problem. I'll show you different methods, to save your time and you can use any of the following depending on your workflow.
Git commands are not much a hassle
Well some of you may already come for me in comments saying these repitative git commands ain't that much hassle, but sometimes writing few letters less can help us not to lose sanity fast.
Let's talk about our first method
Method 1: Creating a bash script
We can indeed create a bash script which can help us to eliminate writing multiple commands. For e.g. you can eliminate writing git add .
, git commit -m ""
, git push -u origin main
, and do it using a single custom command.
Following is an example of how to eliminate writing above three commands:
#!/bin/bash
# Recommend to use!
# Advance script to automate repitative commands
currentBranch="$1"
message="$2"
# Check if current branch and commit message are provided
if [ -z "$currentBranch" ]; then
echo "Usage: $0 <currentBranch> <commitMessage>"
exit 1
fi
if [ -z "$message" ]; then
echo "Usage: $0 <currentBranch> <commitMessage>"
exit 1
fi
# Check if git command is available
if ! command -v git &> /dev/null; then
echo "Git command not found. Aborting."
exit 1
fi
# Check if the current directory is a Git repository
if ! git rev-parse --is-inside-work-tree >/dev/null 2>&1; then
echo "Not inside a Git repository. Aborting."
exit 1
fi
# Check if there are changes to commit
if [ -z "$(git status --porcelain)" ]; then
echo "No changes to commit. Aborting."
exit 1
fi
# Add all changes
git add .
# Commit changes
git commit -m "$message"
# Push changes to origin
git push -u origin "$currentBranch"
echo "Automated Git tasks completed successfully."
How to implement this solution?
- Create a new file in your working directory named
git-quick
- Paste the above script in it
-
Make it executable using
chmod +x git-quick
-
Now you can run the file using
./git-quick
This was one of many use cases, you can automate other commands like this depending on your workflow.
Bonus: You can also make it usable anywhere by following the following guide:
But there'll some people who like to do multiple commits before pushing the code.
Well there's another method you may try, which includes using the git alias
command.
Method 2: Using git alias command
git alias
is probably the most useful command when it comes to save that hassle when it comes to type longer commands
For e.g: git push -u origin main
can be easily simplified as git push
or even git p
. As simple as it gets!
How to implement this?
Well atlassian has a detailed guide related to using git alias
command, you can check that out or here's a quick summary.
- Aliases can be created by either using
git config
as shown below, or it can also be created by editing$HOME/.gitconfig
for global path, for local you can edit./.gitconfig
of the active repository. Following is an demonstration about how to create aliases using both methods:
// By using 'git config' command
$ git config --global alias.co checkout
// By editing './.gitconfig' file (paste the below code)
[alias]
co = checkout
- You can use git alias to add short names to regular git commands.
$ git config --global alias.co checkout
// Can be used as git co
$ git config --global alias.br branch
// Can be used as git br
$ git config --global alias.ci commit
// Can be used as git ci
$ git config --global alias.st status
// Can be used as git st
- You can also create some custom commands using git alias
git config --global alias.unstage 'reset HEAD --'
// This can make following two commands equivalent
$ git unstage fileA
$ git reset HEAD -- fileA
Some useful blogs and resources
- Official documentations of git aliases
- Synk's blog discussing about - git alias for faster workflow
- Gist discussing about git alias command
Final thoughts
In order to become a Git Ninja you can use any of the two methods mentioned above depending on your workflow.
If you have some more tips that I may have missed, then feel free to share them in the comments.
If you found any mistake in this blog then correct me up in the comments.
Thank you for reading this far! ❣️
Follow me on Twitter @eagledev_ to recieve some useful tips and tricks.
Top comments (0)