Alias error: using cd command with Git bash - bash

I am trying to add an alias to git to redirect to the project folder, open VS Code and run a local server
my alias:
[alias]
creative = cd e:/work/vero/creative-app/front-end && code . && npm rune serve
problem is when run git creative response is :
expansion of alias 'creative' failed; 'cd' is not a git command

Just prepend a ! to your alias, and it will be interpreted by bash rather than git directly.
From the doc :
As you can tell, Git simply replaces the new command with whatever you
alias it for. However, maybe you want to run an external command,
rather than a Git subcommand. In that case, you start the command with
a ! character.

Related

Create an alias with more than 1 command as the alias name

I want to create an alias that is composed of 2 commands. Could not find anything online as most questions are about an alias that equals to 2 commands, eg:
alias command="command1 && command2"
What I want:
alias git push="git add . && git commit -m 'auto ups' && git push"
Why using this instead of naming my alias gp (or whatever 1 worded alias)
I want to keep the same git push experience
Want to learn about bash
Is that possible?
As illustrated here, you would need to define a git wrapper.
It should be set in your $PATH before the /usr/local/bin/git itself.
Or you can reference that script through an alias git=git-wrapper in your ~/.bash_profile.
In that wrapper, on push, you can then call a script which would chain any git command you need.

Is there a way in bash/powershell to automatically include a word in a command based on context?

Currently, when inside git repos, I type git status. Is there any way for my shell to understand this is a git repo and automatically append git to relevant commands?
e.g.,
# bash example
# before
git status
# after
status # "status" isn't a registered command, but "git status" is, so bash uses the latter
I'd probably only want this enabled when I'm in a repo, but is there any way to get functionality similar to this in any of the popular shells?
I recognize this isn't always preferred or the best idea, but it could save me from typing a word.
This is what aliases were invented for in bash. You'd add a line to your bashrc that looked like this:
alias status='git status'
If you'd like, you could add one for every git subcommand you ran frequently:
alias commit='git commit'
alias checkout='git checkout'
alias add='git add'
alias push='git push'
alias pull='git pull'
alias config='git config'
Your environment is your own, and if you wanna make a shorthand for git commands, you're free to do so!
Also, these aliases will pass any arguments to the underlying command. So if you write:
commit -m "My message"
This will translate to git commit -m "My message".
These aliases should be added to a file like the .bashrc file in your home directory, if one exists.
See here for a more in-depth explanation of aliases.

How to use a line of hyphen to separate git command results within git alias in git-bash

I've already created a alias called info to display status, branch and log in git-bash. However, all the information was bunched up and somewhat annoying to read, so i wanted to make it easier on myself when reading it by adding a line of hyphens with new line above and below it. After trying many times, I can't get it to work. Therefore i'm seeking help here.
Here's what I tried before, trying to get one to work before copying it in between git branch and git log.
$ git config --global alias.info '!git status && echo && !printf -- '-%.0s' {1..80}; echo "" && echo && git branch && git log'
Below is what the result should look like.
git status result
-----------------------
git branch result
-----------------------
git log result
If the ! syntax doe not give the expected result, try and embed your commands in a shell function within your Git alias:
alias.info !f() { echo begin arg=$1/$2/end; }; f
Or even in a separate script:
git config --global alias.info '!sh info.sh'
The point is: if you can make it work in a regular shell script, you will be able to call it from an alias.
Or you can even name your script git-info (executable, no extension), and simply type:
git info
No alias needed there: any git-xxx script can be called as git xxx (if git-xxx is in your PATH)

Using !! to access bash history inside a Git alias

I want to create an alias that runs git stash and then executes the command that was before it.
This would be useful when git doesn't allow to run a command with unsaved changes, such as checkout, rebase, etc.
What I've tried:
s = "!git stash && fc -s
s = "!bash -c \"git stash && !!\""
None of the above work. It looks like in the first one git creates a subshell to run this command, as fc outputs no command found.
The second one is similar, but here I explicitly create a subshell and it obviously doesn't work, with no access to the history.
Is there a way around this? It's likely that this could be accomplished with a bash alias, but I'd prefer to do it through a git alias.
It's likely that this could be accomplished with a bash alias,
Yes, this would be simple and robust.
but I'd prefer to do it through a git alias.
Given how ugly and fragile this would be, I doubt it. However, here you go:
First, you need to relay the history. Make your shell write it out to the history file after every command by adding this to your .bashrc:
PROMPT_COMMAND='history -a'
You can then add your git alias. It needs to
run with Bash, since the system sh may not support history expansion
enable history and history expansion
read the history file
use history expansion in a separate parsing unit (e.g. after a linefeed, outside a compound command):
So all in all:
s = "!bash -c 'set -Ho history; history -r ~/.bash_history\ngit stash && !-1'"
Note that it'll run the last command executed regardless of bash instance, so if you use multiple tmux/screen/terminal windows, it won't necessarily run the last command in your current shell.
You can't use a git alias to access the Bash history (or any other shell data for that matter), since the command after the exclamation mark is not run as a subshell. Git is an external command, so all its child processes are also not subshells.
More details
You can confirm this by setting a git alias foo = !echo $$ $BASHPID and comparing it with the current shell:
$ echo $$ $BASHPID
11461 11461
$ git foo
25437
In my case it's not even running Bash. After some testing I think it's running /bin/sh, which is Dash for me since I'm using Ubuntu.

simple bash function does not understand git

I have git installed on my machine using homebrew as you can see below.
$ git --version
git version 1.7.3.4
I am learning bash programming and I have following function in my ~/.bashrc
function gitlab {
CMD= "git --version"
$CMD
}
However when I run gitlab I get following output
$ gitlab
-bash: git --version: command not found
I am using bash on mac.
Your problem is the space after CMD=. This evaluates to CMD=<empty string> "git --version". First, the environment variable CMD is set to an empty string, then the command git --version is called. Notice that due to the "..." the shell really tries to execute a command git --version and not git with argument --version. Simple remove the space between = and " to fix this.
You are running the entire string as a single command. It is possible to have a binary called foo bar (with a space), and you would run it just as you showed.
Edit: What are you actually trying to do? There's no need to store a command in a variable before running it:
gitlab() {git --version}
and
alias gitlab='git --version'
both do the same as you were expecting from the function above. In general, the function solution is recommended above the alias (and you don't need to prefix it with function).
There is a superfluous space after the = when you try to assign the string "git --version" to the CMD variable. The shell think this is an assignation of an empty string to the CMD variable that is only local to the next command. As your command is quoted with ", the shell try to execute a program called git --version. As no such program exist, it fails.
Your function is interpreted almost like the following function (the error is easier to see here):
function() {
CMD=
"git --version"
unset -v CMD
$CMD
}
You should remove the space character to have it interpreted like you intended. However, there are easier way to do the same thing:
function gitlab() {
git --version
}
If you what you wanted was to store the result of the git --version execution to be stored in the CMD variable, you should have done it this way:
function gitlab() {
VERSION="$(git --version)"
echo "$VERSION"
}
Edit: corrected my reply as it was incorrect (error spotted by #DarkDust).

Resources