You are a new developer who needs to start working with Git (on a Mac) and I am a “senior entry-level” developer who can hopefully help.

Here we go!

Have you installed Git?

Pull up a terminal and run the following command

git --version

if you get a version number back you are good to go.

Git not Installed?

If you saw the git version then skip this section. Otherwise here is how you can install git on a mac.

You can install git using brew. But before you install brew check if you have it already. For that you can just use the –version command again. So run the following to see if you have brew installed already.

brew --version

Here is how you can install if you don’t have brew installed. Run the following command.

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

Once you have brewed installed you can install git.

Run the following command to install git

brew install git

There is a possibility you will run into weird issues trying to install git or even brew. One possible problem you might run into is permission related stuff. Sometimes you can those resolved by just running the command with sudo . So you would just prepend the original command with sudo. e.g. sudo brew install git

Git is finally installed, now what?

Set your username and email

You must tell git your username and email. But before you do that you might be wondering if you had already done so in the past. In that case run the following commands to see if your username and email have been set already.

git config --get user.name
git config --get user.email

If you see your username and email then move on from this section otherwise run the following commands

git config --global user.name "Your Name"
git config --global user.email "yourname@example.com"

SSH Key stuff…Uggghhh

This part sucks in my opinion. You have to create a (SSH) key and tell GithHub about it so it knows it’s truly you whenever you want to ‘save’ your code.

Let’s see if you have a (SSH) key already. To do so run the following command

ls ~/.ssh/id_rsa.pub

If you don’t see a …No such file or directory message you are good.

So you WANT to see something like this

and NOT this

I purposely misspelled the id_rsa.pub name so I can show you what message you would get.

If you don’t have key as in you got the No such file or directory message then do the following:

Run this command replacing <email> part with your email. Don’t include the brackets!

ssh-keygen -C <youremail>

It will ask you a question, just hit enter then it will ask you for a password. Enter one if you like. You don’t have to.

Once you are done creating a SSH key. Copy the content by running the following command and then copying what is being outputted to the screen.

cat ~/.ssh/id_rsa.pub

Make sure to copy every character from beginning to end!

Then hop over to GitHub.com, login, go to settings, SSH and GPG keys, hit ‘New SSH key’, give a title paste the key that you had copied and finally hit the ‘Add SSH Key’ button.

To make sure it worked you can run the following command:

ssh -T git@github.com

If you get a message that looks like the message below you are good to go!

Git SSH success message

I highly recommend you take a few minutes to contemplate on why you chose to do this crap to begin with!

Git Commands

Git is one of those things you will for sure underestimate. Just when you think you get git you realize there is more to it. So with this guide I am trying to help you focus on the most important and commonly used commands. Let’s get started.

Git init

git init will tell the current directory that you want to track it’s content through git. You will use it ONLY once at the very beginning. It’s initializing the current directory to be a git repository. This command will create a hidden folder .git that hold ALL git related info. If you delete this folder the current directory will no longer function as a git directory.

Git status

Oh boy oh boy I cannot stress how important this command is. If git had ten commandments this would be commandment number 1. You shall use git status every-time you want to take a git action. It will tell you what branch you are on, what has changed, what has been deleted and what has been added. If everything is up to date you should receive a message such as the one below.

The Famous Git Combo (add, commit, push)

The most traditional way to push your code up to GitHub has been by using the three famous git add . && git commit -m "what has changed message here" && git push origin main

commands. You will use them whenever you are ready to push changes up to your remote GitHub repository. You can run them individually or run them on one line by using the two ampersand (&&) in between them.

You will run into issues at some point trying to push up changes. You might have specified the wrong ‘origin’ or target branch. You might have to pull some remote changes first. You might have forgotten to initialize the repo first. The list goes on and on what could go wrong. Most of the time you can do a simple google search to find out what is going on. Other times you have to reach out to coding tutor to get some help.

Git clone

This command is used if you want to get somebody else’s (or your own) code on your local computer. It’s kind of like downloading code off GitHub. All you need is the public url of the GitHub repo.

Git Repo URL

Now that you have the public URL you can run the following command to clone the repo:

git clone [PUBLIC GITHUB REPO URL HERE]

This will create a folder in the current directory and ‘download’ the code into it. Keep in mind that the repo is still associated with the original author. You can’t push up to the original repo unless you have the right permissions to do so. To make the code your own you either delete the hidden .git directory and initialize a new repo or change the url to the remote ‘origin’.

Git pull

Wanna get the latest changes that somebody else might have added to this repository? You can do by doing a git pull origin main

Keep in mind with most of these commands I assume you are on the main branch. For older repos use the older branch name ‘master’.

Git remote –v

Want to see what remote repository is associated with your local directory. You can run git remote -v to see all remote repositories. I sometimes have to run this to make sure I am not associated with somebody else’s repository but rather my own.

Git remote

Do you want to add a new remote repository? There could be several reasons why you might want to do that. Let’s say you created a new repository and have local code that you want to push up to it. Or similarly you cloned somebody else’s repo, deleted the hidden .git directory and now want to push the code to a newly created repo that you created.

So here is the command:

Git remote add origin [REPO URL HERE]

Now your local (git) directory has remote repo called origin with a URL you specified.

Git diff

Made some changes to your code? Try running git diff

Pretty cool, huh? Git diff will show you all the changes you have made and where you have made them. I occasionally have to use this command and it usually clears up any confusion on what changes I have made. Obviously this command assumes you have git setup correctly and the current directory is a git repository.

Git log

Want to see your git commit history? As in do you want to see all the times you committed your repo? Run git log to see your history. It will show the git commit message along with the commit id for each commit. You can the id to reset back to that version of your codebase. Which brings us to the next and final command.

Git reset

Use this one with LOTS of caution. I do not recommend using this command purely based on this article. As in if you have to use this command do another google search just on this command.

With that said git reset can be used to go back to an older version of your code base. So run git log to find the version you want to go back to. Grab that long string with characters and numbers. Then run the following command to go back in git ‘time’.

git reset --hard [GIT ID HERE]

Everything should have changed back to that version of the code base.

Phew. Git is weird and difficult to become an expert in. In addition the internet doesn’t seem to be of much help when it comes to this topic. So if you are a new developer and starting off with Git just keep is basic! Stick to git push, pull, status, clone as much as you can. You will have plenty of time and chances to try more advanced git techniques especially once you are working on a team within a professional environment.

Happy coding!