Git alias for commit with branch name - bash

I'm trying to create a simple bash alias to commit with my branch name in MacOs. For instance, if my branch if CS-12 I'd usually commit as follows:
git commit /file/location/myfile -m "CS-12 my message goes in here"
So I'm trying to create an alias which will receive only the file name and the message, ie:
gcm /file/location/myfile "my message goes in here"
I've got the following but it's not working:
alias gcm="echo git commit $1 -m \"$(current_branch) - $2\""
where current_branch is the function:
function current_branch() {
ref=$(git symbolic-ref HEAD 2> /dev/null) || \
ref=$(git rev-parse --short HEAD 2> /dev/null) || return
echo ${ref#refs/heads/}
}
which does work.
The output of running my alias:
gcm src/pages/register/Register.js "aasdasd asdasd"
is giving me back:
git commit -m master - src/pages/register/Register.js aasdasd asdasd
any idea what I'm doing wrong? Bash is not my area of expertise.
Thanks

The escaped quotes are 'stripped' by alias, so you need to escape them once more:
alias x="echo \\\"foo\\\""
x
"foo"

aliases do not take parameters. Just write a function:
gcm() { git commit "$1" -m "$(current_branch) - $2"; }
Note that there's really no need for aliases, and you shouldn't use them. Since at least 1996, the bash man page has stated: "For almost every purpose, aliases are superseded by shell functions."

Related

I have a code that reads git log returning the error below

Error message: "fatal: your current branch 'master' does not have any commits yet"
After Making a file with this code executable
#!/usr/bin/env bash
cd "$(dirname "$(readlink -f "$BASH_SOURCE")")/.."
{
cat <<- 'EOH'
EOH
echo
git log --format='%aN <%aE>' | LC_ALL=C.UTF-8 sort -uf
} > AUTHORS
The problem is that you didn't add anything, and possibly didn't even have a change to commit, so no commit was done. If you really want that first commit without any changes, you can do this:
git commit --allow-empty -m "first commit"

bash, script to git add one file and commit

I'm trying to create a script to do this:
git add "file"
git commit -m "Comment"
My idea is to run:
gac "file" "Comment"
I know I can do something similar but for all files, with:
echo 'alias gac="/path/to/gitaddcommit.sh"' >> ~/.bash_profile
And the .sh would be:
!/bin/bash
git add .
echo “Enter commit message: “
git commit -am “$commitMessage”
Well you need two things :
A bin folder where you can put every sh script you want to use everywhere.
More knowledge about shell scripting and how you can get argv (in your ex: 'file' 'Comment')
So first go to your /home/<username> then mkdir bin && cd bin && pwd
then copy the pwd and add it into your PATH env variable inside your .bashrc
path example: PATH='/bin/:/sbin/:/home//bin
Then source ~/.bashrc you can now use every sh script inside you bin folder everywhere.
Cool so first problem done !
you don't have to do echo alias gac="/path/to/gitaddcommit.sh"' >> ~/.bash_profile anymore.
Now second problem here a post that can help you post
And let me show you for your example :
cd ~/bin && vi gac.sh
Now the script :
#!/bin/sh
if [ "$#" -ne 2 ]; then
echo "Usage: ./gac FILENAME COMMIT_MESSAGE" >&2
exit 1
fi
git add "$1"
git commit -am "$2"
First we check the number or arg then git add and commit.
Simple and fast maybe checking if arg one is a file might be a good idea too.
PS: i'm going to re write my post ahah
Here's what I have in my .bashrc:
ga ()
{
if test "$1" != "-f" && git rev-parse HEAD > /dev/null 2>&1 && ! git diff-index --quiet HEAD; then
echo 'Repo is dirty. -f to force' 1>&2;
return 1;
fi;
git add "$#";
list=$(git diff --name-only --cached | tr \\n \ );
git commit -m "Add $list"
}
The commit message is autogenerated, but you could easily modify it to prompt the user or take it from somewhere else.

How does eval stop the pathspec errors in this script?

I want to automate the many version control steps of Git. I was successful until I used git commit -S -m ${var} in my Bash script. This line gives me (pathspec errors x # of word) - 1... unless I use eval. How does eval make my script work?
I thought this article had the answer, but my issue involves a string, not an array.
Gif video of the broken vs. working Bash script
Broken code
brokenCommitCode () {
# Give it a multi-word, space-separated message
read -p 'Commit message (use quotes): ' commitMsg
commitMsg="'${commitMsg}'"
echo ${commitMsg}
git add -A &&
git commit -S -m ${commitMsg}
}
Working code
workingCommitCode () {
read -p 'Commit message (use quotes): ' commitMsg
commitMsg="'${commitMsg}'"
echo ${commitMsg}
git add -A &&
eval git commit -S -m ${commitMsg}
}
I expected the brokenCommitCode to commit properly with the message I enter on the prompt. The actual result is a pathspec error when it reaches git commit -S -m ${commitMsg}. How does eval make this work?
I'm using GNU bash, version 4.4.19(1)-release (x86_64-pc-msys) with git version 2.16.2.windows.1 on a Windows 8.1 PC.
Correct fix is
funname() {
read -p 'Commit message (use quotes): ' commitMsg
echo "${commitMsg}"
git add -A &&
git commit -S -m "${commitMsg}"
}
Why eval seems to fix:
single quotes where added to commitMsg variable (seems intent was to prevent message argument to be split on a whitespace)
looking what happens with the following message:
commitMsg="this is a message"
git commit -S -m ${commitMsg}
git commit -S -m this is a message
[error because "is" "a" "message" are taken as different additional arguments]
however it doesn't prevent because single quote is not re-interpreted but is like any other character in variable content
following with the example
git commit -S -m ${commitMsg}
git commit -S -m \'this is a message\'
[error "is" "a" "message'" are taken as different additional arguments]
with eval the single quotes are re-interpreted but also any other character which has a particular meaning in bash (;, &, ${..}, ..)
Suppose for example the following commit message which can inject arbitrary command.
commitMsg="message'; ls -l; echo 'done"
git commit -S -m 'message'; ls -l; echo 'done'

ZSH alias with parameter

I am trying to make an alias with parameter for my simple git add/commit/push.
I've seen that a function could be used as an alias, so I tried but I didn't make it.
Before I had:
alias gitall="git add . ; git commit -m 'update' ; git push"
But I want to be able to modify my commits:
function gitall() {
"git add ."
if [$1 != ""]
"git commit -m $1"
else
"git commit -m 'update'"
fi
"git push"
}
If you really need to use an alias with a parameter for some reason, you can hack it by embedding a function in your alias and immediately executing it:
alias example='f() { echo Your arg was $1. };f'
I see this approach used a lot in .gitconfig aliases.
You can't make an alias with arguments*, it has to be a function. Your function is close, you just need to quote certain arguments instead of the entire commands, and add spaces inside the [].
gitall() {
git add .
if [ "$1" != "" ] # or better, if [ -n "$1" ]
then
git commit -m "$1"
else
git commit -m update
fi
git push
}
*: Most shells don't allow arguments in aliases, I believe csh and derivatives do, but you shouldn't be using them anyway.
I used this function in .zshrc file:
function gitall() {
git add .
if [ "$1" != "" ]
then
git commit -m "$1"
else
git commit -m update # default commit message is `update`
fi # closing statement of if-else block
git push origin HEAD
}
Here git push origin HEAD is responsible to push your current branch on remote.
From command prompt run this command: gitall "commit message goes here"
If we just run gitall without any commit message then the commit message will be update as the function said.
"git add ." and the other commands between " are just strings for bash, remove the "s.
You might want to use [ -n "$1" ] instead in your if body.
I tried the accepted answer (Kevin's) but was getting the following error
defining function based on alias `gitall'
parse error near `()'
Hence changed the syntax to this, based on the git issue and it worked.
function gitall {
git add .
if [ "$1" != "" ]
then
git commit -m "$1"
else
git commit -m update
fi
git push
}
I can easily add params just using $1.
Eg:
alias gsf="git show --name-only $1"
works just fine. To call it I just use gsf 2342aa225

bash how to make command work

I have following bash function in my ~/.bashrc
function gitlab {
MSG='first commit'
CMD="git commit -m '${MSG}'"
echo $CMD
$CMD
}
Here is the result
$ gitlab
git commit -m 'first commit'
error: pathspec 'commit'' did not match any file(s) known to git.
What's the fix?
BASH FAQ entry #50: "I'm trying to put a command in a variable, but the complex cases always fail!"
Definitely read BashFAQ/050 that Ignacio linked to.
You could try this, though:
function gitlab {
local PS4='Running: '
local msg='first commit'
bash -xc "git commit -m '$msg'"
}
I suppose you should use \" instead of ' so it should be something like:
CMD="git commit -m \"${MSG}\""
Try putting your commit message in double quotes, as single and double quotes mean different things to bash.
function gitlab {
MSG="first commit"
CMD=`git commit -m \"${MSG}\"`
echo $CMD
$CMD
}

Resources