DEV Community

Cover image for GIt on Mac with Homebrew
Abel Noriega
Abel Noriega

Posted on

GIt on Mac with Homebrew

Introduction

Git is a version control system that allows developers to track a project and actively contribute without interfering in each other’s work.

It supports collaboration within a project and helps prevent miscommunication or code clashing between team members. The system tracks and saves snapshots of an evolving project, essentially keeping a history of the development.

Users who install the software on their machines can communicate with each other through the system. An even better solution is collaborating over a centralized source (for example, GitHub) where developers can push and pull changes on the cloud.

Instruction

Run the following brew command in the terminal:

brew install git
Then, check the Git version to verify the installation:

git --version
Install Git Using MacPorts
If you are using MacPorts to manage your packages on the system, you can use the port command to set up Git.

Start by updating MacPorts with the command:

sudo port selfupdate
Search for and install the newest Git ports and variants by running the following two commands:

port search git
port variants git
Then, install Git with:

sudo port install git

Configure Git

The next step is to configure Git by adding your credentials to the system. This is important as it helps keep track of which user is committing changes to a project.

Open the terminal and configure your GitHub username:

git config --global user.name “your_github_username”
Then, add your email:

git config --global user.email "your_email@github.com"

Track and Commit Changes

To demonstrate how to work with files on local Git repositories, we are going to create a demo folder and file to work with.

1) First, open the terminal and create a new folder named NewFolder.

mkdir /Users/[username]/Desktop/Tools/Git/NewFolder

2) Then, move into that directory. The path may differ according to the location where you created the new folder.

cd /Users/[username]/Desktop/Tools/Git/NewFolder/

3) As we want to keep track of changes inside this folder, we need to create a local Git repository for it. Running the git init command initializes an empty git repository in this particular location. Therefore, run the command:

git init
With this, you have added a hidden folder inside the directory by the name .git.

Note: To see the hidden .git folder, you need to run the command: defaults write com.apple.finder AppleShowAllFiles YES. If you want to hide the folder again, modify the last part of the command by changing the YES to NO).

4) While in the directory NewFolder, type the following command:

git status
This shows the state of the working directory and displays if any changes made inside the directory.

Since the folder we created doesn’t have any files in it, the output responds with: nothing to commit.

5) Add some files inside NewFolder and see how the git status changes:

touch file1.txt

6) Check the status again:

git status
The output tells you there are untracked files inside the directory and lists file1.txt. Git is tracking the folder in which the file was added, and notifies you that the changes are not being tracked.

7) Prompt Git to track the new file by running:

git add test1.txt
If you recheck the git status now, you would see that the file is now being tracked (as it changed from red to green). However, you still need to commit this change.

8) Commit all changes and add a message that describes the commit:

git commit -m "added test1.txt"
Now, the output tells you the working tree is clean, and there is nothing to commit.

That's all!

Top comments (0)