0

I've been using iTerm2 with zsh, and with this following alias I get an error every time I open a new iTerm2 window or tab.

alias clear-git-branches="git fetch -p && for branch in `git branch -vv | grep ': gone]' | awk '{print $1}'`; do git branch -D $branch; done"

The error I get is:

fatal: not a git repository (or any of the parent directories): .git

Since it's throwing an error related to directory not being a git repository, my guess is that it got some problem related with its quotes. I tried changing it but got the same error.

I use this command to clear local branches with no upstream.

9
  • I think that the alias is somehow being run from the wrong directory. It should be run from a directory that contains a .git sub-directory. Check out what you have in your home directory - that should be your current directory that is changed upon the creation of a new tab. Also, try to take the Operating System Commands (such as git fetch -p, for instance) one by one and see what is going on with each one of them. Debugging a whole One-Liner at once in an instant is a Trick performed only by Master Minds. Also, this has nothing to do with the Bourne-Again Shell-Scripting Environment.
    – user1018743
    Oct 17, 2019 at 17:36
  • What happens if you comment it out? If you still get the problem, you might have some other lines in your .zshrc that's running something. Oct 17, 2019 at 18:52
  • 1
    What does it mean "I get an error every time I open a new iTerm2 window or tab"? Aliases get executed when the are issued. Do you mean opening a new tab issues this alias? Or do you execute it manually?
    – karatedog
    Oct 17, 2019 at 20:15
  • 1
    Because of the double-quotes the command substitution is expanded once the alias is being defined, in the current working directory at the time. Is this intended? Oct 17, 2019 at 20:20
  • @xNinjaKittyx if I comment or remove this line the error stops Oct 17, 2019 at 21:14

2 Answers 2

0

As Kamil pointed out, since you are using double quotes instead of single qoutes some parts of the command gets parsed the aliases are loading (ie. when you open a tab or a windows in your terminal).
It just looks ok when you run it in your terminal because everything gets executed at once then.

Putting everything in single quotes would solve the problem but introduce problems with the inner single quotes. I recommend rewriting it as a function instead. In this case you can use it just as you would use the alias.

clear-git-branches() {
git fetch -p && \
for branch in $(git branch -vv | awk '/: gone]/{print $1}'); 
  do git branch -D "${branch}"; 
done
}
0
1

I had a similar or the same problem with the following in my .zshrc:

# use gcm to checkout master or main
alias gcm="git checkout $(git remote show origin | grep 'HEAD branch' | sed 's/.*: //') && git pull"

Upon opening a new iTerm2 window or terminal window, or reloading the environment i got:

source ~/.zshrc
fatal: not a git repository (or any of the parent directories): .git

Commenting out that line made the issue go away:

# alias gcm="git checkout $(git remote show origin | grep 'HEAD branch' | sed 's/.*: //') && git pull"

After rewriting; swapping double and single quotes:

alias gcm='git checkout $(git remote show origin | grep "HEAD branch" | sed "s/.*: //") && git pull'

The error went away.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .