Check out the new web development bootcamp! learn more

It has already been one month since I first created this blog! I am trying to improve it. Do you have any ideas? leave a feedback

home > learn git

Web Dev Bootcamp ∙ Learn Git

Hieu Nguyen · May 06, 2020 · 5 min read

webdevgitcourse

0

0 leave some love!

What is Git?

Git is a version control software that allows you to keep track of changes to your project.

It allows you to keep a history of the changes you’ve made to a project

Why is this useful?

Bugs are typically introduced when new changes are introduced. Hence, being able to pinpoint when a bug occurred based on when the changes was made can be a time saver

You can also deploy your code to a remote server, such as Github, and other people can look at your code and contribute if they want to.

Installing Git

Window - use the installer

Mac - use the brew package manager to install

brew install git

Linux - use the terminal to install

sudo apt install git-all

Creating a new git repo

In your root project directory, run

git init

Cloning a repository

cloning a repository is like making a contained copy a repository for personal use

git clone LINK_TO_GIT_REPO

Stage, Commit, and Push

There are 3 stages to deploying your change to a remote server.

  1. The first is staging.

Staging is the processing of adding changes to a file to a stage or an Index location

git add <filename>

you can also add all changes to the Index using

git add .
  1. The second step is committing these changes

Committing a set of changes, is simply a way of saying : I really want to keep these changes

git commit -m "first commit"

Note: you have to include a message with every commit using the -m "your message goes here". The message should be descriptive of the changes you have made

You can also make multiple commits in a single workflow

Ideally, each commit should contain a set of related changes, such as a bug fix or a small feature

  1. The final step is making your changes visible in the remote server
git push origin master

If this is a new repository that you created locally, you have to add a remote server first before pushing

git remote add origin <link to the server>

Branches

Every time you create git repo, it automatically generates a branch called the master branch.

Given the name, the master branch is the most valuable and should always be the most up to date

A branch is just a copy of another branch.

here is how you can create a branch

git checkout -b <name of your new branch>

Assuming that you are in the master branch, this command will create a new branch from the master branch.

You can create a branch from any branch; it doesn’t have to be your master branch

you can switch between branches using

git checkout <name of your branch>

You can check the available branches as well as the current branch that you are in using

git branch

The current branch that you are in will have an * before it

To switch back to the master branch, run

git checkout master

To delete a branch, run

git branch -d <name of branch>

Pushing a branch to the remote server

Did you noticed before how we push our changes to the remote server?

git push origin master

Here we are pushing the master branch to the origin server

you can check the servers linked to the git repo by running

git remote -v

Turns out, you can push other branches to the remote server

git push origin <name of your branch>

Pulling changes from remote branches

Let’s say someone else made some changes and pushed them to the remote server.

How do you get those changes? (Please don’t say copy and paste…)

What’s the opposite of push?

git pull

This will do a fetch and a merge into the current branch

Fetching is the process of pinging the remote server for new changes, but doesn’t actually pull the changes to your local repo

Merging Will attempt to merge the new changes with your local repo

Sometimes you want to merge some changes from one local repo to another repo

You can do that by running

git merge <destination branch>

This will merge the current branch into the destination branch

When to create a branch?

If you are working by yourself on a small project, then branching might not be necessary.

However, it does start making sense or even become essential when you are working in a collaborative environment on a large scale project.

Each person can work on a separate feature or a fixing a different bug at the same time.

There is no need to wait for a person to finish their part before you can start working on yours.

A good rule of thumb is that each feature should be its own branch, typically called the feature branch.

Stick to a consistent branch naming convention, such as

git checkout -b feature/adddeletefunctionality

Similar for bugs,

git checkout -b bug/pagedoesnotload

Git Status and Logs

Sometimes you want to check the status of your git repo, such as files that are staged, run

git status

You can also access the log by running

git log

What’s Next?

Introduction to node


Hieu Nguyen - Founder of devsurvival.com. I am also a video gamer, a full stack developer, and an anime lover. I'm also an advocate of teaching others how to code.
Archive