DEV Community

Squademy
Squademy

Posted on • Updated on • Originally published at squademy.Medium

Install and configure Git on a fresh new macOS 11 Big Sur in 2021 — A complete guide

Install Git on a fresh macOS 11 Big Sur

1. Install XCode 12 (optional)

Xcode is an IDE for macOS containing a suite of software development tools for developing software for macOS, iOS, watchOS and tvOS. Even if you're not a full time professional Apple developer, we still recommend to install it on your brand new laptop/iMac. Xcode has some long term benefits that you'll need in the future.
Xcode 12 is around 11 GB and downloading it though Apple Store might need some times, depends on how fast your Internet connection is.

2. Install Xcode Command Line Tools (required)

$ xcode-select --install
Xcode CTL will bring Git to you systems, along with other tools to build and compile software packages such as: GCC, Make..
When it's done, to test that it installed properly you can run:

$ git --version
git version 2.30.1 (Apple Git-130)
Enter fullscreen mode Exit fullscreen mode

And $ which git should output /usr/bin/git
Another way to install Git is to install Homebrew and then install Git though brew commands. Homebrew calls itself The missing package manager for macOS and is an essential tool for any developer. We highly recommend it.
To install HomeBrew:

/bin/bash -c "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/install.sh)"
Enter fullscreen mode Exit fullscreen mode

For a complete guide on install Homebrew on Mac M1 ARM, please read this article

Now install Git via Homebrew with $ brew install git
When done, to test that it installed properly you can run:

$ git --version
Enter fullscreen mode Exit fullscreen mode

And which git should output /usr/local/bin/git.

Notice that the Git version installed from brew is likely more update-to-date than the Git version in Xcode CTL. Homebrew always put it things on /usr/local/bin/ path which owned by a local user, in stead of the /usr/bin/ in Xcode CTL which owned by the macOS system.

Git setup at global level

1. Set global user.name and user.email

$ git config --global user.name "Your Name" 
$ git config --global user.email "your_email@your_company.com"
Enter fullscreen mode Exit fullscreen mode

The global Git configuration file is stored at $HOME/.gitconfig on all platforms, i.e: ~/.gitconfig. You shouldn't need to manually tweak this file, unless you particularly want to. Use this command to open up Vim editor with that file loaded, if you're keen:
$ git config --global --edit

2. Enable Git password caching

Let's macOS save the Git credentials on its keychain, so that you won't have to type Git passwords many times:

$ git config --global credential.helper osxkeychain
Enter fullscreen mode Exit fullscreen mode

3. Only allow git pull in fast-forward mode

$ git config --global pull.ff only
Enter fullscreen mode Exit fullscreen mode

It's a good practice and if you're wondering why, here is the reason: https://blog.sffc.xyz/post/185195398930/why-you-should-use-git-pull-ff-only-git-is-a

4. macOS globally ignored files

On a Mac, it is important to remember to add .DS_Store (a hidden macOS system file that's put in folders) to your ~/.gitignore files. If you want to never include .DS_Store files in your Git repositories, you can configure your Git to globally exclude those files:

# specify a global exclusion list:
$ git config --global core.excludesfile ~/.gitignore

# adding .DS_Store to that list:
$ echo .DS_Store >> ~/.gitignore
Enter fullscreen mode Exit fullscreen mode

If you would like to use some ready-to-use file, consider Github's default .gitignore for macOS environment:

$ curl https://raw.githubusercontent.com/github/gitignore/master/Global/macOS.gitignore -o ~/.gitignore
Enter fullscreen mode Exit fullscreen mode

5. Force Git to use https:// instead of git://, or vice versa

Some package manager, such as NPM has package.json file fixed with SSH protocol and you might behind a corporate firewall that does not allow SSH..

"dependencies": {
    "package_name": "git+ssh://git@git.scm.domain.com:Domain/package_name.git",
}
Enter fullscreen mode Exit fullscreen mode

The only option is to force Git to change its protocol to HTTPS for you to be able to install the package - notice the semicolon in git@github.com: must be included

$ git config --global url."https://github.com/".insteadOf git@github.com:
$ git config --global url."https://".insteadOf git://
Enter fullscreen mode Exit fullscreen mode

If you want to switch it back:

$ git config --global url."git@github.com:".insteadOf https://github.com/
$ git config --global url."git://".insteadOf https://
Enter fullscreen mode Exit fullscreen mode

6. Global Git config file - final result

Final ~/.gitconfig will be something similar to this:

[user]
        name = Your Name
        email = your-email@your-company.com
[pull]
        ff = only
[core]
        excludesfile = /Users/user1/.gitignore
[credential]
        helper = osxkeychain
[url "https://github.com/"]
        insteadOf = git@github.com
[url "https://"]
        insteadOf = git://
Enter fullscreen mode Exit fullscreen mode

Summary

# edit and list global Git config:
git config --global --edit
git config --global --list
# edit and list local Git config
git config --local --edit
git config --local --list
Enter fullscreen mode Exit fullscreen mode

Top comments (0)