How to enable auto-completion with Git [all platforms] - bash

How to enable auto-completion with Git?
When using Git on different platforms, auto-completion is not always enabled/available.
Note that this question is NOT a duplicate of git auto-complete for branches on the command line, because that question was only about Apple users. This question is mostly the same, but including other platforms too.

You need to get bash's programmable completion configured to return the possible commands. You can see the "Programmable Completion" section in the bash man page if you want to implement it yourself.
A much easier method to use a script to pre-configure bash's programmable completion. On most distributions, the file for git completion, /etc/bash_completion.d/git (on Fedora and derivatives), /usr/share/bash-completion/completions/git (on Debian and derivatives) (other distributions might have it in other places, try locate or find to locate it) are provided by the git package. (If it is not included with your git package, you can get it from here).
You can configure bash to source the file when it is started (probably using .bashrc), or you can install bash-completion, which will automatically load completions like the one that the git package installs (and generally completions from /etc/bash_completion.d/ or /usr/local/etc/bash_completion.d/ (typically BSDs)). On most distributions, bash-completion should be available in the repositories and you can simply install the package. For other systems, see the instructions included in the README.md to install it. (On non-GNU/Linux systems, some completions might need to be disabled to prevent it from outputting errors when starting)

Apple users
See question git auto-complete in the command line
Linux users
Solution tested with Debian 9 (Stretch)
sudo apt install bash-completion
Pros:
Very easy for newcomers to Linux/scripts
No "manual" downloading
Only makes use of the official repos: the "safest" way.
Easy to update: sudo apt update then sudo apt upgrade
I also know that the question was asked for Apple users, but it shows up first in Google for "git auto-completion branch", so I thought a Linux answer would still be useful.

Related

How to update Git Bash?

I want to update bash to the latest version. How to do that? I am on a Windows 10.
$ bash --version
GNU bash, version 4.4.23(1)-release (x86_64-pc-msys)
I tried:
bash --Update
didt work
bash --self-update
didnt work
Bash does not have the ability to update itself!
Software updates on a computer are limited to a super user. But Bash can be used by everybody. It does not make much sens to put the update functionality into a program, everybody can use, although only super users are allowed to do the update. This is the status quo on Unix, where Bash came from. On Windows, which came from DOS, a single user OS, the situation may be different.
Inventing non existing and not documented command line options is unrewarding. Read Bash's manual to know, which command line options are available.
It is the work of a package manager to install software and to update software. Almost every operating system has its own package manager. Even on Windows there are at least two different package managers.
If you use MSYS2, then you can update with pacman.
If you use Cygwin, then you can use its setup.
BTW: The question is off-topic here. Try it at superuser.com.
Git bash updates are not managed on windows. You are supposed to download and reinstall on each version.
see https://superuser.com/questions/1260587/how-to-disable-git-bash-daily-updates-checking
You can have a "update check " option, but no real update.

bash conda autocomplete on macos

I would like to enable auto complete for conda commands when I press tab.
I have searched the internet and find some documentation. I have applied the proposed solution, but auto complete is still not working.
conda.io: enable-tab-completion
Is there any other approach. How could I enable autocomplete for conda commands?
Autocompletion is now possible with conda-bash-completion. Just install it by entering the following in your terminal:
conda install -c conda-forge conda-bash-completion
The developer provides more thorough instructions on the project's github page.
Autocompletion for conda is not supported in bash right now.
Conda versions up to 4.3 supports tab completion in bash shells via
the argcomplete package. Tab completion is deprecated starting with
version 4.4. See issue #415.
It will technically work with versions prior to 4.4, but I will not recommend using versions so old as major changes were introduced since then, and the development pace of conda is quite fast.
Three possible solutions:
this issue states completion works for Fish shell, so you might use fish
the same issue suggests someone could implement autocompletion for Bash... if you're looking for a project contributing to conda. I believe the conda team will gladly point you to the right direction for this.
there is mention of a hack for Bash, although this is a few months old with no guarantee it will work, and could break at any time
To enable tab completion:
Make sure that argcomplete is installed:
conda install argcomplete
Add the following code to your bash profile:
eval "$(register-python-argcomplete conda)"
Test it:
Open a new terminal window or an Anaconda Prompt.
Type: conda ins, and then press the Tab key.
The command completes to:
conda install
Source:
https://conda.io/projects/conda/en/latest/user-guide/configuration/enable-tab-completion.html

How do I install Git for Windows software to a specific directory?

I have just downloaded the latest Git for Windows installer, v2.4. It appears to want to install to the standard Windows "Program files" (with-spaces-in-name) directory.
Since I have all my development code in a folder called (simply) "/bin" -- I want to see if there's a command line option or parameter to change the install directory.
In my case, these days I use a environment variable such as GIT_HOME for important software like git; so it would be useful if there was a way to apply that to things like git commands, etc once I have the program installed.
possibly related:
How do I change the directory in Git Bash with Git for Windows?
I also came across a few questions asking: "whereis git". That's answered above, however I take that as an indicator that others may want git somewhere else too.
To start the installer with a different installation path you can open a CMD terminal in the same directory as the installer executable and pass in an option parameter of /DIR="x:\dirname"
For instance, if you have version 2.17.0 for Windows 64bit and you want to install git to D:\git, you would run:
Git-2.17.0-64-bit.exe /DIR="D:\git"
The installer will launch as usual and you need to walk through the other options, but the install location will be the path specified.
Since I just ran into this problem because my SSD is filling up, I figured I'd share the solution I came to on Windows 11 with Git v. 2.37.2.
The best way I could figure was to uninstall Git, then in CMD Prompt use the suggested command from the Git website with an appended --location/ -l flag:
winget install --id Git.Git -e --source winget --location [drive:/directory]
where [drive:/directory] is your target for the install. Had no issues and verified it worked with a project.

How can I implement a fast install of git using sh?

I need to get a classroom full of students to install git on their computers quickly and during a lecture. I've been inspired by the installation process of meteor to try and implement something similiar, i.e:
curl easy-install-git.com | sh
I know there already exist binaries and easy ways to install git, but those are all multi-step processes and this would be one simple command in terminal. How do I write an sh script to do this? (Or better yet, does one exist already?)
Any other easy ways to do a git install on a clean machine would also be appreciated.
5 years later, the easiest/fastest way to install Git on Mac is to type:
git --version
If you don’t have it installed already, it will prompt you to install it.
But that is an old Apple fork of Git (2.10 at most)
The git-osx-installer dmg files are up-to-date, with Git 2.18.
As seen in this script, it can be curl'd.

Install Git separately from Xcode

Firstly apologies if this is a really simple question but Git is absolutely brand new to me. Basically I've been dabbling with a bit of iOS development for a little while and now have a project idea that I want to start working on and for the first time I'm planning on putting this into source control.
Now I know Xcode 4 has some Git integration but I've spotted a couple of posts that suggest the integration isn't that great and it's better to do it the command line way.
So, I've been following instructions here http://git-scm.com/book/en/Getting-Started-Installing-Git and I've downloaded version 1.7.10.3 for Mac and ran the install.
However when I run git --version I get version 1.7.4.4 which is the version that was installed with Xcode I assume?
My question is, how do I make use of my new version 1.7.10.3? The plan is to learn to use Git via command tools and set up a central repository on a server I have at home, then have local copies on my laptop.
As I say I'm really new to this, so a quick step by step idiots guide would be helpful. I have searched and tried to find details here and on google but other than the suggestion to use Git outside of Xcode I've not found out exactly how to do it.
Any advice, or links to a decent tutorial that covers this would be very much appreciated.
Update:
I've changed the path to the new path in .bash_profile (/usr/local/git/bin) and now get git version 1.7.7.5 (Apple Git-26). This still appears to be the wrong version (although this could be me being dense). I downloaded the installer for OSX from the above link. Should I be doing something else. I want to play around with the command line tools so I can get a proper understanding
Update 2:
I'm now also trying MacPorts as recommended in the article. This seems to be downloading a lot more than just Git though
Xcode installs git in /Applications/Xcode.app/... but if you run 'Install Command Tools' then git gets installed also in /usr/bin/git. Xcode uses its private version in Xcode.app. If you installed git in /usr/local/git/bin then you need to include that directory in your PATH. You should have a .bashrc file (or equivalent for whatever shell you are using). Add
PATH=/usr/local/git/bin:$PATH # your shell might use different syntax.
to that file 'rc' file.
Also, note that Xcode would allow you to accomplish your goal of setting up a remote at home while maintaining a local copy on your laptop. Go to the Xcode 'organizer' and click 'repositories'. There you will find all your machine's Git repositories. Choose one, select the 'remotes' folder and then '+' to add your home remote. I use Xcode this way.
Credits goes to #GoZoner for the answer. I'm just summing up everything.
Download official git installer from : http://git-scm.com/download/mac
Install and add its directory to your path :
echo "PATH=/usr/local/git/bin:\$PATH" >> ~/.bash_profile
source ~/.bash_profile
The best way is:
step 1:
Install homebrew on your mac this is the official website Homebrew homepage
From a Terminal prompt:
/usr/bin/ruby -e "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/install)"
step 2:
From a terminal prompt
brew install git

Resources