loading...
Cover image for Gitting Started with git!

Gitting Started with git!

j3ffh95 profile image Jefferson Huisa Updated on ・6 min read

Basic commands to help you using git.


Alt Text

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

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

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


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
  2. Windows
  3. Linux

Before I get started, just wanted to let you know that I will be using Bash shell commands(reference link for Bash). 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

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

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

Then in your terminal you will have to enter:
git clone git@github.com:j3ffh95/IAmGoinToGetCloned.git
Alt Text
It will create a folder of the cloned repository.


Adding a file/files to git:

Alt Text

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

Alt Text

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

git add .

Alt Text

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

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
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


Branches!! 🌳🌳

Alt Text

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

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

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

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

Merging:

Alt Text

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

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

git merge login

Alt Text

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.


Undoing changes:

Alt Text

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

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

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

Resources!!

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

  1. Git Documentation
  2. Git — The Simple Guide
  3. Git — Cheatsheet
  4. Git Handbook
  5. GitHub Guides

Posted on by:

j3ffh95 profile

Jefferson Huisa

@j3ffh95

Web Developer Student

Discussion

pic
Editor guide
 

This will be so useful when I get confused using git ! thank you

 

no problem! thanks for reading