What exactly git is?
Git is a free and open source distributed version control system and now you will ask what is version control system?. It is a system which records changes that is made in a file or set of files over time so that you can recall specific version later.
how to Install Git on Linux, Mac, and Windows, Click here for complete instructions.
How git works?
Git store information like a series of snapshots of filesystem. Every time you save the state of your project, Git take a snapshot of yours files and store a reference to that snapshot.
How did Git know what changes are made?
In Git everything is check-summed before it is stored. This means that git knows about every change in content of any file or directory by check-summed technique. The mechanism that git uses for checksum is called SHA-1(Secure Hash Algorithm 1) hash . This hash is 40 character long string composed of hexadecimal characters and is calculated based on content of a file or directory structure in Git. Git stores everything in its database by the hash value of its contents. The SHA-1 hash looks like this:
13b9db6551152997dd493b52c8696cc6d3a06374
Git has three states in which yours files can reside in:
Working directory :- files are pulled for modify or use.
Staging area:- is a file that store in .git directory. It stores information about what changes you want to be part of your next commit.
Commit (local repository):- store the metadata for your project. When you do a commit, then files in the staging area are store as snapshot permanently to your git directory.
Creating a Repository
I have Created a greyatom1 directory or you can use existing directory that you want to put under version control. Then initialize the directory using following command:
$ git init
This “git init” command creates a new sub-directory named .git that contains all the metadata and history of repository files. Now “greyatom1” is working directory, the files in this directory can be in two states: tracked and untracked. Git keep record of every change or commit made in a tracked file.To check,which files are in which state(working ,staging, local repository) use git status command.
$ git status
output of git status command:
On branch master
No commits yet
nothing to commit (create/copy files and use "git add" to track)
This output means that our working directory is empty and no commit has been till yet.
Add a new file testdata.txt in “greyatom1” directory and add any text you want I add “hello”. Now again run git status command and output will be:
On branch master
No commits yet
Untracked files:
(use "git add ..." to include in what will be committed)
testdata.txt
nothing added to commit but untracked files present (use "git add" to track)
The testdata.txt file is now untracked file and Git only include those files in snapshot(commit) which are tracked files, to include this file into tracked files type following command:
$ git add testdata.txt
If you again run git status command, you will see following output:
On branch master
No commits yet
Changes to be committed:
(use "git rm --cached ..." to unstage)
new file: testdata.txt
Add another line of text in textdata file I added “World”, then again run git status commands, you will see following output:
On branch master
No commits yet
Changes to be committed:
(use "git rm --cached ..." to unstage)
new file: testdata.txt
Changes not staged for commit:
(use "git add ..." to update what will be committed)
(use "git checkout -- ..." to discard changes in working directory)
modified: testdata.txt
Now you have noticed that the testdata.txt file is under “changes not staged for commit” this means that a file has been modified in working directory but not yet moved to staged state, to include changes to staged state type git add testdata.txt command.
To save changes permanent use commit command.
$ git commit -m "saving hello and world lines
Commit records the snapshot of files in staging area and ”m” is message to your commit.
For viewing committing history
$ git log
Cloning an existing repository
If you want to get copy of some exiting repository or project, then type
$ git clone url
To get url, go to repository in GitHub then click on clone or download option and copy URL.
Branching
Chain of yellow circle in fig. 2 is the branch of master. The branch is a lightweight movable pointer to the commits. The default branch name is master, when we make commit then master branch will point to this new commit.
For creating branch type git branch followed by branch name. This command will create a new pointer that will be pointing to the same commit you are currently working on.
$ git branch login_feature
Git used special pointer HEAD, which point to branch you are working on. when you run git log command then you will HEAD pointer point to master branch. The git branch command only creates new branch but doesn’t switch to that branch.
commit 6dcf2bfb0f327d53886bce58fadf324dff0b1694 (HEAD -> master, login_feature)
Author: shoaib
Date: Wed Aug 29 16:53:29 2018 +0530
machine learning folder
commit e4d4562b8cbca49e504cd620bec83757e8c8e667
Author: shoaib
Date: Wed Aug 29 14:17:50 2018 +0530
saving hello and world lines
To point HEAD to newly created branch, so any commit you made will be store in new branch. Type following command:
$ git checkout login_feature
Again run git log command, now you will see HEAD changes to login_feature.
commit 6dcf2bfb0f327d53886bce58fadf324dff0b1694 (HEAD -> login_feature, testing, master)
Author: shoaib
Date: Wed Aug 29 16:53:29 2018 +0530
machine learning folder
commit e4d4562b8cbca49e504cd620bec83757e8c8e667
Author: shoaib
Date: Wed Aug 29 14:17:50 2018 +0530
saving hello and world lines
Add text “Mumbai” in testdata.txt file and then commit the changes. All these changes will be saved in new branch. Now my testdata.txt file contain following data:
$ cat testdata.txt #command to read text file
hello
World
Mumbai
Go to master branch and create another branch “city_name” by git branch city_name , add “Delhi” text in testdata file, commit changes. Now city_branch will contain following text.
$ cat testdata.txt
hello
World
Delhi
If you come back to master branch and read text file then it will neither contain “Mumbai” nor “Delhi”. It will contain following text:
$ cat testdata.txt
hello
World
To merge the city_name branch with master branch, use following command, but condition is that you must be in master branch.
$ git merge city_name
push local repository into GitHub account
$ git push origin master
Congrats, you learned basic of Git. For any query, comment below. looking for the feedback so we can add more such beginners content Have fun, keep learning and be innovative.