Introduction to Git tool and GitHub (2020 tutorial)


 

1. Introduction to Git

1.1 The history of git

Git is a distributed version control system for tracking code changes during development. It facilitates the team development for projects. 

It was designed by Linus Torvalds (creator of Linux) for Linux kernel development in 2005. You could learn more about git on wikipedia.

1.2 Features of git

It has two main features:

   1. version control

It facilitates the team development of projects and help you to maintain development history of your project.


   2. a distributed system

A Git directory on every computer is a full-fledged working repository with a complete history and full version-tracking capabilities without accessing network. This feature is a total departure from the traditional client-server version control system.

2. Git install and basic use (Ubuntu linux)

To install git is just a single line of code on Linux:

sudo apt-get install git


After installation, just type git to check its command


Next, we could just create a directory in your home directory named "git_demo".

Then in order to let git to help you manage codes in our "git_demo" directory, we have to initialize git in the "git_demo" directory.


First, cd to the "git_demo" directory. As you could see, there is nothing in the directory yet.

Second, type "git init" to initialize a git repository. Then we check the directory using command "ls -al". You could see a hidden directory ".git" is created.

The ".git" directory will help you to manage your codes during the process of development.

Now, let's create a basic text file for testing in the "git_demo" directory.


Using vim to create a file "code01.txt" and type the following line in it and quit vim.

Now we have a file "code01.txt" inside the "git_demo" folder. By now, git still cannot help us to manage our "code01.txt" file, we then need to add our file to the git cache first by typing "git add code01.txt".


Now, we could check the "git status "; it says our changes still need to be committed. 

Type "git commit -m "code01.txt v1.0"".


If fails just like the screenshot above, just type in your email address and user name to set up. Retype the command "
git commit -m "code01.txt v1.0"". 

Now we have created a version for our change to the "code01.txt" file. We could see the git log as well:


The first line is the most import one; it is the commit sequence created every time you commit a new version. The last line is the description for the newly created version.

Now you knew how to create a version with git; let's create a another version.


Add second line to the code01.txt file.


As always, type "git add code01.txt" and "git commit -m "code01.txt v1.0.1"".

Then our git log will have two versions with its own specific commit sequence.

3. What does git do behind the scene?

When we create a file and add one line inside it; then add the file and commit the change. The git will create a record behind the secene like the following:


Next, after made the second commit, we get the following structure:


A second record is created, but the second record only record the changes to the first one instead of storing all the content.

3.1 Head pointer and master branch

By now, we only have one single "master" branch



We could use the "HEAD" pointer to move back and forth among different versions in the master branch.


When we made the second commit, our "HEAD" will points to the "code01.txt v1.1.1" version in the master branch. If we want to go back to the "v1.1.0", we could type "git reset --hard HEAD^":

Now as you could see we are in "v1.0.0" again instead of "v1.0.1".


By typing "HEAD^" we could go back only one version.

"HEAD^^"    -------- go back two versions

"HEAD^^^" -------- go back three versions

If we have over 100 versions, we don't have to write 100 "^"s. We could just use "~[num]":

"HEAD~100" -------- go back 100 versions

"HEAD~1"     -------- go back 1 version same as "HEAD^"

If we could move back to history versions, how could we move forward to newer verions? 

It is simple, to move forward we just need the commit sequence number of the version.


Now we type "git log" and see that there is only one version to which our "HEAD" pointer is pointed. So we could use "git reflog" to get the information of our "v1.0.1" info.


Now could see our commit sequence number of version "v1.0.1", it is "9955cf9" which is just the front 7 digits. Now type "git reset --hard 9955cf9"


Now we are at "v1.0.1" again.



3.2 Working directory and repository


3.2.1 Working directory

The working directory is where we do our coding work such as editing, creating, etc.

In our case, it is the directory that we created in the first place "git_demo".

3.2.2 Repository

The repository is where git record our version info. In our vase, it is the hidden diectory ".git" when we execute command "git init".

3.2.2.1 Stage

There is a "stage" acrea in our git repository. When we type "git add xxxx", git will not create a record by now. Our changes to the file only goes to the stage area waiting to be committed. 

After we type "git commit -m "xxxx"", a record will be created by git; then git could track our versions.

4. Manage changes with git

Git could only commit changes inside the stage area. If you have made changes to a file, but not add it to the stage. You could not commit those changes and git will not create a record or version for those changes.

Add a third line to the "code01.txt" file:


Now type commit "git commit -m "code01.txt v1.0.2"":


Because we have not added those changes to the stage, git will commit nothing. It will let us know that in red color "modified: code01.txt" : changes made to "code01.txt" but not staged.

If we stage the changes and commit, a new version will be created by git.


Now we have three records or versions:



4.1 Discard changes to file before staging

Just add another line to our "code01.txt"


Before stage the changes to file, we could type "git restore code01.txt" to discard changes.



As you could see from the screenshot, changes to the file "code01.txt" has been discarded.

4.2 Discard changes to staged file

Just add the new line to the "code01.txt" file and then stage the change by typing "git add code01.txt".


By now if we want to discard the changes, we need two steps:

  • use "git restore --staged code01.txt" to unstage
  • use "git restore code01.txt" to dicard change

5. Compare files

5.1 Compare file in working directory with one in repository

Add a new line to the "code01.txt" and type "git diff HEAD -- code01.txt" and enter. This will compare the "code01.txt" in the working directory with the version where "HEAD" points to.


Look at the three lines in red box:


There is no "+" or "-" sign attached to those lines and those lines or content are what both files share. And the green line with "+" sign is only owned by our working directory file. 


The "+" line points to "+++ b/code01.txt" which represents the working directory file "code01.txt".

5.2 Compare two versions

Now, discard the changes to the working directory file "code01.txt" and type "git diff HEAD HEAD^ -- code01.txt".


Now, this time the red line "-this is the third line" represent the "--- a/code01.txt" and this version has this additional line and it is the version where "HEAD" points to.

We could also type "git diff HEAD^ HEAD -- code01.txt".


Now "+++ b/code01.txt" is the "HEAD" version and "--- a/code01.txt" is the "HEAD^" version.













Comments

Popular posts from this blog

How to write a slide puzzle game with Python and Pygame (2020 tutorial)

How to create a memory puzzle game with Python and Pygame (#005)

Introduction to multitasking with Python #001 multithreading (2020 tutorial)