Most of the time, I use these git
commands :
git add src/ package.json
git commit -m "custom message"
git push origin "name_of_the_branch"
So I want to make a single alias command which should look like:
git_alias src/ package.json && "custom message" && name_of_the_branch
By running the above alias, it should run all three git
commands (add
, commit
and push
).
How can I do this?
Note
I know that I can make alias for any command (in this case, git commit
) as follows:
alias gc='git commit -m '
In this case you need to make a function. Add the following snippet to your
~/.bash_aliases
:Now, when you reload your Bash config, this function and alias should be defined.
Notice the three parameters:
$1
,$2
and$3
. These are the command line arguments for your function. Also note that the expression&& \
(logical AND plus escaping the line break) will only run the subsequent command if the first command is run.Now you can run your function (or the alias
gc
) with three arguments corresponding to the three strings (package name, custom message and name of branch). Make sure that all arguments containing spaces are enclosed in quotes, like this:You could also add some sanity checks, for instance that the files you want to add exists:
(Now modified to accommodate multiple files as argument
$1
)@arturmeinild approach is very good. If you need to keep track of more such alias or script commands, you can create a folder in
/home/$USER/bin
and store the commands in idividual script files.Paste the following contents into the file
Save andnclose the file. In your
~/.bashrc
, add this line at the end if it is not already there (check for similar path export commands)Now you can use the
gc
commands anywhere. You can extend it to do anything. For example set the command fail if something is wrong in between usingset -e
above the commands.Also if you sync this folder or create the above command script file in any syncable folder, you will have this command backed up.