Gitting Started with git!
Tags: git, vcs, github, productivity
Published: 2020-06-06 16:08:16

# Basic commands to help you using git.
---
![Alt Text](https://dev-to-uploads.s3.amazonaws.com/i/32rfzzw8jwjqac05f9li.png)

  When I first started learning git, I felt like I was learning what to do without learning why I was doing it and/or what was actually happening behind the scenes. 
***
![Alt text of image](https://media.giphy.com/media/kaq6GnxDlJaBq/giphy.gif)


So with this article, I wanted to give you the information that helped me understand what git is and the commands that I find to be the most helpful when working in a git repository. Please keep in mind that this article is meant to be very brief and it is not my intention to go very deep into the world of git.

Let’s git it!


---
### Is git and github the same thing?
![Alt text of image](https://media.giphy.com/media/cMVgEhDeKzPwI/giphy.gif)

Unfortunately those are two completely different things.
Git is a version control system which is to manage your source code history.

 Simple idea will be you and your friend writing a book and you being able to control different plot development to the story and also able to switch endings of the story base off what your readers prefer.

GitHub is a cloud based version control service. In the book example, GitHub will be like the google doc that allows you to save one version of the current story and letting others to make copy of it and work on their plot development on their local computer.

I found the concept of git and GitHub can be a bit confusing for beginners like me, Hopefully this analogy does not make you get more confused about git and github lol. I’m definitely not smart enough to get it the first day.

![Alt Text](https://dev-to-uploads.s3.amazonaws.com/i/29kmy8dryv1bcyqr4jyp.png)

***

### Gitting started

First thing first, let’s make sure git is installed in our computers - if not, check the links below for your system:
1. [MacOS](https://git-scm.com/download/mac)
2. [Windows](https://gitforwindows.org/)
3. [Linux](https://git-scm.com/book/en/v2/Getting-Started-Installing-Git)

---

Before I get started, just wanted to let you know that I will be using Bash shell commands([reference link for Bash](https://www.linuxtrainingacademy.com/linux-commands-cheat-sheet/)). I will also be following a procedure of steps on making a new repository.

***
###Git Vocabulary:

#####Repository (Repo) = Project
-Where Git saves all the data/changes/history of a working project.
#####Working Directory
-The folder location where the project lives.
#####Staging = Controlling what is being committed
-Before committing changes, they need to be staged.
#####Commit = Git’s way of saving.
-Git doesn’t save changes until the user actively ‘commits’ changes
#####Push
-Uploads a committed repository from the user’s hard drive onto GitHub’s server.
#####Pull
-Allows a user to access the most recent changes from the server into the user’s local repository
#####Cloning
-Is a command that allows you to copy an existing git repository.
#####Branch
-Branching gives you a way to work on a new feature without affecting the main codebase.
#####Merge
-Merging is when you want to integrate your branch/feature to the master branch.

***

###Create a new repository:
![Alt Text](https://dev-to-uploads.s3.amazonaws.com/i/x21ne53zywaqkp3gbt19.png)

First we have to make a new directory(fancy way for saying folder) to start our repository/project using *mkdir new_project*

Now will move into that new directory using *cd new_project*

We create this as a new git repository by entering *git init*

---

###Cloning an existing repository:

![Alt Text](https://media.giphy.com/media/Z8koEOoTT2rgghCzXK/giphy.gif)

From a Git hosting server GitHub:
git clone https://github.com/path_to_repo.git

You will look for the green button saying “Clone or download”:

![Alt Text](https://dev-to-uploads.s3.amazonaws.com/i/nvut22qpfv05v38mbmhb.png)

Then in your terminal you will have to enter: 
*git clone git@github.com:j3ffh95/IAmGoinToGetCloned.git*
![Alt Text](https://dev-to-uploads.s3.amazonaws.com/i/8n17gsouwe4qzxni9la9.png)
It will create a folder of the cloned repository.

---
###Adding a file/files to git:

![Alt Text](https://dev-to-uploads.s3.amazonaws.com/i/7n6g5q7e1tyemxgbkre7.png)

Now that we have a repository set up, we need to add some files for git to track:

![Alt Text](https://dev-to-uploads.s3.amazonaws.com/i/0vv83tzg7yw8tg28zjle.png)

add all new and modified/edited files in the directory, a period refers to all changes:

*git add .*

![Alt Text](https://dev-to-uploads.s3.amazonaws.com/i/alumh40zcz0dk18ewqzd.png)

Now that we asked git to add files, we then need to commit those files:

*git commit -m "add these changes"*

There are two things happening in the above command:

1. git commit ... ===> is committing the added files.

2. -m "add these changes" ===> -m will allow you to create a message so your coworkers know what changes are included in this commit.


***
###Pushing our commits/changes to our remote repository:

Once we have added and commited our files , we now need send or push those changes to our repository:

![Alt Text](https://dev-to-uploads.s3.amazonaws.com/i/garyshqy35lkpgowxyir.png)

*git push origin master*

Let me explain to you what the above command is doing:

1. git push ... ===> send our changes.

2. ... origin ... ===> the location of our repository.

3. ... master ===> the branch name, we will talk more about branches shortly.

![Alt Text](https://media.giphy.com/media/DqQhFf7nje2k0/giphy.gif)
Yes!! We did it we push our changes from our local repo to the remotely repo! We can see below the new files is on our Github repository.
![Alt Text](https://dev-to-uploads.s3.amazonaws.com/i/2ck5nwihx2fe7j5oydhs.png)

---

###Branches!! 🌳🌳

![Alt Text](https://media.giphy.com/media/XpYyhn0Gutfaw/giphy.gif)

Let’s imagine that we are creating a webapp which may have several different features. A good practice is to split the app development into the separate different features. Branches are also important when you are working with others.

Now let's say that one of the features we need to code is a login system …

With git we can make a new branch called login:

*git checkout -b login*

![Alt Text](https://dev-to-uploads.s3.amazonaws.com/i/w9lkwcgbga8arxo6f79j.png)

In the above git command:

1. git checkout ===> switch to the following branch name.
2. ... -b login ===> -b will change you to that branch

The above command is actually a shortcut one in the sense that it is actually checking out and creating a new branch in one go.

Also if we already have a branch created called login we could switch to it simply by:

*git checkout login*

After creating this local branch in order to have it remotely we need to push it to our remote repo. We can use the same command that we used earlier.

*git push origin login*

---
###Pulling 

![Alt Text](https://media.giphy.com/media/PWP86D0d8kLTO/giphy.gif)

OK so now let's say we need to get all the updates that our remote repo has. For example if you are creating a project in a team, and all of you are working in different machines at home. And one of your team members makes some changes and then when you start working on the project you want to have the lastest updates then you will need to make a git pull.

![Alt Text](https://dev-to-uploads.s3.amazonaws.com/i/khbznhp8ml0rgxsncb7r.png)

1. git pull ===> tells git to get the latest version of the repo

---

###Merging:

![Alt Text](https://media.giphy.com/media/cFkiFMDg3iFoI/giphy.gif)

Alright so now if our login branch we made earlier is debug-free and we want to *merge* it to our master branch then we meed to do git merge. 

But first you need to be on our master branch so we will run the *checkout* command:

*git checkout master*

![Alt Text](https://dev-to-uploads.s3.amazonaws.com/i/77dc5xavdm579xpu7hvn.png)

Then we can merge the new branch using the *merge* command:

*git merge login*

![Alt Text](https://dev-to-uploads.s3.amazonaws.com/i/0abo9qxb1jolugq751gy.png)

So now the master branch will have all the login feature from our login branch. This will work but sometimes when you are working other you may get a merge conflict. If you want to learn more about merging you can go [here](https://git-scm.com/docs/git-merge).

---

###Undoing changes:

![Alt Text](https://media.giphy.com/media/374pcIBVEGb6g/giphy.gif)

Last but not least is undoing changes. There are going to be occasions that you will need to undo changes to the local repo. For example we would like to remove some changes we did to a file called index.html, then we would need to run the checkout -- command :

*git checkout -- index.html* 

![Alt Text](https://dev-to-uploads.s3.amazonaws.com/i/5qmleh4kl5e0cepisujk.png)

ALso this command is very usuful when someone edits or erases any files in your repo, by using this command it goes back to your last commit.


---
![Alt Text](https://media.giphy.com/media/mR3dXKpI6P8CA/giphy.gif)

And We are done!(well for the basics 😅)

###Resources!!
These are some awesome resources to get to know more about git:

1. [Git Documentation](https://git-scm.com/doc)
2. [Git — The Simple Guide](http://rogerdudler.github.io/git-guide/)
3. [Git — Cheatsheet](https://www.git-tower.com/blog/git-cheat-sheet/)
4. [Git Handbook](https://guides.github.com/introduction/git-handbook/)
5. [GitHub Guides](https://guides.github.com/)