Stephen Sun

Software engineer based in Houston, Texas.

I'm a detail-oriented individual who thrives in fast-paced team environments. I have experience across different industries, working with both front end and back end technologies.

Beginner's Guide to Git

What is Git

Git, in a nutshell, is a system that allows us to properly and effectively track code changes. It allows multiple developers to work together at the same time, while also maintaining a history of all the changes in a repository. Git also provides many usefuls features such as branching and merging to allow developers to work on specific features independently.

Install Git

Download Git: Getting started installing Git

Essential Git Commands

git init

git clone <path/url>

git add <filename>

git commit -m "<commit message>"

git push origin <branch name>

git pull

git status

git merge <branch name>

git checkout -b <branch name>

git checkout master

git branch -d <branch name>

git remote add origin <server>

Git Walkthrough

Step 1: Create local Git repository

Find a good location where you would like to store your repositories and create a folder for your new project. For this example, let's use my-first-project.

Since this is a brand new repository, we need to initialize it with git init. This tells Git to track code changes for your project.

mkdir my-first-project
cd my-first-project
git init

Next, let's create a file called index.js using the touch command in the terminal.

touch index.js

Step 2: Add a new file to the staging area

To include this change in our upcoming git commit, we will need to add this file to the staging area. We can think of this staging area as git's way of grouping our pre-commit changes together. This can be done with the add command.

git add index.js

Now, what if we wanted to add more than one file at a time? All we have to do is list out all the file names at the end. It would look something like this:

git add index.js file1.js file2.js file3.js

Also, if we wanted to simply add all the files in our project, we can use the shortcut:

git add .

All changes, staged or unstaged, can be checked at any time with the status command.

git status

Step 3: Create a new commit

Once we're satisfied with our staged changes, it's time to create our first commit with the commit command. We should always add a message to our commit, describing our code changes.

git commit -m "Created index.js file"

Step 4: Push our commit to the remote repository

The commit is now ready to be pushed from our machine to our remote repository (GitHub, Gitlab, Bitbucket, etc). We will use the push command to do so. The origin keyword is a shorthand name for our remote repository and master refers to the branch we want to push our commit to. If we wanted to push to a different branch, we can specify another branch name (e.g. development).

git push origin master