Adding a function() to .zshrc - zshrc

I would like to create a shortcut for rebasing x number of commits:- git rebase -i HEAD~3. I have a function function gri() { git rebase --interactive HEAD~"$1"; } and have tried adding it to my .zshrc file, .bashrc & .bash_profile. But where ever I add it when I try and run gri(2) for example, I get the error zsh: command not found: gri(2)
SOLVED
Just needed to remove function, gri() { git rebase --interactive HEAD~"$1"; } works a treat :D

Related

Bash completion for git aliases containing multiple subcommands

I have set up the following alias in my .gitconfig file:
[alias]
ss = stash show
Unfortunately bash completion does not work correctly on this alias. When I type git ss <TAB>, I get:
❯ git ss <TAB>
apply clear drop pop show
branch create list push
Which is obviously the completion for git stash instead of git stash show.
With the original command I get the list of available stashes:
❯ git stash show <TAB>
stash#{0} stash#{1}
Is there a way to get the completion on the alias behave like on the original command?
I am on Ubuntu 20.04 and using the distro's default git completions.
I posted this question on the Git mailing list and got a reply to work around this issue:
It is possible to make completion work for your particular
alias by using our completion script's extension mechanism that allows
users to specify completion functions to their own git commands. If
you type git foo <TAB> and there is a _git_foo() function in your
shell's environment, then the completion script will invoke that
function to perform completion; this works not only for commands but
for aliases as well. So for your alias you only need to "borrow" all
the "show"-subcommand-specific case arms from _git_stash() and place
them in a _git_ss() function, e.g. like this:
_git_ss () {
case "$cur" in
--*)
__gitcomp_builtin stash_show "$__git_diff_common_options"
;;
*)
__gitcomp_nl "$(__git stash list | sed -n -e 's/:.*//p')"
;;
esac
}
Add it to your ~/.bashrc, or to a separate file that you source from
your .bashrc; If you use bash-completion, then you don't even have to
touch you .bashrc: save that function to a file git-ss (dash, not
underscore!) in one of the directories scanned by bash-completion
($BASH_COMPLETION_USER_DIR, ~/.local/share/bash-completion/completions
or its XDG_DATA_HOME-equivalent) and it will be auto-loaded as needed.

How to use function on git-prompt on Git Bash for Windows?

I want to use the last exit code to customize my bash prompt. So following this question I should add $? inside a function:
PROMPT_COMMAND=__prompt_command
__prompt_command() {
local EXIT="$?"
...
I tried many times, but whenever I add a function on my C:\Program Files\Git\etc\profile.d\git-prompt.sh file, Git Bash can't run this file, and it doesn't use it.
Is there a way to use a function on git-prompt.sh on Git Bash for Windows, or another approach to use the last exit code to customize the bash prompt?
Follow these instructions to configure your git-bash prompt.
First, open git-bash, and cd to your ~ directory.
Using vim or nano, edit your ~/.bash_profile file to contain the following two lines:
test -f ~/.profile && . ~/.profile
test -f ~/.bashrc && . ~/.bashrc
Then create a ~/.bashrc file and add your custom prompt command function in this file:
PROMPT_COMMAND=__prompt_command
__prompt_command() {
local EXIT="$?"
# The rest of your code here
}
Make sure your changes saved correctly for both your ~/.bash_profile and ~/.bashrc files.
Open a new git-bash window. Assuming you have no bugs in your __prompt_command() function, your custom prompt should display properly.
If you quickly want to test your prompt, you can instead just run source ~/.bashrc in your current git-bash window rather than launching a second one.

How do I enter the commit message in this bash pop-up?

I want to merge a remote repository to a new repository on GitHub. I have tried the following bash commands:
git init
git commit --allow-empty -m "Initial dummy commit"
git remote add --fetch old_a <OldA repo URL>
git merge old_a/master --allow-unrelated-histories
After executing the last command, this screen pops up:
Here bash is asking me to enter a commit message. But typing appears to be prohibited everywhere except in the yellow line. Not only that, I can't even terminate this prompt if I want to go back to the command line. There seems no way to break out other than closing bash itself. So how do I proceed from here?
PS: I have already tried to change my default git editor to vim using the following commands:
git config --global core.editor vim
As well as,
export EDITOR="vim"
But both of these commands didn't work.
This is not an issue, all you need to do is
Press Insert Key and then Enter Key. This will allow you to type a message
Once you have done that Press Esace (Esc) then :wq to exit

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)

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