Getting Started with Git and GitHub: Complete Beginner's Guide

Why do you need to use Git?

Git is becoming an ‘industry standard’. If you want to become a better developer, you may need to use Git to develop software and collaborate with other developers. Git lets you manage code development in a virtually endless variety of way. Here are benefits of using Version Control System/Git:

  1. Git allows you to create as many branches of your project as you want. You can use each branch to test, create a new feature, fix bugs, etc.
  2. You can see what was changed in your project’s files. This helps you understand what happened and improve your code
  3. You can easily store all the versions of your site and restore previous versions whenever you want.
  4. Store your files on cloud-based Git repository host services like Github and Bitbucket.
  5. You can easily share your files with others
  6. A VCS or Git helps your team work more efficiently. Everyone knows what is going on and can merge the changes into a common version.

How to install Git?

Note: if you don’t know how to run a command, please read the Recipe 1 - Introducing CLI (Command Line Interface).

Install Git on Mac

The easiest way is to install the Xcode Command Line Tools. You can do this by simply running this command.

xcode-select --install

Click Install to download Command Line Tools package. Alternatively, you can also find the OSX Git installer at this website:

http://git-scm.com/download/mac

Install Git on Windows
You can download GitHub for Windows to install Git:

https://windows.github.com

Install Git on Linux/Unix
You can install Git by running this command:

sudo yum install git

If you’re on a Debian-based distribution, use this:

sudo apt-get install git

For more information and other methods, you can see this guide: https://git-scm.com/book/en/v2/Getting-Started-Installing-Git

Configuring Git

When you first install Git, you should set your name, email address and enable coloring to pretty up command outputs. Open your CLI and run these commands:

1 git config --global user.name "Your Name"
2 git config --global user.email "Your Email Address"
3 git config --global color.ui auto

Note: Remember to replace Your Name and Your Email Address.

Set up ssh on your computer.

I like Roger Pengguide to setting up password-less logins. Also see github’s guide to generating SSH keys.

  1. Look to see if you have files ~/.ssh/id_rsa and ~/.ssh/id_rsa.pub.
  2. If not, create such public/private keys: Open a terminal/shell and type:
$ ssh-keygen -t rsa -C "[email protected]"

Note: (Don’t type the $; that just indicates that you’re doing this at the command line.)

Copy your public key (the contents of the newly-created id_rsa.pub file) into your clipboard. On a Mac, in the terminal/shell, type:

$ pbcopy < ~/.ssh/id_rsa.pub

Paste your ssh public key into your github account settings.

  1. Go to your github Account Settings
  2. Click “SSH Keys” on the left.
  3. Click “Add SSH Key” on the right.
  4. Add a label (like “My laptop”) and paste the public key into the big text box.
  5. In a terminal/shell, type the following to test it:
$ ssh -T [email protected]

If it says something like the following, it worked:

Hi username! You've successfully authenticated, but Github does not provide shell access.

Read also How to Recursively Remove .DS_Store Files on Mac OS

Start versioning your project using Git

Git is very simple to use. First, you need to go to your working directory

cd Code/Laravel

Note: If you’re using Homestead, the Code directory is where we will put our Laravel apps. Code/Laravel is your working directory. You can use Git on Homestead or on your local machine, it’s up to you.

Now we can use the git init command to initialize Git:

git init

This command creates an empty Git repository. If you’re using Homestead, the path of the Git directory is:

/home/vagrant/Code/Laravel/.git/ . “.git” is a hidden folder and it doesn’t contain your project’s files yet.

Add and commit your files
Now we can use git status command to check the status of our working directory:

git status

You will see a list of untracked files, that means Git doesn’t monitor those files yet.

setup-git-first-time

To tell Git that you want to include all these files, use the git add -A command:

git add -A

Note: Alternatively, you can use git add –a or git add –all or git add . command.

When we run the git status command again, you’ll see:

setup-github

The git add command tells git to add changes in your project to the staging area. However, those changes aren’t saved yet until you run git commit:

git commit -m "First commit"

Note: You can use the -m flag (stands for message) to give a comment on the command line. My message is “First commit”, but you can use whatever you like. Well done! You’ve made your first commit!

Push your project to Github

Your new repository (repo) is empty. You need to upload your files to that Github repo. Every repository has a unique remote URL, your remote URL should look like this: https://github.com/YourGithubUsername/YourRepoName.git Take note of this link. Good! We will try to upload our Laravel app (/Code/Laravel) to Github.

git push

You now have your files on the cloud!

Cloning a repository

To download any repo, you can use the git clone command. First, navigate to the location where you want to place the cloned directory:

cd yourDirectory

Type git clone and the unique remote URL to clone the repo:

git clone https://github.com/YourGithubUsername/YourRepoName.git

This command creates a local clone of the repository on your computer.

Note: you can clone any public repository. If you don’t want anyone to download your repo, set it private.

Hope you've enjoyed this tutorial. 

Codechief is a very fast growing community among programmers and have a reach of around 1 million+ readers globally. Contribution at Codechief is open for all those who have a passion to learn and help others by sharing their knowledge. If you think you have the zeal to learn, start contributing on Codechief contribute. you can also mail your article to [email protected] See your article appearing on the codechief main page and help other code.

We believe that everyone has the right to learn, so we allow both students and professionals to contribute on Codechief.Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.

A web enthusiastic, self-motivated Full-Stack Web Developer from Dhaka, Bangladesh with experience in developing applications using JavaScript, Laravel & Wordpress specifically. Facebook Github Website