MacOS PATH issue: which git I am using? - bash

On my macOS Catalina 10.15.5, I have two git installed, one is system default version in folder /usr/bin, one is install by brew in folder /usr/local/bin. The system $PATH env variable in as following, it is obvious /usr/local/bin is listed in front of /usr/bin. However, when I issue git on command line, the git in /usr/bin was executed, rather than the other one. How could this happen? The shell is bash.
lannis20mbp:~ lannis$ echo $PATH
/Users/lannis/anaconda3/bin:/Users/lannis/anaconda3/condabin:/usr/local/bin:/usr/bin:/bin:/usr/sbin:/sbin:/Applications/Visual Studio Code.app/Contents/Resources/app/bin
lannis20mbp:~ lannis$ which git
/usr/local/bin/git
lannis20mbp:~ lannis$ whereis git
/usr/bin/git
lannis20mbp:~ lannis$ git --version
git version 2.21.1 (Apple Git-122.3)
lannis20mbp:~ lannis$ /usr/bin/git --version
git version 2.21.1 (Apple Git-122.3)
lannis20mbp:~ lannis$ /usr/local/bin/git --version
git version 2.27.0

If you have added new binaries (executable programs) to your PATH since your shell started, you may need to rehash the lookup tables in your shell. You can do that with:
hash -r
If you want to know how the shell will interpret a given command, it is generally more useful to use type COMMAND rather than which COMMAND because type will also tell you if the command is aliased:
type git
Here is a simple example. First, see that type find and which find give the same result:
which find
/usr/bin/find
type find
find is /usr/bin/find
Next, create an alias for find which masks /usr/bin/find:
alias find='ls'
Now, see that which doesn't tell you what you want to know:
which find
/usr/bin/find
Whereas type does:
type find
find is aliased to `ls'
Read more about the hash, type and other builtins with:
help hash
help type
Notes: hash, rehash, type, builtin, builtins, built-in, built-ins, PATH, which.

Related

Upgrading bash on mac

I've tried to upgrade my bash version on my Macbook Pro (Mojave OS). To do this, I've run:
brew install bash
sudo nano /etc/shells # And then added the new bash shell to the bottom of the list.
chsh -s /usr/local/Cellar/bash/5.0.11/bin/bash
After doing this, bash -version still returns version 3 but echo $BASH_VERSION print's version 5. If I try and make an associative array (I think this isn't present in 3), it works, so I assume I am using the new shell. Why has my bash version not updated?
Although you are running your updated version of bash, the command bash is (without a full path) still pointing to the original bundled version: /usr/bin/bash.
Assuming that you actually need to call the command in this form, then you should check the order of locations in $PATH, and make sure that the bin/ folder with the new bash command is in the $PATH list before /usr/bin. Failing that, make an alias in the shell pointing to the new bash.
brew adds the bash executable to /usr/local/bin/ directory.
I think this should work:
brew install bash
sudo nano /etc/shells # And then add /usr/local/bin/bash to the bottom of the list.
chsh -s /usr/local/bin/bash

Git - how do I add the configuration for me to "tab" to get the correct branch or branches if there aren't any matches? [duplicate]

E.g. on a fresh ubuntu machine, I've just run sudo apt-get git, and there's no completion when typing e.g. git check[tab].
I didn't find anything on http://git-scm.com/docs, but IIRC completion is included in the git package these days and I just need the right entry in my bashrc.
On Linux
On most distributions, git completion script is installed into /etc/bash_completion.d/ (or /usr/share/bash-completion/completions/git) when you install git, no need to go to github. You just need to use it - add this line to your .bashrc:
source /etc/bash_completion.d/git
# or
source /usr/share/bash-completion/completions/git
In some versions of Ubuntu, git autocomplete may be broken by default, reinstalling by running this command should fix it:
sudo apt-get install git-core bash-completion
On Mac
You can install git completion using Homebrew or MacPorts.
Homebrew
if $BASH_VERSION > 4: brew install bash-completion#2 (updated version)
Pay special care which version of bash you have as MacOS default ships with 3.2.57(1)-release.
add to .bash_profile:
[[ -r "/usr/local/etc/profile.d/bash_completion.sh" ]] && . "/usr/local/etc/profile.d/bash_completion.sh"
For older versions of bash: brew install bash-completion
add to .bash_profile:
[ -f /usr/local/etc/bash_completion ] && . /usr/local/etc/bash_completion
MacPorts
sudo port install git +bash_completion
then add this to your .bash_profile:
if [ -f /usr/share/bash-completion/bash_completion ]; then
. /usr/share/bash-completion/bash_completion
fi
more info in this guide: Install Bash git completion
Note that in all cases you need to create a new shell (open a new terminal tab/window) for changes to take effect.
i had same issue, followed below steps:
curl https://raw.githubusercontent.com/git/git/master/contrib/completion/git-completion.bash -o ~/.git-completion.bash
then add the following lines to your .bash_profile (generally under your home folder)
if [ -f ~/.git-completion.bash ]; then
. ~/.git-completion.bash
fi
source : http://code-worrier.com/blog/autocomplete-git/
Most of the instructions you see will tell you to download
https://raw.githubusercontent.com/git/git/master/contrib/completion/git-completion.bash
and source that in your bash startup script like .bashrc.
But there is a problem with that, because it is referencing the master branch, which is the latest version of git-completion.bash. The problem is that sometimes it will break because it is not compatible with the version of git you've installed.
In fact, right now that will break because the master branch's git-completion.bash has new features that requires git v2.18, which none of the package managers and installers have updated to yet. You'll get an error unknown option: --list-cmds=list-mainporcelain,others,nohelpers,alias,list-complete,config
So the safest solution is to reference the version/tag that matches the git you've installed. For example:
https://raw.githubusercontent.com/git/git/v2.17.1/contrib/completion/git-completion.bash
Note that it has a v2.17. in the URL instead of master. And then, of course, make sure to source that in the bash startup script.
Ubuntu 14.10
Install git-core and bash-completion
sudo apt-get install -y git-core bash-completion
For current session usage
source /usr/share/bash-completion/completions/git
To have it always on for all sessions
echo "source /usr/share/bash-completion/completions/git" >> ~/.bashrc
Just do this in your ~/.bashrc:
source /usr/share/bash-completion/completions/git
Other answers are telling you to install bash-completion, you don't need to do that, but if you do, then there's no need to source the completion directly. You do one or the other, not both.
A more generic solution is querying the system location as recommended by the bash-completion project:
source "$(pkg-config --variable=completionsdir bash-completion)"/git
See https://github.com/git/git/blob/master/contrib/completion/git-completion.bash
You just need to source the completion script
on my ubuntu there is a file installed here:
source /etc/bash_completion.d/git-prompt
you can follow the links into the /usr/lib/git-core folder. You can find there an instruction, how to set up PS1 or use __git_ps1
macOS via Xcode Developer Tools
Of all the answers currently posted for macOS, this is only mentioned in a very brief comment by jmt...
If you already have the Xcode developer tools installed, then you shouldn't need to download anything new.
Instead, you just need to locate the already-existing git-completion.bash file and source it in your .bashrc. Check the following directories:
/Applications/Xcode.app/Contents/Developer/usr/share/git-core
/Library/Developer/CommandLineTools/usr/share/git-core
Failing that, git itself might be able to help you out. When I run git config as follows, git reports a setting which comes from a gitconfig file located in the same directory as my git-completion.bash:
$ git config --show-origin --list
...
file:/Applications/Xcode.app/Contents/Developer/usr/share/git-core/gitconfig credential.helper=osxkeychain
...
or you can always brute-force search your machine and grab some coffee:
$ find / -type f -name git-completion.bash 2>/dev/null
Thus, I have the following insertion for my ~/.bashrc:
# Git shell completion and prompt string on macOS
_git_dir="/Applications/Xcode.app/Contents/Developer/usr/share/git-core"
if [ -f "${_git_dir}/git-completion.bash" ]; then
source "${_git_dir}/git-completion.bash"
fi
if [ -f "${_git_dir}/git-prompt.sh" ]; then
source "${_git_dir}/git-prompt.sh"
fi
unset _git_dir
Note that this sources the git prompt-string script as well, since it resides in the same directory.
(Tested in macOS Catalina)
May be helpful for someone:--
After downloading the .git-completion.bash from the following link,
curl https://raw.githubusercontent.com/git/git/master/contrib/completion/git-completion.bash -o ~/.git-completion.bash
and trying to use __git_ps1 function, I was getting error as--
-bash: __git_ps1: command not found
Apparently we need to download scripts separately from master to make this command work, as __git_ps1 is defined in git-prompt.sh . So similar to downloading .git-completion.bash , get the git-prompt.sh:
curl -L https://raw.github.com/git/git/master/contrib/completion/git-prompt.sh > ~/.bash_git
and then add the following in your .bash_profile
source ~/.bash_git
if [ -f ~/.git-completion.bash ]; then
. ~/.git-completion.bash
export PS1='\W$(__git_ps1 "[%s]")>'
fi
source ~/.bash.git will execute the downloaded file and
export PS1='\W$(__git_ps1 "[%s]") command will append the checkout out branch name after the current working directory(if its a git repository).
So it will look like:-
dir_Name[branch_name] where dir_Name is the working directory name and branch_name will be the name of the branch you are currently working on.
Please note -- __git_ps1 is case sensitive.
Arch Linux
Source /usr/share/git/completion/git-completion.bash in one of the bash startup files.
For example:
# ~/.bashrc
source /usr/share/git/completion/git-completion.bash
You may be able to find the script in other locations like /usr/share/bash-completion/completions/git but these scripts did not work for me.
Mac M1
For those that are using Mac M1 environment, I was able to install via homebrew:
brew install bash-completion
Then added to my ~/.bash_profile or ~/.bashrc (whatever you use):
[[ -r "/opt/homebrew/Cellar/bash-completion/1.3_3/etc/profile.d/bash_completion.sh" ]] && . "/opt/homebrew/Cellar/bash-completion/1.3_3/etc/profile.d/bash_completion.sh"
You may need to update the version number (1.3_3). You'll just need to look it up in that directory. I would love to know if there's a better way.
Windows
How it works for me finally on Windows 10 command line (cmd):
Install Clink
Copy git-autocomplete.lua file into C:\Users\<username>\AppData\local\clink directory
Restart Windows
Ubuntu
There is a beautiful answer here. Worked for me on Ubuntu 16.04
Windows
Git Bash is the tool to allow auto-completion. Not sure if this is a part of standard distribution so you can find this link also useful.
By the way, Git Bash allows to use Linux shell commands to work on windows, which is a great thing for people, who have experience in GNU/Linux environment.
On Ubuntu 22.04 just add this line at the end of .bashrc or .zshrc
source /etc/bash_completion.d/git-prompt
On Github in the Git project, They provide a bash file to autocomplete git commands.
You should download it to home directory and you should force bash to run it. It is simply two steps and perfectly explained(step by step) in the following blog post.
code-worrier blog: autocomplete-git/
I have tested it on mac, it should work on other systems too. You can apply same approach to other operating systems.
Just put below in the .bashrc and relaunch the terminal. Navigate to Git repo to see the path in the prompt.
PS1='\[\033[0;32m\]\[\033[0m\033[0;32m\]\u\[\033[0;36m\] # \[\033[0;36m\]\h \w\[\033[0;32m\]$(__git_ps1)\n\[\033[0;32m\]└─\[\033[0m\033[0;32m\] \$\[\033[0m\033[0;32m\] ▶\[\033[0m\] '

New Windows 7 git install, missing commands in bins, e.g. alias for git bash

Just installed git on my Windows 7 laptop and desktop and it seems that some standard Unix commands are missing in the setup. I'm new to Git and not that great with Windows stuff.
I found out the issue by trying to configure the git bash using a .bashrc by adding a simple alias thus:
alias ls 'ls -F'
When the .bashrc is run (and it's getting executed when the shell starts), I get this message:
$ alias ls 'ls -F'
bash: alias: ls: not found
bash: alias: ls -F: not found
Upon closer examination of the bin directories (/bin, /usr/bin, i.e. C:\Program Files\Git\bin and C:\Program Files\Git\usr\bin, respectively) there is no alias or alias.exe.
I'm assuming this has something to do with Unix commands re-created into windows executable and not some branch of a path that I'm missing. I've checked on the path and cannot find some hidden bin directory.
I've seen several postings on installing Cygwin on it's own and I can do this but before launching into a protracted fix for both my laptop and desktop I'd like to know if I'm missing something or my git initial install is bad or if there's a package that I should be adding -- would appreciate any assistance.
The correct syntax would be:
alias ls='ls -F'
alias is a bash builtin command, so you won't find an alias.exe anywhere in the git distribution.

Shell scripts with Git Bash 64-bit on Windows

I've installed Git Bash 2.5.1 64-bit on a fresh Windows 10 machine:
mark#Foo MINGW64 ~/Documents/FsCheck (master)
$ git --version
git version 2.5.1.windows.1
When I list the contents of a directory, I can see a shell script:
mark#Foo MINGW64 ~/Documents/FsCheck (master)
$ ls
appveyor.yml build.cmd build.sh* Docs/
bin/ build.fsx Contributors.txt examples/ FsCheck.sln
(Some files and folders removed for clarity.)
Notice that the shell script build.sh has an asterisk after the extension. In the console, it's also coloured green, where other files are light grey. I haven't seen this before, so I don't know if it means anything.
When I try to run the script, I get an error message:
mark#Foo MINGW64 ~/Documents/FsCheck (master)
$ build.sh
bash: build.sh: command not found
Also, there's no tab completion for any of the build files.
How can I run the shell script from Git Bash?
You probably have ls aliased to ls -F. So when an trailing asterisk appears that means that the file is executable.
In POSIX systems, you can't directly execute files in current directory (for safety reasons). If you want to, you can use this trick:
./build.sh
You can, of course, edit your PATH by adding ";." at the END of the current value. In mingw64_shell.bat do
SET PATH=%PATH%;.
Note: I'm looking at my MSYS2 installation, so you may need to look for the ".bat" file that launches bash. The biggest security problem with the WINDOWS default path that had "." in front of the path is that it allows a user to over-ride a system executable -- that would make your scripts do some weird stuff, especially if you import them from someone else.

How can i make ZSH use the latest git version?

I am using ZSH with oh-my-zsh on OS X.
Today I used hombrew to update to the latest version of git (1.8.something).
However, if I run
➜ ~ git --version
git version 1.7.10.2 (Apple Git-33)
I see that still an older version is used. On bash everything works fine and the latest version of git is called.
Since I am new to ZSH, any advice on how to set up ZSH to use the "new" git is appreciated!
Best,
Tobi
This means that your $PATH variable isn't set up to include the right git (and everything else homebrew installs).
Try doing echo $PATH from both bash and zsh. You should see at least one difference: the directory where you installed homebrew, probably /usr/local/bin. (It'll either not be in there, or be after /usr/bin, where the Apple-supplied binary lives.)
To fix it, add a line like
export PATH=/usr/local/bin:$PATH
to your ~/.zshenv.
If the PATH modification didn't instantly work, you need to realize that with zsh you need to type "rehash" for zsh to recognize there are new executables in the path. Or just log out and back in.
Compare the outputs of which git (and the outputs of echo "$PATH") in bash and zsh.
The directory containing an up-to-date git is probably not present in $PATH variable for zsh, but it is in bash. It's likely caused by $PATH items being added in your ~/.bashrc and/or ~/.bash_profile file, which zsh doesn't source on startup. If it's so, add the same assignment to PATH to your ~/.zshrc

Resources