Getting Started with Git and GitHub | Complete Beginner's Guide
Hello developers, in this tutorial i am going to discuss about git. So in this git tutorial i will explain step by step why do you need git and github and how to set up git in your machine with SSH keys. I think this git tutorial is going to be the best git tutorial. Hope you will enjoy it. Let's start.
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:
- 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.
- You can see what was changed in your project’s files. This helps you understand what happened and improve your code
- You can easily store all the versions of your site and restore previous versions whenever you want.
- Store your files on cloud-based Git repository host services like Github and Bitbucket.
- You can easily share your files with others
- 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.
Click Install to download Command Line Tools package. Alternatively, you can also find the OSX Git installer at this website:
Install Git on Windows
You can download GitHub for Windows to install Git:
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
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.
Now we have to set up our ssh keys. let's do it
- Look to see if you have files
- If not, create such public/private keys: Open a terminal/shell and type:
$ ssh-keygen -t rsa -C "firstname.lastname@example.org"
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.
- Go to your github Account Settings
- Click “SSH Keys” on the left.
- Click “Add SSH Key” on the right.
- Add a label (like “My laptop”) and paste the public key into the big text box.
- In a terminal/shell, type the following to test it:
$ ssh -T email@example.com
If it says something like the following, it worked:
Hi username! You've successfully authenticated, but Github does not provide shell access.
Start versioning your project using Git
Git is very simple to use. First, you need to go to your working directory
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:
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:
You will see a list of untracked files, that means Git doesn’t monitor those files yet.
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:
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.
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:
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.