Hey all!
This is my first post in here; so please bear with me. :)
Background
I used to use sourcetree for working with GIT; lately it is consuming a lot of RAM and CPU resources, GIT extensions is another client that is nice and light weight. But working directly with command line feels liberating, I am mostly able to understand and learn on what is exactly going on with command line instead of a GUI.
Earlier I used to use just my laptop for work and desktop for personal work. Due to the pandemic we all mostly work from home. In my case I keep switching between PC and desktop.
First good step was that I moved all my personal projects to Github. π‘
Problem
Due to use of multiple computers and GIT, usage of the 3 powerful commands increased (git add, commit and push). And sometimes I just want to push changes with default commit message to save my work.
-
Stage all files:
git commit .
-
Add commit
git commit -m {commit message}
-
Push changes to current branch, in this case master
git push origin master
I am sometimes lazy to type and execute the 3 commands. What I want is ability to do this in a single command. π
Scripting for the rescue
This was an opportunity to learn some automation and execute this with a single command, found that I could use some shell scripting. π
I created a bash file that can help me with this minor inconvenience:
With this script I can now run command from command line as: sh expressPush.sh "My commit message"
, It does the following:
- Stages all files
- Adds a commit with provided message (My commit message) as an argument.
- Reads current branch name and executes a git push.
Additionally command is executed without a message as: sh expressPush.sh
, this uses current date time stamp for message. I use this when message is not relevant.
Now its not nice to copy this file in every project. π©
Make the script available to projects
As most my project have a package.json
file, I can store this shell script in a local folder and add a script for this in each project, package.json
looks would like below:
"scripts": {
"expresspush": "sh 'E:\\GITUtils\\expressPush.sh'"
}
- Now we can simply run
npm run expresspush "My commit message"
ornpm run expresspush
within the project directory.
This is good enough but has a problem with dependency as this assumes or hardcodes the directory of shell script file in package.json
. And not all projects may based on npm
. π
Make the script available globally π€©
Learnt later that we can alias the shell script file to make the command available globally.
To do so, create or update .bashrc
file in directory: C:\Users\username
and add:
alias expresspush='sh "E:\GITUtils\expressPush.sh"'
Now we can simply run the script with alias from command line in any GIT project:
-
expresspush
orexpresspush "My commit message"
Closing:
This solved my problem. If you have any thoughts or suggestions to improve, please do let me know. ππΌ
Top comments (4)
Have you tried making a "custom command" for git?
You could try make one like it says on this article: Extending Git.
This looks quite interesting and thanks for sharing with me, I will give this try! :)
Custom command felt hard and unfortunately I could not make it work. So I created an alias in .bashrc and it worked like charm. :)
That's a nice trick to save push efforts every time. Thanks @rahul