Git alias with bash script errors out on variable modification

git alias - multiple commands
git alias with parameters
bash alias
bash alias with arguments
git alias list
git alias bashrc
git alias escape quote
useful git aliases

My git branch name:

If I run this script in the console, it works properly:
bash -l -c 'current_branch="$(git rev-parse --abbrev-ref HEAD)" && no_feature=${current_branch##*/} && no_underscore=${no_feature%%_*} && history -s "git commit -m \"$no_underscore | " && echo "Press UP to start commit command:" && echo "" && read -e -p "> " && eval "$REPLY"'
It drops me in a prompt that helps me auto-generate a commit message based on the feature branch name.
> bash -l -c 'current_branch="$(git rev-parse --abbrev-ref HEAD)" && no_feature=${current_branch##*/} && no_underscore=${no_feature%%_*} && history -s "git commit -m \"$no_underscore | " && echo "Press UP to start commit command:" && echo "" && read -e -p "> " && eval "$REPLY"'
Press UP to start commit command:


But if I turn it into a git alias, the script errors out.

auto-message = !bash -l -c 'current_branch="$(git rev-parse --abbrev-ref HEAD)" && no_feature=${current_branch##*/} && no_underscore=${no_feature%%_*} && history -s "git commit -m \"$no_underscore | " && echo "Press UP to start commit command:" && echo "" && read -e -p "> " && eval "$REPLY"'
> git auto-message
bash -l -c 'current_branch=$(git rev-parse --abbrev-ref HEAD) && no_feature=${current_branch: -c: line 0: unexpected EOF while looking for matching `''
bash -l -c 'current_branch=$(git rev-parse --abbrev-ref HEAD) && no_feature=${current_branch: -c: line 1: syntax error: unexpected end of file

Using git commit's -e option would be far simpler if you just want to seed the commit message with some initial text. It also allows the user use their text editor of choice, rather than forcing readline on them.

git commit -em "$(git rev-parse --abbrev-ref HEAD | sed 's|.*/||; s|_.*$||;' )"

(One drawback is that this starts with a temporary file that contains the -m argument, so simply quitting the editor without saving isn't sufficient to abort a commit. You have to explicitly save an empty file to do that.)

This, then, is much easier to quote properly:

auto-message = !git commit -em "$(git rev-parse --abbrev-ref HEAD | sed 's|.*/||; s|_.*$||;')"

One weird trick for powerful Git aliases, Which will take all uncommitted and un-staged changes currently in the git allows you to shell out in aliases – that is to escape to a shell (like bash or The trick is to wrap our git command in an “anonymous” bash Doing it like this we have access to command line variables and An error occurred. If you study up on Git hooks, you’ll find yourself able to find out all kinds of information that Bash knows nothing about, but you can leverage that information with a Bash alias. Here’s an alias to find the top level of a Git project, no matter where in that project you are currently working, and then to change directory to it, change to

In Bash, when to alias, when to script, and when to write a function , An alias should effectively not (in general) do more than change the default options of a Make ls output in color by default. alias ls="ls --color=auto" # make mv ask before a ); # prints "Alias" bash -c "alias aa='Another Alias'; aa" # ERROR: bash: aa: Changes to those variables do not propagate back to the parent script. This shows you how to run a simple single line bash command as a git alias. You first tell git you are going to run a shell command with !() then you give it your single line of bash. Multi line aliases and passing parameters. The next step is to be able to run more of a shell script. Multiple lines and be able to pass in arguments.

A different solution. This doesn't uses aliases, but just uses a script.

Create a file in your path with the name git-auto-message (no extension!) with the following content:


current_branch="$(git rev-parse --abbrev-ref HEAD)"

read -p "Complete commit message (empty cancels) \"$no_underscore | " msg
# remove leading whitespaces
# remove trailing whitespaces
if [ -n "$msg" ]; then
    git commit -m "$no_underscore | $msg"

Make the script executable.

Then you can call git auto-message. Git realizes, that it doesn't have an internal command named auto-message and that there is no alias with that name. So it searches the path for an executable named git-auto-message. So the script gets executed.

While aliases are great for smaller things, this feature allows to create rather complex functionalities.

Updated script, as I missed, that you want to add additional text.

Bash Shortcuts to Enhance Your Git Workflow, By learning a little Bash, you can to create Git aliases that will save You can check out the bash documentation for more on parameter The script again uses the optional branchName to substitute a The benefit of this command is that it will stop processing if the previous command failed to complete. This section will focus on Git aliases. To better understand the value of Git aliases we must first discuss what an alias is. The term alias is synonymous with a shortcut. Alias creation is a common pattern found in other popular utilities like `bash` shell. Aliases are used to create shorter commands that map to longer commands.

Environment Variables, Git always runs inside a bash shell, and uses a number of shell environment with your commands, but you don't have access to change or remove it. GIT_PAGER controls the program used to display multi-page output on the command line. This includes the expansion of aliases, and delegation to other sub-programs. Git doesn’t automatically infer your command if you type it in partially. If you don’t want to type the entire text of each of the Git commands, you can easily set up an alias for each command using git config. Here are a couple of examples you may want to set up:

Git Aliases, git config --global checkout $ git config --global branch $ git Test for current head Signed-off-by: Scott Chacon <>. We’ve added a few aliases for commands we haven’t yet discussed. We will talk about the git branch command very soon, and the git cat-file command is useful for exploring git. 04 Command aliases (optional) If your shell supports aliases, or shortcuts, you can add aliases on this level, too. I use: File: .profile

Bash aliases you can't live without, With those aliases defined in your Bash initialization script, you can then type ll and =0077,codepage=437,iocharset=ascii,shortname=winnt,errors=remount-​ro) What you need is an alias to list the most recently modified files. If you study up on Git hooks, you'll find yourself able to find out all kinds of  Aliases can't be exported so they're not available in shell scripts in which they aren't defined. In other words, if you define them in ~/.bashrc they're not available to (unless you source ~/.bashrc in the script, which I wouldn't recommend but there are ways to do this properly).