Theme

Git

Git Logo

Version Control System

Git is a version control system. A version control system is a piece of software that helps the developers on a software team work together and also archives a complete history of their work.

There are three basic goals of a version control system:

  1. people able to work simultaneously, not serially.
  2. when people are working at the same time, their changes are not conflicting each other.
  3. archive every version of everything that has ever existed — ever. And who did it. And when. And why.

Using Git

Using command line

Open Git Bash and try the following commands.

Change directory

cd folder
cd folder/subfolder
cd ../
cd ../another-folder/nested-folder

Create file

touch file-name.ext

Create directory

mkdir folder-name

Remove file

rm file-name.ext
rm folder

Remove folder and all its contents (!)

rm -rf folder

Create a new, empty repository

repository = filesystem * time

git init

The three states

Git has three main states that your files can reside in: committed, modified, and staged.

  • Committed: the file is safely stored in repository
  • Modified: the file is changed but not committed to repository yet
  • Staged: the file is marked to go into your next commit snapshot

The diagram below illustrated the 3 states:

The three states of file in git

The basic workflow goes something like this:

  1. You modify files in your working tree.
  2. You selectively stage just those changes you want to be part of your next commit, which adds only those changes to the staging area.
  3. You do a commit, which takes the files as they are in the staging area and stores that snapshot permanently to your Git directory.

To stage your change, we use the following command:

git add <fileName>

To commit all staged files, use the following command:

git commit -m "<commit message>"

To check the current status:

git status
git status -s

To unstage a staged file:

git reset HEAD <file-name>

Branching

When you create a git repo, by default you have one branch, and it is called master.

However, if multiple people working on same files together, their changes may conflict each other, and making everyone’s life difficult.

Branching means you diverge from the main line of development and continue to do work without messing with that main line.

To create a new branch

git branch testing

Two branches

However, we’re still at master branch.

HEAD pointing to master

To switch to the new branch

git switch testing

HEAD pointing to testing

Let’s modify something and do another commit.

testing branch is now ahead of master branch

Switch back to master branch

git switch master

switch back to master branch

When you make another commit on master

master and testing branches diverge now

You can merge changes of testing branch into master with:

git merge testing

Finally, you can delete a branch with

git branch -D testing

Images in this section are adapted from Pro Git

Using GitHub

Create a Repository

Pulling and Pushing from Repository

Forking and Pull Request

Fork and Create a PR to this repo.