loading...
Cover image for How to properly set up Git on your computer!

How to properly set up Git on your computer!

landonp1203 profile image Landon Patmore Updated on ・7 min read

Installing Git

OSX Setup

If you have never used Git on your computer, you most likely will not have it installed. Pull up your terminal and type in git. It should prompt you that you need to install Xcode Command Line Tools to actually install Git. Once it is done downloading, Git will be properly installed on your computer and you will now be able to use it from the command line.

Windows Setup

Windows does not come with Git, nor does it come with a built in way like OSX to install it without having to install additional software. Go to this link: Download Git which is a link to the installer download. Go through the steps and keep the default settings as there is no reason to change them, unless you seriously know what you are doing. This will install Git Bash which is a terminal emulator for Windows which will allow you to access Git.

Side Note: While you may be able to use Git through the command prompt, I would highly recommend using Git Bash as it gives you information about the branch you are working on right in the terminal compared to command prompt which does not tell you any information, as well as you can use Linux commands.

A kind user has told me that you can install a Plugin for PowerShell (Command Prompt) which will allow you to see branch information and commit differences just like Git Bash! To install, you must have Git installed already, and type in the following command in Command Prompt: Install-Module posh-git -Scope CurrentUser. Here is the link to the repository if you are having issues with the plugin for any reason: dahlbyk/posh-git.

Linux

If you are running Linux, you most likely know what you are doing and have used Git before. But if you haven't, go to your terminal, and type in git. Linux usually comes with Git so it should already be there, but if it is not, type this into the terminal: sudo apt-get install git. This will install Git, and you will be able to go from there.

Setting Up Git

There are a couple steps to get you up and running properly with Git so there are no hiccups. For these steps, it is assumed that you are either in Terminal (OSX/Linux) or Git Bash/Command Prompt (Windows). The steps are as follows:

  1. git config --global user.email "GIT SERVICE EMAIL" - Sets your email globally for Git.
  2. git config --global user.name "GIT SERVICE NAME (NOT USERNAME)" - Sets your name globally for Git.
  3. There are two ways to go about this step:

This part of the article assumes that you are using a service called GitHub. GitHub is a highly popular Git service that allows you to use their service to upload your code to their web servers for version control rather than keeping everything local. There are other services such as: GitLab, BitBucket, and Microsoft Visual Studio Git. Each has their own specific way of setting up the next steps, but the SSH Key method will be the same up until you have to add the Key to your specific service. As for the HTTPS method, it may be completely different. Each service should have a documentation page going about both of these methods which should be very easy to access.

Way 1 - Generating a SSH Key (Recommended)

This sounds daunting, but it is probably the easiest way out of the two personally (I use it) and better for the long run to use. We are going to generate a SSH Key. From Github's Documentation: Using the SSH protocol, you can connect and authenticate to remote servers and services. With SSH keys, you can connect to GitHub without supplying your username or password at each visit.

Steps:

  1. ssh-keygen -t rsa -b 4096 -C "GITHUB EMAIL" - This creates a new ssh key, using the provided email as a label
  2. When you're prompted to "Enter a file in which to save the key," press Enter. This accepts the default file location.
  3. At the prompt, type a secure passphrase.
  4. eval "$(ssh-agent -s)" - Start the ssh-agent in the background.
  5. ssh-add -K ~/.ssh/id_rsa - Add your SSH private key to the ssh-agent.
  6. pbcopy < ~/.ssh/id_rsa.pub - Copies your SSH key to your clipboard.
  7. While logged into Github, go to the top right, click your profile picture, and click Settings in the dropdown. Then click SSH and GPG Keys, or click this link to bring you there: SSH and GPG Keys
  8. Click New SSH Key, give your key a title My Computer, and paste the key in the large box.
  9. Done. Now anytime you clone a repository, Github will automatically give you the SSH URL and you will have to provide your SSH Key paraphrase anytime you push and pull changes and you will not need to type in your username or your password anymore!

Way 2 - Generating a Personal Token

This sounds daunting as well, but it is easy as well! We will generate a GitHub Personal Token to use on our computers. We need to do this if we are using the HTTPS URL from the repo if we have Two-Factor Authentication set up.

Steps:

  1. Go to the top right, click your profile picture, and click Settings in the dropdown.
  2. On the bottom left, click Developer Settings.
  3. On the left, click Personal access tokens.
  4. Click the Generate New Token button.
  5. In the Token Description box, type a name for your token, My Computer.
  6. Tick the box that says repo and then scroll to the bottom and click Generate Token.
  7. Important: you will only see this token once for security reasons, so if you would like to (and I recommend as I don't use this method, so I don't know if it saves the token when you use it), copy the token to somewhere safe.
  8. Done. Now anytime you clone a repository, Github will automatically give you the HTTPS URL and you will have to provide your Github Username as well as password which is the personal token we just generated, not your actual Github Password, anytime you push or pull changes (it may save the token so it may not ask you every time).

Setting up a global .gitignore

A kind user mentioned to me that another very important thing to do when setting up Git is to set up a global .gitignore file. A .gitignore file is used by Git to globally ignore a list of files that you may not want in your repos, such as: .jar, .exe, .DS_Store, etc. To set this up, copy the following into your terminal of choice:

git config --global core.excludesfile '~/.gitignore'
# on a mac:
echo .DS_Store >> ~/.gitignore
# If you use vim
echo '.*.sw[po]' >> ~/.gitignore
# And any other files that should never be checked into git in ANY repo

Each individual repository can have its own unique .gitignore, but the global one applies to them all.

Repository Setup

This section applies to all Git Services.

Now that we have setup our Git client to work like a well oiled machine, we now need to do some setup on the repository itself. The team you are associated with is the repo you will be working on. Click the repo that is your team's, and you will be brought to an overview of your repository which shows: files, commits, collaborators, and other things.

What we are interested in is the Fork button in the top right. Forking is the act of copying a repo to your own account. We do this so that no one is directly working on the Main Repository, or Upstream.

It may ask you to click your username or automatically start forking the repo to your profile. A couple of seconds later, you will see your fork. It looks the exact same, but now it is your personal repo and you can do whatever you would like and it will not affect anyone else! Awesome! We also know it is our fork and not the main repo because in the top left it will tell you where it is forked from.

Next, we want to clone our Fork, not the main repo (you will run into issues real quick!), by clicking the Clone or Download button and copying the link in the dropdown. Don't change to HTTPS if you are using SSH and vice versa (again issues!). cd into whatever directory you would like the repo to be in. Next type in the command: git clone and then paste the link after and click Enter. This will clone your fork on to your computer, which means you now have a local copy of your fork! Now cd into the folder that was now downloaded.

We are almost done with setup. We need to add the main repo, or Upstream, to our local repo so that we can pull changes down when others have their Pull Requests accepted. This is so that we can always be up to date with the main repo. Go back to Github and click on the main repo, not your fork, and click the Clone or Download button and copy the link you get from the main repo. Now, make sure you are inside the repo on your computer and type the following command: git remote add upstream and then paste the link and click Enter. It should not give you any feedback which means it was added. To check if it was added, type git remote -v which will list the remotes with their URLs. You should have origin and upstream there.

We are now all set up inside our repo!

Git Applications

Since some of you may not be super comfortable using just the terminal for Git operations, you can use a couple different Git GUI Applications to help you visualize what's going on so you don't screw something up on the command line!

  • Source Tree (personally use): Link
  • GitHub Desktop: Link
  • GitKraken: Link

Hope this helped! Any questions, feel free to ask me!

Happy Hacking!

  • Landon

Posted on by:

landonp1203 profile

Landon Patmore

@landonp1203

Software Engineer at Delta Flight Products

Discussion

pic
Editor guide
 

I would highly recommend using Git Bash as it gives you information about the branch you are working on right in the terminal compared to command prompt which does not tell you any information, as well as you can use Linux commands.

Windows guys actually made a plugin for PowerShell which you can install with Install-Module posh-git -Scope CurrentUser that's it,

 

Hi Saurabh,

I just looked at the plugin and it looks pretty awesome. But the issue I see is now is that even though the plugin can be installed for Powershell, you still need to install Git through the means I provided in the article. This now means the user has to install two things rather than one.

While it may be small, I personally would stay with Git Bash, especially for the Linux commands and so that I wouldn't have to install another thing on my system.

To each is their own, and I will take your comment and add it into the article, thanks for the feedback and another tool to use!

  • Landon
 

So here is my thing; it isn't always about installing the fewest things or whatever. It is about having the tools to do the job in the way that works best for you; especially when those things don't add additional strife to others on your team.

If you are working on Windows -- you are going to benefit from using PowerShell. Especially if you spend some time to customize your profile (just like you would with your bashrc, etc)

If you are working primarily with Linux systems then it makes sense to stick with that. You have the Linux Subsystem for Windows that is pretty slick.

The only issue I take with the article (all very good information) is that it is implied that this is THE proper way to do it. That just isn't the case. There is more than one way to skin this cat; and when personal opinion comes into play (like it does with so many things about how you do work) it is not really possible to say "this way is the correct way".

 

I'd highly recommend cmder.net for Windows. It includes Linux commands, git, and more!

yeah, cmder is the coolest terminal available for windows along with hyper.is

 

Git bash is definitely worth it. You can also use a GIT Prompt to see the status of the git repo right there in your console. Setting up git prompt

 

Came here to say this; been using this with git from PowerShell and it is pretty much the bees knees.

 

One more bit I consider critical in a proper git setup:

git config --global core.excludesfile '~/.gitignore'
# on a mac:
echo .DS_Store >> ~/.gitignore
# If you use vim
echo '.*.sw[po]' >> ~/.gitignore
# And any other files that should never be checked into git in ANY repo
 

Hi Daniel,

I wish I could give you a cookie, I forgot about global .gitignores myself.

I will add this in!

Thanks!

 
 

Nice. A couple of assumptions I'd highlight though - the linux instructions are Debian specific, and git doesn't require a github account (this is important because a lot of newbies think they're some kind of official part of git). I know your post has github as a tag, but the title implies it's a general git tutorial.

 

Hi Ben,

I wrote this up for a class and completely forgot about changing it to be more broad. When I’m back from class, I will change it. Thanks!

 

Hi, Landon.

First, the article is pretty good. Kudos for that. :)

The only (minor) thing I can comment about is your comparison of PowerShell as being the same as Command Prompt. They're not. In fact, PowerShell is a completely different shell environment. It's simply the shell that Windows should've always had.

Keep the good work!

Jorge

 

Hi Jebarsallo,

Thanks for the help! I am not a windows user, so I don’t know this! When I get near my computer again, I will fix it!

Thanks!

 

If you're going to install on windows, see if there is already a package for it via chocolatey.org. Chocolatey is much more useful for managing installations, and the more people use it, the more things are kept in sync.

 

I like it, just like homebrew for OSX.

 

Thanks for an easy to follow tutorial!

At step 5 in "Generating a SSH Key" when I write "ssh-add -K ~/.ssh/id_rsa" I get the error "ssh-add: unknown option --K", do you know how to deal with this?

I'm new to git and can't find anything when searching for the error.
I hope you can help!

 

Hi Maren,

As the error says, you wrote “—K” instead of “-K”. If it throws you an error even with that, try “-k”.

Hope it helps.

  • Landon
 

Hi Landon,

That was exactly the help I needed!
My problem is now solved :)

Thank you so much!!

 

I think you need a lower case 'k' as the parameter.

ssh-add -K ~/.ssh/id_rsa
unknown option -- K
-k Load only keys and not certificates.

 

Hi NetBzz,

I took that directly from GitHub’s guide on how to create SSH keys.

It works perfect for me!

 

It works because you're using macOS. On any other Linux distro that uses OpenSSH will ask for the lowercase 'k'.

 

Awesome write-up!! I wish I'd had all this written out when I first got started. 💯

 

Another tool for git is tig

 

If you work with GitHub I think that hub is really useful

 

U should not make the assumption that everybody in the Linux ecosystem is using Ubuntu/Debian.