Git Setup for Mac Users. Mac users, let’s setup Git the right... | by Joseph Robinson, PhD | Towards Data Science

I stumbled upon Medium a few months ago — initially, I found the content broad in scope and material keen on quality — already there have been significant improvements found in blogs and overall interface. With that, and as I configure a new iMac for the lab (i.e., SMILE Lab — more on this later), I figured I would record the process followed to install and configure Git. I intend to make this my first of many blogs; nonetheless, I figured this simple tutorial serves as an excellent way to “break the ice”. I hope you enjoy it!
Let’s now move on to some good old fashion Git for Mac — We will get your Mac machine set up properly.
Install Git
Initial Setup
Git Styles in Terminal
Git Autocomplete
Git Ignore
Git Aliases
Git Authentication
Desktop (GUI) Applications
The first thing is first — installation. This is quite simple. It can be done a couple of different ways. For the sake of having the brew package manager installed, I recommend using Homebrew.
Open the terminal and install Homebrew by running the following command:
/usr/bin/ruby -e "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/install)"
Then install git
brew install git
— or —
The easiest way to install Git on a Mac is via the stand-alone installer:
Download the latest Git for Mac installer.Follow the prompts to install Git.
Download the latest Git for Mac installer.
Follow the prompts to install Git.
Open a terminal and verify the installation was successful by typing and running the following in the terminal
Open a terminal and verify the installation was successful by typing and running the following in the terminal
git --version
2. Configure your Git username and email using the following commands, replacing Ava’s name with your own. These details associated with any commits that you create:
git config --global user.name "Ava Paris"git config --global user.email "aparis@domain.com"
3. (Optional) To remember your Git username and password when working with HTTPS repositories, configure the git-credential-osxkeychain helper.
There are many styles for display. Regardless, let’s keep it simple and neat — setup a Git color scheme and branch information displayed in the terminal.
Colors:
Unless you are reading this from a monochrome display, let’s take advantage of some of the color features for git. Most definitely, specific components of Git are best displayed in color, making it easier to identifier different components and, thus, more comfortable to read.
From the terminal run the following set of commands (i.e., from any folder):
git config --global color.status autogit config --global color.branch autogit config --global color.interactive autogit config --global color.diff auto
Mac terminals can be configured to use colors to improve the display. For this, copy and paste the following into file ~/.gitconfig.
[color]	branch = auto	diff = auto	status = auto[color "branch"]	current = yellow reverse	local = yellow	remote = green[color "diff"]	meta = yellow bold	frag = magenta bold	old = red bold	new = green bold[color "status"]	added = yellow	changed = green	untracked = cyan
Repo and branch listing:
One of the best customization for Git, in my opinion, is displaying branch information in the terminal.
For this, simply add the following lines of text to ~/.bash_profile file:
parse_git_branch() {git branch 2> /dev/null | sed -e '/^[^*]/d' -e 's/* \(.*\)/ (\1)/'}export PS1="$NO_COLOUR[\t] $BLUE\u$SSH_FLAG:$YELLOW\w$NO_COLOUR: \n$GREEN\$(parse_git_branch) $BLUE\\$ $NO_COLOUR"
Now, in the current repo, the prompt would look like the following:
[jrobinso@blackhawk matlab (master)]$
Autocomplete via tab key can be a handy feature to have. However, autocompletion is not set up automatically on MacOS. Fortunately, setting this takes only a couple of easy steps.
First, install the required brew package as follows:
brew install bash-completion
Next, add the following snippet of code to ~/.bash_profile file.
source /usr/local/etc/bash_completion.d/git-completion.bash
Either open a new terminal or run source ~/.bash_profile and enjoy!
Some files or file-types are typically not added to the repo. Thus, the ‘status’ display printout would be cleaner if these files were omitted. All that needs to be done for this is to create ~/.gitignore and add. For starters, let’s create a file with the following contents:
.DS_Store
Note, ~/.gitexcludes also work per project, in which case the file exists in the root directory of the repo. Nonetheless, .DS_Store should be ignored globally per the instructions above were followed.
Aliases, especially for Git, are typically worthwhile! If there is nothing else, at least add this feature to your global Git configurations. Notice I referred to the configurations as being ‘global’ — ~/.gitconfig are configurations applied system-wide, and project-specific configurations get handled in .gitconfiglocated in the root of a repository (i.e., <repo>/.gitconfig).
For aliases, typically makes the most sense to add to global configurations. If needed later, then add unique alias to a specific project. Perhaps worth noting that alias, or all Git configurations for that matter, are first set using local configurations, and then globally if not specified in locally. In other words, let’s say you have the same alias defined in local and global configurations, then the local definition would be used in for that specific repo.
Regardless of local or global, but with global is recommended, add the following [alias] block to .gitconfig:
[alias]  st = status -uno  ci = commit  br = branch  co = checkout  df = diff  lg = log -p  lgg = log --graph --pretty=format:'%Cred%h%Creset -%C(yellow)%d%Creset %s %Cgreen(%cr) %C(bold blue)<%an>%Creset' --abbrev-commit --date=relative  f = fetch
Now try + add additional aliases whenever seen fit... it’s as easy as that.
[jrobinso@blackhawk matlab (feature_branch)]$ git co master  Switched to branch 'master'[jrobinso@blackhawk matlab (master)]$ git st # On branch master nothing to commit (use -u to show untracked files)[jrobinso@blackhawk matlab (master)]$ git f # <fetch output>
Often an SSH key is needed for authentication purposes.
For this, generate a key from the terminal:
[jrobinso@blackhawk ~]$ ssh-keygen
Hit return a couple of times (i.e., leave passcode blank).
Next, copy SSH key to clipboard via
[jrobinso@blackhawk ~]$ cat ~/.ssh/id_rsa.pub | pbcopy
Finally, paste the key into settings page on repository host(s).
Considering Git does have a tad bit of a learning curve, even though the best way to overcome this is to get comfortable with it at its core (i.e., from the terminal), perhaps a GUI would be preferred by some. With that, I figured it was worth mentioning GUI-based Git repo tools. For this, two options stand out: SourceTree and Github Desktop App.
SourceTree has the advantage of working with repositories from various hosts (e.g., Github, Bitbucket, etc.), while Github Desktop App is Github specific.
Properly setting up a development environment and first-and-foremost in most projects. By taking a few minutes to complete this tutorial, Git version control is now correctly set up on your machine to enhance and optimize your throughput.
For those new to git or not comfortable in the terminal, perhaps a desktop application would suit you best (e.g., SourceTree or GitHub Desktop App). These provide many features, tools, and visualizations in a GUI. Regardless, the terminal is a must in some situations. Plus, a better understanding of Git and version control would be acquired if one faced the learning curve that comes with using it from a terminal.
In any case, whether an expert or beginner, your system should now be ready to use Git. I hope you enjoy it!
If something is missing or could be improved, please share below — whether a Git alias you could not live without, a neat feature, or an aspect that could be improved or is outdated. Of course, all questions are also welcome!
Thank you for reading my first blog — hope it was enjoyed, feel free to provide feedback on how future blogs could be improved.
Checkout version for macOS Catalina 10.15.3 [ blog ].