Show Git Branch Name in Terminal - MacOS Catalina - missing zsh files - bash

I have a fresh MacOS Catalina 10.15.6 and I'm following the steps on this link show git branch name in terminal macos so I can display the branch name on my terminal whenever I cd in a git repository.
I'm in my home directory so when I run ls -la, I can't see any of the following files
.zshrc, .bashrc, .bash_profile, .zprofile
The only relevant files showing on the list are .bash_history and .zsh_history.
Are these located on a different path or am I missing any step in the process?

MACOS Instructions- execute the following commands in your terminal:
cd
nano .zshrc
copy and paste this lines of code:
autoload -Uz vcs_info
precmd() {vcs_info}
zstyle ':vcs_info:git:*' formats '(%b)'
setopt PROMPT_SUBST
PROMPT='%n> ${PWD/#$HOME/~} ${vcs_info_msg_0_}%%'
CTRL+o ENTER to save and CTRL+x to exit
source .zshrc
and that's it. Please share your configuration!

Simplest answer, install Oh My ZSH
Trust me, you can't do a better job customizing your own .zshrc file.
Just go here: https://ohmyz.sh/#install
Run this command in your terminal:
sh -c "$(curl -fsSL https://raw.github.com/ohmyzsh/ohmyzsh/master/tools/install.sh)"
Voila, you got pretty terminal outputs

Following this answer on SuperUser StackExchange, I would assume that you have to create .zshrc, .bashrc, etc., on your own. As far as I can tell, you just need a .zshrc for what you are doing. Then you can continue with the setup from the tutorial.

Related

macOS Catalina 10.15(beta) - Why is ~/.bash_profile not sourced by my shell?

I want to set the environment variable I added below the line to ~/.bash_profile and ~/.profile but it didn't work.
export JBOSS_HOME=/Users/{USERNAME}/Desktop/jboss7
Afterward, exit the terminal and open it again when executing echo $JBOSS_HOME I get nothing.
Apple has changed the default shell to zsh. Therefore you have to rename your configuration files. .bashrc is now .zshrc and .bash_profile is now .zprofile.
If you for some reason (as me) don't want to rename/move your ~/.bash_profile file you can do the next things:
Create a new file ~/.zprofile
Type there source ~/.bash_profile
Save and close
Run a new terminal session
You can just copy your existing bash_profile and name it zprofile and it will work fine.
Run the below command in terminal and you are set after closing and opening new terminal.
cp ~/.bash_profile ~/.zprofile
I created a new file called
/usr/local/bin/mybash
which contains a wrapper script:
/usr/local/bin/bash --init-file $HOME/.bashrc
I installed this local/bin/bash from HomeBrew.
Full Sequence of Events
brew install bash
echo "/usr/local/bin/bash --init-file $HOME/.bashrc" > /usr/local/bin/mybash
chmod +x /usr/local/bin/mybash
Then I opened the settings for terminal.app [cmd-comma]. Under the General Tab, select the radio button for Command (complete path)
In the text box change the text from /bin/zsh/ to /usr/local/bin/bash.
After you close a Terminal window, variables you set in that window are no longer available. If you want the value of a variable to persist across sessions and in all Terminal windows, you must set it in a shell startup script. For information about modifying your zsh shell startup script to keep variables and other settings across multiple sessions, see the “Invocation” section of the zsh man page.
You can use ~/.zlogin to add your variables.
Check out this reference.
changing the bash profile to zsh profile works and source it as well to see in action.
vikas#Vikas-Kumar ~ % mv .bash_profile .zsh_profile
vikas#Vikas-Kumar ~ % source .zsh_profile
You can create a simbolic link and keep your .bash_profile file with this:
ln -s .bash_profile .zsh_profile
source .zsh_profile
Any changes in .bash_profile will be reflected in .zsh_profile
Even with os Catalina /bin/bash comes for free, brew is not needed. Simply create your .bash_profile and set shell in terminal settings to /bin/bash. it automatically finds your .bash_profile. z-shell is not bash-shell and simply renaming will work in most cases but definitely is not correct.
you don't need to update the file, zsh is mac's default, put this in terminal. e.g.:
export ANDROID_HOME=$HOME/Library/Android/sdk
export PATH=$PATH:$ANDROID_HOME/emulator
export PATH=$PATH:$ANDROID_HOME/tools
export PATH=$PATH:$ANDROID_HOME/tools/bin
export PATH=$PATH:$ANDROID_HOME/platform-tools
cp zprofile ~/.zprofile
Add to .zprofile:
export LC_ALL=en_US.UTF-8
export LANG=en_US.UTF-8
eg. by >vi .zprofile
Done

I have to type export PATH=~/anaconda/bin:"$PATH" everytime I rerun the terminal

I have installed the Anaconda for Mac, but there is something wrong with me:
when I type the commandwhich conda or which ipython, I get conda not found and ipython not find
Then I find this command export PATH=~/anaconda/bin:"$PATH" works for me. It solves the problem above, but everytime I rerun the terminal the problem is still there, I have to type it again.
so I want to find a way to solve the problem fundamentally
I have tried to add it into the ~/.bashrc, ~/.profile, ~/.bash_profile, but these don't work for me.
Try this in .bash_profile
export PATH="$HOME/anaconda/bin:$PATH"
Then try launching a new terminal and running:
echo $PATH
The output should start with /anaconda/bin:
If that still doesn't work... A work around might be to invoke bash after running terminal i.e. type "bash". Which should cause bash to launch with .bash_profile
I run on MacOs Catalina 10.15 and this did the trick for me:
shell is zsh !
$ source /Users/myprofilename/anaconda3/bin/activate
then
$ conda init zsh
the new anaconda documentation also highlights this:
Make sure you're not using ZShell or another form of a shell. If the case you'd have to add the path to your respective shell file, e.g .zshrc.
sudo xed /etc/environment
after open this archive add :/home/youruser/anaconda3/bin
If you're using ZShell follow the steps below:
In your terminal type open ~/.zshrc
Add the following to the file export PATH=/opt/homebrew/bin:$PATH
Save your file and then run the following command source ~/.zshrc
Please note that the homebrew path on Apple silicon is /opt/homebrew/bin

~/.profile, ~/.bashrc, and ~/.bash_profile not running on new terminal start up

I am trying to create a permanent alias for my terminal. I put the alias in my ~/.profile, ~/.bashrc, and ~/.bash_profile files, previously empty. When I start a new terminal, bash does not recognize the alias, but if I source any of them, it does. Why are these not getting run when I open a terminal? I am on OSX.
Newer MacOS versions use zsh as the default shell for both Terminal and iTerm2. Run echo $SHELL to confirm if this is the case for you.
Zsh looks for a .zshrc file upon shell startup, so to continue using zsh while sourcing the contents of your bash profile, you can run the following:
echo "source ~/.bash_profile" >> ~/.zshrc
Open a new terminal window for the changes to take effect.
Two things need to happen here when using iTerm to get the loading of dotfiles to work.
First you should add the following to your .bash_profile
[[ -s ~/.bashrc ]] && source ~/.bashrc
Secondly you will need to ensure that in iTerm preferences your terminal is set to launch a login shell.
Hope this helps!
Using the default mac terminal, what worked for me was to add a command to run on start up to source my .bash_profile.
Preferences > Profile > Startup > Add command 'source ~/.bash_profile'
Mac terminal preferences window screenshot
Might be considered to be a bit hacky, but it does the trick.
Adding source ~/.profile to my .bash_profile worked for me.
As of High Sierra, both Terminal and iTerm want to load ~/.profile first. So I suggest you put one line in your .profile to make your Mac work like other Unixes:
source ~/.bash_profile
By editing this one file, you won't have to search through the menus of multiple apps to override Apple's bizarre behavior in each.
As of Catalina the default shell is now zsh. You can change it back to bash with chsh -s /bin/bash and that should load your .profile or .bash_profile
Why are your shell's initialization files not loading?
As with most things, It Depends ™
I recently experienced the same phenomenon and went through the following exercise to resolve it:
I use iTerm. iTerm runs a login shell by default. Verify in iTerm Preferences > General > Command > (*) Login Shell
Therefore, I know that ~/.bash_profile will always be called.
Knowing that, I put the following in my ~/.bash_profile file:
for file in ~/.{bashrc,bash_exports,bash_aliases,bash_functions}; do
[ -r "$file" ] && source "$file"
done
unset file
Notice that I use separate files for .bashrc, .bash_exports, etc. It keeps things separate and simple.
Note also that /etc/profile is loaded first, but since I have never used that system wide init file, I knew that that was not my problem. For details check out $ man bash
So, I started with my ~/.bash_profile file.
I found that when I installed Canopy Express that it's installer replaced the contents of my ~/.bash_profile file with the following content:
# Added by Canopy installer on 2017-04-19
# VIRTUAL_ENV_DISABLE_PROMPT can be set to '' to make the bash prompt show that Canopy is active, otherwise 1
alias activate_canopy="source '/Users/lex/dev/python/User/bin/activate'"
# VIRTUAL_ENV_DISABLE_PROMPT=1 source '/Users/lex/dev/python/User/bin/activate'
p.s. Canopy is an excellent, free python IDE, that I highly recommend.
Fortunately, I backup my ~/.bash* files so restoring that was easy and quickly fixed my issue.
My advice would be to understand the order of calls to your initialization files and start with the first one and work your way through them until you find the problem.
Oh, and you may want to verify which shell you are using (I use bash):
~ $ echo $SHELL
/usr/local/bin/bash
I am guessing you may use another shell, such as bash, tcsh, sh, zsh etc.
Put source .bash_profile into your appropriate 'bashrc' file will make the auto loading recovered, i.e.
.login for tcsh, .bash_profile for bash, .zshrc for zsh
My issue was solved by unchecking Preferences > General > tmux >
Use "tmux" profile rather than profile of the connecting session
Most likely, you need to create the files yourself as they appear not to exist by default. You should give them execute permission to make them run.
~ % sudo chmod 700 ~/.bash_profile
Also, you should check the ownership of the files. They should belong to current user rather than root. Otherwise, you will get permission denied error.
~ % ls -a -l
~ % sudo chown <user_name> ~/.bash_profile
Finally, please note that bash looks in your home directory for .bash_profile, .bash_login, and .profile in order. Bash will stop looking if the first is found.
This means if you have both .bash_profile and .profile files, the .profile will not run.
For more information
Hope this would help you.
Little late to the party but it seems that the file .zprofile is the equivalent to that of .bash_profile when loading zsh. I used this instead to execute a few commands on startup. Of course this only valid for a specific iTerm setup with zsh.
https://zsh.sourceforge.io/Intro/intro_3.html

Is there anything in Zsh like .bash_profile?

Everytime I open the terminal, I have to source .bash_profile to enable the $JAVA_HOME or other variables.
Yes, it's called ~/.zshenv.
Here's how I have $JAVA_HOME set in ~/.zshenv:
export JAVA_HOME="$(/usr/libexec/java_home)"
Keep in mind, however, that zsh is not bash, so just 'cause you have to source your .bash_profile every time you open a terminal does not mean that you have to do that with zsh. With zsh, I only have to re-source my ~/.zshenv when I make changes to it, and then only for terminals which are already open: new terminals should have already sourced my new and improved ~/.zshenv.
NOTE
I often find it helpful, when trying to determine which of my zsh startup files I should place things in to consult zsh startup files.
A newer version of the documentation for startup files can be found here.
I know this is an old question, but I recently upgraded MacOs to Catalina which changed the default shell from bash to zsh.
I ended up doing this:
echo source ~/.bash_profile > ~/.zshenv && source ~/.zshenv
To have zsh source my original .bash_profile.
Recently, with the upgrade to macOS Catalina, the default shell changed to zsh, which uses ~/.zshrc as the resource file.
We usually had ~/.bash_profile inside user home directory the solution is to simply
Open ~/.bash_profile by running vim ~/.bash_profile
Open ~/.zshrc by running vim ~/.zshrc
Copy the content of ~/.bash_profile into ~/.zshrc
Open a new terminal window and run your previous aliases/scripts, which should work flawlessly.
Other simple alternative to continue using your .bash_profile is add this file to your .zshrc file:
Open your .zhsrc file > vim ~/.zshrc
Add this line to your .zshrc file > source ~/.bash_profile
with this simple solution you can continue adding your .bash_prifile if you like zhs.
Adding .bash_profile
There are five separate profile scripts that get executed (in the order given below) when we launch a zsh shell or close it out.
(1) .zshenv --> This is always sourced first but can be overridden by other
(2).zprofile --> This is equivalent for users coming from ksh experience
(3).zshrc --> This is for all of the interactive customizations of zsh
(4).zlogin --> This executes after first three are done
(5).zlogout --> This is executed when we logout of the zsh shell
it would be advisable to put your stuff in .zshenv or in .zshrc
It is not mandatory to have any one of these files. But if it is there, it will be sourced from and executed in the above order.
In Mac Catalina onwards osx versions, the terminal uses zsh. There is a system-wide profile /etc/zprofile.
cat /etc/zprofile
# System-wide profile for interactive zsh(1) login shells.
# Setup user specific overrides for this in ~/.zprofile. See zshbuiltins(1)
# and zshoptions(1) for more details.
if [ -x /usr/libexec/path_helper ]; then
eval `/usr/libexec/path_helper -s`
fi
it says , if you want to override then create ~/.zprofile.
touch ~/.zprofile.
update: macOS Monterey 12.4
yes - for Zsh, it is the file: .zshrc
add there your parameter.
In Mac Catalina, terminal uses zsh. Instead of having .bash_profile, good to have .zshenv and write your script there.
When you open terminal next every time, scripts inside .zshenv gets executed.
I was running into this issue and I followed Zack and Luke Schoen's answer, but my $PATH didn't look the same as what I had in bash.
This post explains what the different config files do:
https://unix.stackexchange.com/questions/71253/what-should-shouldnt-go-in-zshenv-zshrc-zlogin-zprofile-zlogout
I found that splitting my .bash_profile path exports into .zprofile and my aliases into .zshrc worked best for what I wanted.
I found why Zack and Luke Schoen's answer didn't work for me:
The path exports that I listed in .zshenv were executed first and /usr/libexec/path_helper was executed afterwards,
which prepended the paths listed in /etc/paths.
I found the profile file under /etc/zprofile location. This will be for zsh

Homebrew’s `git` not using completion

When using OSX’s git, after I modify a file I can simply do git commit <tab>, and that’ll auto complete the file’s name to the one that was modified. However, if I install a newer version of git from homebrew and I use it, that feature no longer works (meaning I press <tab> and it just “asks” me what file I want to do it on, even including the ones that have no changes).
Can anyone shed some light as to why, and how to solve that? I’d prefer using homebrew’s git, since it’s more up-to-date.
My shell is zsh, and Neither installing bash-completion or zsh-completions worked (even after following homebrew’s post-install instructions).
Also, after installing git with homebrew it says
Bash completion has been installed to: /usr/local/etc/bash_completion.d
zsh completion has been installed to: /usr/local/share/zsh/site-functions
So shouldn’t I be able to use one of those?
You're looking for:
brew install git bash-completion
As warpc's comment states, you'll need to add the following to your ~/.bash_profile to get homebrew's bash-completion working:
if [ -f $(brew --prefix)/etc/bash_completion ]; then
. $(brew --prefix)/etc/bash_completion
fi
The above is mentioned in the caveats when you install the bash-completion formula.
Note: if you are using Bash v4 or later (via brew install bash) then you're going to want to use brew install bash-completion#2, to enable tab completion add the following to ~/.bash_profile as described in the caveats:
export BASH_COMPLETION_COMPAT_DIR="/usr/local/etc/bash_completion.d"
[[ -r "/usr/local/etc/profile.d/bash_completion.sh" ]] && . "/usr/local/etc/profile.d/bash_completion.sh"
The additional export is necessary for git, docker, youtube-dl, and other completions which may be included in the $(brew --prefix)/etc/bash_completion.d/ directory.
This get's git tab completion working on OSX without having to restart your terminal:
curl https://raw.githubusercontent.com/git/git/master/contrib/completion/git-completion.bash -o ~/.git-completion.bash
echo "source ~/.git-completion.bash" >> ~/.bash_profile
source ~/.bash_profile
EDIT: this doesn't work in Catalina's default zsh shell. I changed the default shell back to bash and it works again. https://www.howtogeek.com/444596/how-to-change-the-default-shell-to-bash-in-macos-catalina/
In case anyone else makes my dumb mistake, try brew install git. I was using the git that comes with Xcode and didn't realize that I had never installed Homebrew's git to get the autocompletions.
for some reason I was missing the file at $(brew --prefix)/etc/bash_completion so #Graham Perks' correct answer didn't work for me
It ended up the fix in my case was:
brew unlink bash-completion
brew link bash-completion
I solved the problem by figuring out that $(brew --prefix)/etc/bash_completion returned Permission denied when executed. So after a simple:
chmod +x $(brew --prefix)/etc/bash_completion
Everything is now working fine. I'm wondering why Homebrew doesn't make the bash_completion file executable on installation, though.
For bash on macOS Catalina (3/30 update: Big Sur too), if you want to also use Bash 5 from homebrew, you need to make sure that your login shell is set to homebrew's bash, and not the default.
To check if you need to do this, run echo ${BASH_VERSION}. If you see a version starting with 3, you are not using Brew's bash for your login shell.
To change this,
Open System Preferences->Users and Groups.
Right click your user and select "Advanced Options". You may need to unlock this with your password by clicking the lock in the bottom left.
Set the login shell field to the location of your brew's bash, which you can usually find by running which bash in a terminal after you install brew's bash. Mine was /usr/local/bin/bash.
Restart your terminal, and follow the instructions in this excellent answer
Found a working solution. It's very recent (authored 16 hours ago, and committed 2 hours ago), and it comes directly from homebrew.
brew install git --without-completions
Just tried it, and it finally works as intended.
I had the same issue and even found this post this morning. I fixed the issue by updating brew with brew update and then reinstalling git with brew reinstall git.
I was then notified of another file that is blocking the homebrew linking process, in my case it was /usr/local/share/zsh/site-functions/git-completion.bash. Removing the file and running brew link git solved the issue. Guessing it was just a bad recipe version we stumbled upon.
If you have $BASH_VERSION < 4.1, eg 3.2.57(1)-release then go ahead with:
brew install bash-completion
# In ~/.bash_profile :
if [ -f $(brew --prefix)/etc/bash_completion ]; then
. $(brew --prefix)/etc/bash_completion
fi
However if you've brew install bash to get version 4.4.12(1)-release
you can use the better and more complete completions in:
brew install bash-completion#2
# In ~/.bash_profile:
[ -f "$(brew --prefix)/share/bash-completion/bash_completion" ] \
&& . "$(brew --prefix)/share/bash-completion/bash_completion"
Note that some packages (brew, docker, tmux) will still put some completions into $(brew --prefix)/etc/bash_completion.d/ so you might add:
for completion in "$(brew --prefix)/etc/bash_completion.d/"*
do
. $completion
done
Finally you should be able to add the git completion script if for some reason the way you installed git did not add it to either of those:
[[ -f $(brew --prefix)/etc/bash_completion.d/git \
|| -f $(brew --prefix)/share/bash-completion/completions/git ]] \
|| curl https://raw.githubusercontent.com/git/git/master/contrib/completion/git-completion.bash \
-o $(brew --prefix)/etc/bash_completion.d/git
You can get and add it with the above.
Step 1: Download auto completion script:
cd ~
curl -O https://raw.githubusercontent.com/git/git/master/contrib/completion/git-completion.bash
Step 2: Update .bash_profile and .bashrc
echo "source ~/git-completion.bash" >> .bash_profile
Via https://www.anintegratedworld.com/git-tab-autocomplete-on-osx-10-11-el-capitan/
If above does not work, try https://github.com/bobthecow/git-flow-completion/wiki/Install-Bash-git-completion
In 2019, using Bash v5, you do not need to explicitly source the git bash completion script in your .bash_profile or .bashrc
Ensure you have the following two lines in your .bashrc
export BASH_COMPLETION_COMPAT_DIR="/usr/local/etc/bash_completion.d"
[[ -r "/usr/local/etc/profile.d/bash_completion.sh" ]] && . "/usr/local/etc/profile.d/bash_completion.sh"
Download the git bash completion script (https://raw.githubusercontent.com/git/git/master/contrib/completion/git-completion.bash) and save it to /usr/local/etc/bash_completion.d/ as git
That's it! Bash will automatically pick up the git completion file and enable completion.
Side Note: I recommend putting all these changes in .bashrc as this ensures that when you drop into an interactive shell (ie. from pipenv shell), completions will get loaded correctly as bash will source .bashrc and NOT .bash_profile.
For me , I had to put
source $(brew --prefix)/etc/bash_completion
into .bashrc (not .bash_profile) to get this to work.
".bash_profile is executed for login shells, while .bashrc is executed
for interactive non-login shells" -- from What is the difference between .bash_profile and .bashrc? It appears to me that MacOS Sierra doesn't execute .bash_profile when opening a new terminal window, only .bashrc.
I wouldn't put it in _bash_profile, because then I'd have to reboot/logout for updates to take effect.
This worked for me in Mojave (OSX 10.14.1):
brew install bash-completion
Then add the following line to your ~/.bash_profile:
[ -f /usr/local/etc/bash_completion ] && . /usr/local/etc/bash_completion
It may have something to do with libedit being used instead of readline in Lion.
Try installing readline before git.
brew install readline
For those who already have brew bash-completion installed. I did not have the git completion script installed and could not find any tap for that.
So I added it manually:
curl https://raw.githubusercontent.com/git/git/master/contrib/completion/git-completion.bash -o $(brew --prefix)/etc/bash_completion.d/git
Note that you have to rename the file and remove the extension for it to work.
If you do not have completion or git installed, install it in the accepted answer.
brew install git bash-completion
If you used homebrew to install git, then probably there is no need to install anything to support git completion.
"git-completion.bash" file is somewhere (mine was here: /usr/local/git/contrib/completion/git-completion.bash)
All you need to do is to find the file:
sudo find / -type f -name "git-completion.bash"
Then source its path in your .bash_profile.
For example I needed to add this line to my ~/.bash_profile:
source /usr/local/git/contrib/completion/git-completion.bash
Don't forget to source your ~/.bash_profile or reopen your terminal ;)
from:
how-enable-git-tab-completion-bash-mac-os-x
I know this is an old post, but you don't really need to install any additional packages.
Homebrew is informing you that there is a directory with all the stuff you need.
You can simply add the following line to your .bash_profile if you are using Bash:
source /usr/local/etc/bash_completion.d/git-completion.bash
If nothing works, it could be because you have an older version of bash and bash completion script is not getting sourced by the /usr/local/etc/profile.d/bash_completion.sh script. You can test this by adding a simple echo inside the conditionals in file /usr/local/etc/profile.d/bash_completion.sh:
10 if shopt -q progcomp && [ -r /usr/local/Cellar/bash-completion#2/2.11/share/bash-completion/bash_completion ]; then
11 # Source completion code.
echo "doing bash completion or not"
12 . /usr/local/Cellar/bash-completion#2/2.11/share/bash-completion/bash_completion
And open a new terminal. If you don't see the echo message, then the conditionals do not evaluate to true. In my case it was because the bash version was old, the default from mac 3.2.blah.
I did install a newer bash from brew, but i forgot to chsh and that caused me a lot of headache. bash --version would return 5.1.8 but the enabled shell was still the old one :) To test the enabled bash you can do
for n in {0..5}
do
echo "BASH_VERSINFO[$n] = ${BASH_VERSINFO[$n]}"
done
The fix was to sudo chsh -s /usr/local/bin/bash After which the completions worked.
After tearing my hair out over this one for ages, I discovered that when I had the hub command installed, the completions for the hub command were breaking the completions for git. I had to remove /usr/local/etc/bash_completion.d/hub.bash_completion.sh. This meant no completions for hub, but git completions now worked. I didn't debug why this was.
I had the following brew packages installed:
bash: 5.1.16
bash-completion#2: 2.11
git: 2.35.1
hub: 2.14.2
Enable Auto Completion of GIT commands on MAC-OS Mojave 10.14
I am a developer and use GIT from the command line all of the time. When I consider the development perspective, I used to execute a lot of commands using the command line for GIT operations. Most of the time it is very annoying that MAC OS doesn't have automatic support for the command completion which I partially support. as well as the command suggestions, which means what are the commands available for typed characters. So it is very troublesome to type very long command and mostly repetitive task as typo going wrong. :(
Tab completion would certainly be faster and easier. Unfortunately, the default installation of git on some Mac computers doesn't have tab completion enabled.
So that I was searching for a fix for the problem and there are several solutions found from the web search such as StackOverflow, GitHub as well as from the medium. Unfortunately, those solutions did not work for me and got frustrated with trying different solutions so many times.
I was searching deeply and trying out different solutions and fortunately, it is an easy fix. Below are the steps I have collected from several posts and finally it worked as expected. So I hope to share with others who have this problem like me.
f you go to the web search and you can find many solutions which mentioned the git completion bash file. Even GitHub guide as well. But I suggest you check first if the git-completion.bash file is already in your MAC computer with the git-core or something else which came from installation. you can use below command.
sudo find / -type f -name "git-completion.bash"
you will get below results. (may have some difference according to the content)
/Library/Developer/CommandLineTools/usr/share/git-core/git-completion.bash
/Users/Dilanka/git-completion.bash
/Users/Dilanka/.oh-my-zsh/plugins/gitfast/git-completion.bash
/Users/Dilanka/Downloads/git-completion.bash
I suggest you to pick which installed from git-core
If the git-completion.bash script doesn't exist on your machine, please retrieve it from the below provided above and save it to your local machine in a new file called git-completion.bash in the /usr/local/etc/bash_completion.d/ directory.
https://git-scm.com/book/en/v1/Git-Basics-Tips-and-Tricks
If you use the Bash shell, Git comes with a nice auto-completion script you can enable. Download it directly from the Git source code at
https://github.com/git/git/blob/master/contrib/completion/git-completion.bash
If the git-completion.bash script exists on your machine, but is not in the /usr/local/etc/bash_completion.d/ directory, you should create that directory and copy the file into it. Below command will do the job:
sudo mkdir /opt/local/etc/bash_completion.d
sudo cp /Library/Developer/CommandLineTools/usr/share/git-core/git-completion.bash /usr/local/etc/bash_completion.d/git-completion.bash
After the completion of above. The git-completion.bash script should exist on your local machine in the/usr/local/etc/bash_completion.d/ directory.
Now you need to refresh your profile using below command. It will load your added bash file to the terminal context.
source ~/.bash_profile
Great!!! you have done it. Just start the terminal window and try it. Just type "git sta" it will show suggestions as below:
git sta
stage stash status
git chec<TAB> will show git checkout
see my GitHub post here:
https://github.com/DIL8654/Enable-Auto-Completion-of-GIT-commads-on-MAC-OS-Mojave
See my Medium post here:
https://medium.com/#dilanka85/enable-auto-completion-of-git-commands-on-mac-os-mojave-10-14

Resources