Understanding Git for Mac Users

Lauren Esser
Analytics Vidhya
Published in
7 min readJan 14, 2021

Don’t mock me, but for some reason whenever I see or hear the word “git” the song “Get Down Tonight” by KC & The Sunshine Band automatically plays in my head. Am I the only one? Most likely. Within this article I will be going over Git for Mac users, from set up to functionality. Let’s get started.

Image from thecurrent.org

The Installation Process

I used the site https://git-scm.com/download/mac to first understand how to download git for my Mac book. I already had Homebrew on my computer, therefore, all I did was go into my terminal and type:

brew install git
Image taken from my computer

If you do not already have Homebrew on your computer another option is to go to https://sourceforge.net/projects/git-osx-installer/files/ and click the green button that states “Download Latest Version”

Screenshot from my computer

If you believe that you have followed the above steps correctly, next type:

git -- version

into your terminal and press enter. It should return with git version 2.15.0 (or whichever version you just downloaded). Congratulations, you now have Git installed on your Mac book!

Before we continue with explaining Git we need to configure your Git username and email. Follow below steps in your own terminal:

git config --global user.name "Your name goes here"git confit --global user.email "Your_email@address.com"

The Good to Knows

So you have set up git onto your Mac book. Well, what now? Let’s cover some of the most important aspects of git that you will use on a daily basis.

cd

cd stands for change directory and is how you will move from different folders within your terminal. If I wanted to move into a folder called “github” that is within my “Documents” folder I would code:

cd Documents/github

Notice that behind my username it now says github. This shows that I am in my github folder.

Another way to identify where I am within my computer is to use:

pwd

pwd stands for “print working directory” and will state exactly where I am within my computer.

Notice I am currently in /Users/laurenesser/Documents/github. If I accidentally moved into the wrong folder I can move back to base by typing in “cd” again to move back to base. To show this I typed pwd to print the working directory.

Once you have moved into whichever folder you’d like it is now time to create or clone your first git repository.

Creating your own repository:

To create an empty Git repo within a specified directory you have two options.

  1. Navigate to the directory of choosing then type:
git init

2. Specific which directory you want to initialize in after git init:

git init <directory>

For more specific information on initializing a repository I highly recommend you take a look at git’s documentation on https://git-scm.com/docs/git-init

Cloning an existing repository:

Cloning an existing repository means to take the repo located at www.fullwebsiteurl.com and bring it to your local machine. When cloning a repo there are two things to think about.

1. Is it your repository already? If so copy the link and clone it to your local system.

Above I copy and pasted the link from my “Better Stock Market Prediction” project and cloned it onto my local system.

2. I do not own the repository, but would like to “fork” it and work on it.

Go to the webpage of the repo you would like to fork. Click fork in the top right of screen.

Screenshot from my computer

Once you have Forked the repo, you should see your user name before the title of the git repository. Copy the url and paste it into your terminal after the words “git clone”

You now have your own copy of the repository on your local machine.

Opening repository into Jupyter Notebook:

Now that you have successfully cloned or initialized your repository, it is now time to open the repository up into your Jupyter Notebook. To do this type “jupyter notebook” into your terminal and press enter. Your terminal should automatically open up a webpage into your correct Jupyter notebook.

Saving your work to Github:

When you are finished making changes in your Jupyter Notebook, you will need to “Save and Checkpoint”, then “Close and Halt” your notebook to save changes.

Screenshot from my computer

When your notebook closes you will return back to the jupyter notebook main screen. Click “Quit” in the top right corner.

Screenshot from my computer

Now your notebook is officially closed. In order to have these changes show on Github you will need to return back to your terminal to add the changes, commit them, and finally push them onto Github.

git add

git add is used to stage all changes in the directory or repo for the next commit. The easiest way to add changes is to type “git add .” Adding the period after git add simply means to add ALL the changes you just made in your directory. This will not always be the case, at times there may be files that you want to keep locally and do not want to push to Github. We will talk about those in another blog.

git commit -m

git commit is used to commit all of the changes you made and the -m is to add a commit message. The commit message should be a brief description of the changes you made.

git push

git push is the process of pushing the remote work to Github and updating the online repository. You now have all of the work you have done locally saved to Github.

Other important items:

ls

Let’s say you are in a folder within your terminal and cannot remember the name of a specific file. By typing “ls” and hitting enter. You will list all the files in your current working directory. Another option to help you remember the files name is to hit tab. If I were to type “cd r” then tab, it will show me all the files within the current directory that begin with r.

git status

List status is usually used when you are finished working in your notebook. By typing “git status” and hitting enter you will see which branch you are on, commits ahead of master, files that are staged, unstaged, and untracked.

git reset

I want to take a minute and discuss “git reset” because this can be so beneficial when used right, but so detrimental when used wrong. Sometimes you might accidentally “git commit” an item that you did not intend. For example, lets say you have a really large file that cannot physically be pushed to Github. In order to fix this issue, you will need to reset the staging area back to the past commit. There are two different types of git resets.

  • git reset — soft: If you are not sure which reset to do, always start off with a soft reset. This preserves changes done to your files, but removes your last commit. By doing this, you can now add that large file to your .gitignore and then redo your commit.
  • git reset — hard: This is where I accidentally deleted hours of work from my computer. A hard reset changes the staging area, working directory, and overwrites all changes that you had just made. Aka you are going back to whatever your last push to Github was.

git reset is a powerful tool for when we make an error in our commits, just ensure you are using the correct one!

When using Git, you won’t only be using it for yourself. You may be working as a team to accomplish a specific goal. Perhaps a divide and conquer route.

Image from nobledesktop.com

Git has an item called branches that allows you to take a copy from the master and make your own changes to later merge back with the master. To create your own branch you would type

git checkout -b <nameofbranchhere>

Here you are creating the name of your branch, as well as moving to or “checking out” your branch to work on. When you are finished with the work you will still do a git add and git commit -m. Now instead of pushing your work back to Github you will “git merge”, which allows the changes you made to be integrated back into the master branch.

Today we went over the need to know introductory to understanding Git. I encourage you to look at the extra resource below for other commands in Git and maybe even try to test them out on your own.

Extra Resource: If you are new to using git I HIGHLY recommend heading over to https://www.atlassian.com/git/tutorials/atlassian-git-cheatsheet and downloading the Git cheat sheet. This would have helped me avoid a lot of issues just starting off

From https://www.atlassian.com/git/tutorials/atlassian-git-cheatsheet

Reference:

https://git-scm.com/

--

--