DEV Community

Code Panda
Code Panda

Posted on • Updated on • Originally published at codepanda.dev

Intro to Git

What we'll cover:

  1. What is git?
  2. Why should you use git?
  3. Basic Git workflow and commands

What is git?

Git is a free open-source version control system that allows you to keep track of your code by creating multiple versions of your code-base and storing it all in a central place either on your computer, on the cloud or both. This place is called a repository or "repo" where you to store all your modifications and revert to them at any time.

Why should you use git?

There are many benefits in using a version control system such as collaboration, storing versions of your app and backing up your code.

Let's talk about some of the benefits that git offers.

1. Ability to collaborate with others.

Git makes it easy to work in teams and allows all the team members to access and contribute to the same project and also keeps track of who did what and when. As soon as everyone is finished with his/her task, they can merge their changes in one place and manage conflicts in their code if there are any.

2. Backup and recovery.

As mentioned before, git allows you to have versions of your code stored for future reference, which means in case of an accident, you can immediately revert to your code-base stored locally or on the cloud and retrieve all lost code.

For example, if your app fails for any reason and you can't find the bug to fix it, you can go back to any of the working versions with a simple command and start over again. We will explore all the basic commands up next.

Before we continue, here are the recommended command-line interfaces based on your operating system. Of course, you can follow along with any command-line interface. 😊

MAC: Use the keyboard shortcut command + space and type "terminal" and navigate to the folder you wish to use git.

Windows: Download & install Git bash on your system then run it and navigate to the folder you wish to use git.

Basic Git workflow and commands

To use git, you first need to install it on your computer by visiting git's download page and downloading & installing it.

After the installation process is complete, open your command-line interface and type:

git --version

If you see a version, that means that you successfully installed git on your system.

Now, let's go through the main stages of initializing, staging, committing and maintaining.

1. Initialization.

You'll need to configure your name and e-mail for git to keep track of who did what.

You can configure your name globally on your machine by typing:

git config --global user.name "Your__Name__Here"

Now check if it's set by typing:

git config --global user.name

Now let's configure your email by:

git config --global user.email "Your__E-mail__Here"

You can check it by:

git config --global user.email

Note: You can also configure your info in each repo separately by typing the same commands but removing the --global flag. What --global does is that it set your info all over your machine, not in a specific repo.

Now, let's start with the very first step in using version control, which is initializing git your project by opening your command-line interface and navigate to the base directory of your project where all the files & folders of your project are located and enter:

git init

This will create a new local git repository in your project directory.

Now that you initialized git in your project, you'll want to have a remote repository on the cloud to interact with and save your project files.

There are many repository hosting services such as Github, GitLab and Bitbucket. In this blog, we're going to be using Github to host our remote repository, other than your local repository, there is a remote repository that you interact with which has all your code stored in the cloud.

Go ahead and visit Githubs page and create an account. it's free for public repositories.

Now that you have an account, it's a good practice to set up Github with an SSH key which will add an extra layer of protection.

Now let's start with the setup process.

First, we'll need to generate a pair of SSH keys, a public key, which is the key we can provide to services and a secret key which will be used for validating your connection.

Now, open your command-line and enter:

ssh-keygen -t rsa -b 4096 -C "YOUR__EMAIL__HERE"

Then, it's gonna say something like ( Enter file in which to save the key (/Users/YOUR__USERNAME/.ssh/id_rsa)" ), you don't wanna change that, just click the Enter when it'll ask you for a passphrase twice where you type a password for your key, please do so.

Note: when you type your passphrase it'll be invisible so just type it and click enter.

Then you see something like:

Your identification has been saved in /Users/YOUR__USERNAME/.ssh/id_rsa.
Your public key has been saved in /Users/YOUR__USERNAME/.ssh/id_rsa.pub.
The key fingerprint is:
--SOME__STUFF--

That means that you successfully generated an SSH key. Now, the next step is adding this generated SSH key to an SSH agent.

Now enter:

eval "$(ssh-agent -s)"

Your output will be like:

Agent pid SOME__NUMBER

This will make sure that your SSH-agent is running.

Next enter:

ssh-add -K ~/.ssh/id_rsa

Then you'll be asked for your passphrase, please enter it.

Next, let's copy the public SSH key to the clipboard by entering:

pbcopy < ~/.ssh/id_rsa.pub

Let's take that copied SSH key and head to your account on Github.

  • Head to the top-right profile icon and select "Settings".
  • Select "SSH and GPG keys" on the left menu.
  • Click "Add SSH key" button.

Add a title to your key to identify it for example "Work laptop" then paste the copied key into the "Key"
field

log in and create a new repository by clicking the green "New" button on the top left.

Note: the website's layout might change depending on the time you read this.

After you have created your repo, you'll get an instruction page similar to this:

This page is just Github trying to help out by giving you some instructions and commands to get hookup your local repo to the remote one.

By default, HTTPS will be selected, since we configured your SSH key, it's better to use SSH so go ahead and select SSH

Now, we'll need the URL of the remote repo on Github to interact with it.

You can find your URL here:

Now, let's connect your local repo to the remote one by typing:

git remote add origin YOUR__URL

This command adds your remote repo and gives it the name origin. The name is completely up to you but it's a good practice to name it origin.

Now that we initialized and configured git, let's dive into the next step.

2. Staging

In this step, you'll take what is called a 'snapshot' for your app and then add it to a place called the "staging area". The whole idea is to take a snapshot of your current app then save it for later if you need to go back to it.

To achieve this, you'll need to add the files that you need to be snapshot to the staging area so that they could be tracked by git.

First, Let's check the files that are not tracked by git by typing the command:

git status

This will list all untracked files and mark them in red.

Now to add them you can type:

git add FILE__NAMES

OR if you'd like to add all files then:

git add .

Let's check the tracked files one more time:

git status

You'll see that all the files you added are now green which means that they are tracked and are ready to be saved, which brings us to the next step.

3. Committing

After taking a snapshot of your files, you'll want to move the snapshot from staging to your actual local repository. This step will allow you to do this. After this, you have the option to continue working locally or send you current snapshots to the remote repo.

Let's now save the snapshots to your local repo by:

git commit -m "YOUR__DESCRIPTION"

This allows you to save your snapshot to your local repo and you'll have to pass a message along with each snapshot by providing the flag -m followed by a description of what it contains.

The next step is to push/send your snapshots out to the remote repo by typing:

git push -u origin master

The -u is short for --set-upstream and it sets the default remote branch. Now, you can just write git push next time not git push -u origin master. 😊

Now you can go back to the GitHub remote repo where there were instructions and refresh the page.

TADAAA! now all the files that you added are now live on your remote repo and you can revert to them anytime.

Keep in mind that now your code is public, so anyone can download or clone it but now one can change your code without your permission.

If you'd like to clone other peoples repo you'll have to do the following:

  1. Visit the repo you wish to clone.
  2. Click "Clone or download".

Download -> will just download the whole project as a ZIP file

Clone -> you'll need to use the command-line interface and go to the folder when you wish to save this project and type:

git clone "PROJECTS__URL__HERE"

This will download a folder containing all the files.

Now let's jump into the next stage.

4. Maintaining

In this step, you'll just repeat some of the steps above whenever you add a new code to your code-base.

Let's go through them:

  1. git add . to add and update all the modified files.
  2. git commit -m "YOUR_DESCRIPTION" . to save the new snapshots to your local repo
  3. git push to send your new snapshots to the remote repo.

This was is the basic use of version control and commands.

Let's now talk about branches.

Branches

you can think of branches as copies of one code-base usually the master branch.

Let's say you have 1 branch called ( master ) and you're working only it and you have a bug-free app that is running perfectly fine. You can create another branch which the exact code which is in ( master ) that you're coming from but copied and put in another place, let's call it ( develop ) and now the branches are separate and whatever you do to ( develop ) won't affect the ( master ).

That way you can keep on developing your app on ( develop ) branch and keep the working copy which is ( master ) untouched and then when you're satisfied with your ( develop ) branch and everything is working fine you can "Merge" the 2 together and now have the ( master ) the same as ( develop ) branch and so on.

Let's explore some of the basic commands for branching.

git branch -a

Lists all your branches. Your current branch will have an asterisk "*"

git branch BRANCH__NAME

Creates a new branch with that name.

git branch -d BRANCH__NAME

Deletes branch with that name.

git checkout -b BRANCH__NAME

Create a new branch and switches to it

git checkout BRANCH__NAME

Switches to another branch

git merge BRANCH__NAME

Merges a branch into your current

git merge FROM_BRANCH__NAME TO_BRANCH__NAME

Merges a specified branch into another specified branch

That's all when it comes to the basic usage of git.😊

I hope you learned something new, if you did, please share this post with a person that you think can benefit from.😄

Have a great day!

Top comments (0)