Well, well well, you are all welcome! Once again, But…
Today…, My fingers are trembling my friends, please don’t say it is just because of ‘I am typing’, but it is actually on the account of what I am going bring to practice with this “Topic” of “git” and “version control” I am gonna bring Sirius Black (Harry Potter’s godfather) in this tutorial.
But before I actually begin with the topic, I would like to discuss a couple of issues with you. So, shall I ask you a question now? Well okay, Tell me have you ever been part of a project with the team? Any one, It may be a school project, or a university project, or a self-assigned experimentation one. If yes, then my next shoot to you, Had the project been developed within a single attempt of writing code, documentation, etc.? ( don’t think I am out of my mind today I gonna sweep these points within the topic, soon right here ). No, I surely can say that no project is just as simple as to be accomplished in a single attempt. Therefore, we work hard continuously over several days, weeks, depends on the complexity of the project to achieve its success.
But What if you want to keep the version control of each day, keep the track of each change, keep the track of who changed what, who added/removed what, what added/removed when and that too with the minimum possible memory usage and efficient security? Hmm?
Ladies and Gentlemen, introducing for this onus complex job none other than the fantastic concept of “Version Control Using Git!”.
There are various platforms available for doing Version Control, like Git, CVS, SubVersion, Mercurial, etc. But pardon me, for this blog is dedicated to Git only.
Git is undoubtedly a popular, efficient, terminal as well as GUI based, version control system. I am using my Linux (Ubuntu) terminal in this blog, you may use any of the other distributions of Linux as well.
First, let’s install git on our system:
sudo apt-get install git
Now try: man git — if it is giving the manual page about git then it means that you have successfully installed it on your system.
First, understand some basic terminology used in Version Control.
1. Repository:- It is the folder or directory in which version control would take place. It may have files and sub folders in it. Often called as “Repo”.
2. Commit:- It is to permanently make a fixed change in the file by giving a suitable “Commit Message”.
3. Commit Message:- Message that describes a commit.
4. Forking:- In Programming Forking means Coping. Here forking means to copy other’s repositories (with all its branching structure and history) to you.
5. Remote:- It is the place on the network where data is stored (cloud, hosting, etc.,).
6. Cloning:- It means to Download one of your repo. From a remote.
7. Push:- It means to send your repo. To your remote.
8. Pull:- It means to bring/copy/download other’s repo’s updated version (when you already have the previous version of it ). Helpful in updating the commit history from the remote repository.
9. Pull Request:- This is to send a request to a remote branch (may or may not be yours) to accept the changes you made out with the project.
10. Branch:- Your main working commits on a project creates a chain of commits and this main chain of commits of the Project is called your Master Branch.
11.Branching:- you may further separate your Master as well as other branches to create a separate chain of commits. (This is done because, suppose If we are not sure whether to add specific changes to our master branch then we can branch our Master to initiate a separate chain of commits, then after doing couple of changes in our second branch, and reviewed so that we become utmost sure to add these changes in our master, then we can Merge these branches. But if this second test branch is not found very useful then we may not Merge this with our master. So, basically branching is done to test to add some changes and if later on found suitable we may merge it with the main project. It saves our master branch from getting littered with useless commits.)
12.Merging:- to combine two branches. (merging conflict may arrive we will see how)
13.Merging Conflict:- These conflicts arrive when two branches are not able to merge by itself (automatically).
14.Staging Area:- Files and changes that can be tracked by git are called to be in the staging area. This means if you want to track changes on a file then you need to add it to git’s staging area. Also before making commit any change/modification you need to add it to git’s staging area.
Now Onwards we’ll go in flow with commands of git and would see how it works and does version control. For reference, we would be working on a test project, since I am a big fan of Harry Potter series, and Sirius Black is one of my favorite characters. Therefore our test project is Sirius_Black, okay? let’s go then.
[all bold text are git terminal commands, all italics are there specific description if required]
First, see where you are:
I prefer to be in “home/Public/”
Now create a new directory say SiriusBlack:
Change directory to SiriusBlack:
Now you must be in home/Public/SiriusBlack/ This directory will become your Repository. but First, configure your git profile with your email and username:
git config – – global user.name “Your name comes here”
git config – – global user.email firstname.lastname@example.org
To verify that you have successfully configured your git, type:
git config – – list
// this will show your user name and email.id you just configured with.
Now check your status:
this will print: fatal: Not a git repository (or any of the parent directories): .git
//all git commands starts with “git” word. This command “git status” will give you simply the current status we will see soon its’ message’s variations.
Here “.git” is the directory which would be required by git to perform version control and keeping all of its required data.
To initialize the current directory with a git repository type:
this will print: initialized an empty git repository in home/Public/SiriusBlack/.git/
// now you are all set to do version control in this repo. Now try:
this will print this time:
On branch master
Nothing to commit (create/copy files and use “git add” to track)
Your first commit was to create and initialize an empty git repo.
Now let’s create a new file:
This will open a new empty file SiriusIntro in your nano text editor (you may use vim, gedit, emacs, etc, as you wish). Type something in this file and save and exit
This is introduction of Sirius Black.
Sirius Black was one of the character in Harry Potter series.
[Ctrl + o],[enter],[ctrl + x] (in nano)
again see: git status
this time it prints: ……
//here your untracked file(s) is(are) listed
SiriusIntro // in our case
to add this file to staging area type:
git add SiriusIntro
// if a number of files are untracked then giving the name of each is a tedious task. For this you have:
git add -A
// this will add “-A” means “all” the files, folders, modifications and every other untracked element.
Now see: git status
Now showing: ……..
changes to be committed
new file: SiriusIntro
This shows that your SiriusIntro file is now in the staging area and any new modification on it can be tracked by git. If you want to remove this file from the staging area then you may type exactly:
git rm – – cached SiriusIntro
// this will unstage the file “SiriusIntro” that you may check with: git status
// Also, to remove everything from the staging area, you may type: git reset
// again to add our file type: git add -A
Now let us do our first commit:
// make sure you have added files and “changes to be committed:”
git commit -m “added a file”
This is your commit statement where text after “-m” in “ ” (double-quoted) shows commit message, without which git won’t allow you to commit.
Now see: git status
this will print: On branch master nothing to commit, working directory clean
Your file is committed to being changed in git. Try modifying it now:
This is introduction of Sirius Black. Sirius Black was one of the character in Harry Potter series. Sirius Black was Harry’s godfather.
[ctrl + o][enter][ctrl + x]
see: git status
here you will see that git had made out that your file is modified (this is because your file was in the staging area). Now if you want to commit these changes in the file. Then you need to add this changed file in the staging area.
*NOTE:- committing the file and saving the file are two different things altogether.
Saving means just to save the file after editing. And committing means to bring those changes in the git repository (may be one file or as many as you add). e.i you can save a file 2,3, or as many times before doing 1 single commit. Okay? So do not get confused with saving or committing. let’s add the modified file then:-
git add SiriusIntro
and do another commit:
git commit -m “added another line”
and see: git status
again your : working directory clean! This time try doing it in flow;
This is introduction of Sirius Black. Sirius Black was one of the character in Harry Potter series. Sirius Black was Harry’s godfather. His nickname was Padfoot.
[Ctrl + o][enter][ctrl + x]
gid add -A
git commit -m “added Sirius’ Nick name”
git status // working directory clean now, right?
Moving on… How will you find that how many commits have been done on this repository? You may do:
(note: if any time in log your control get struck up type “:q”)
// this shows all the commits, with its author, date, time, commit message, and at the top most (in yellow color) that is “commit hash” it is nothing but a very much secure 40-bit random code sequences, by which your individual commit is identified.
Git log, shows the commits in order of “Last commit First”.
To see what has been changed in the last commit try typing:
// lines in green with “+” symbol are added lines. And in red (if any, must not be here if you are following only this tutorial) with “-” symbol are removed lines.
git show -2 — this show last two(any number) commit changes.
Now I want to add a line in this file, but I am not sure whether should I add that line in my actual project or not, what should I do then, can you suggest me anything? Ahh! You may be right It’s time to use a branch, other than my master so that I can do commit in my file that are not part of my actual master project.
So, do not wait, just create a branch with: git branch branch_name
git branch broomstick
see if it is created or not by: git branch
// this will show you all the branches in your repository. And the one marked by asterisk “*” symbol in green color is the one on which you are currently available (“*master” in this case).
Let’s switch to the other branch with command checkout:
git checkout broomstick
try doing now: git branch
now you must have switched to “broomstick” branch. So, try doing some commit here and see are they reflected in master branch or not.
This is introduction of Sirius Black. Sirius Black was one of the character in Harry Potter series. He was Harry’s godfather. --substituted “he” in place of “Sirius Black” His nickname was Padfoot.
[Ctrl + o][enter][ctlr + x]
git add SiriusIntro
git commit -m “substituted a pronoun”
// now can you see the line in red that is the “removed line”. So, is this change reflected back to our master branch let’s see.
git checkout master
—making sure that you switched in master
and see here, your last commit is not shown since that was in the other branch.
here: working directory is clean. // since you have committed the change in broomstick branch. But if you didn’t commit it. git status would be showing the message of “modification” not “working directory clean”.
//commits of different branches are not reflected on others and that is exactly the use of branch. But what if, we like our broomstick branch so much that we would like to merge that branch within the master, and thus bring all those commit to the master branch. We use command merge for this:
//first switched to the branch (“master in this case”) in which you want to merge the other branch. (we are already in our master branch: can verify by : git branch )
git merge broomstick
// still you may see that broomstick branch is available even after it’s merging. You may remove this branch by:
git branch -d broomstick
Now your broomstick branch is “-d” deleted now. Once again you are left alone with your master.
// But let’s see if the branch broomstick merge really? by: git show
//Yes, here is your commit of broomstick branch in your master branch now.
// now again modifies the file:
This is introduction of Sirius Black. Sirius Black was one of the character in Harry Potter series. He was Harry’s godfather. His nickname was Padfoot. He was James Potter’s best friend.
[Ctrl + o][enter][ctlr + x]
// this shows you that your file is modified but if you want to know exactly what has been modified then?:
// this will show you the difference and modification (but this is “before commit” and git show shows you this “after commit”) so that you can be sure, that okay so these were the modifications made.
Now you can do: git add SiriusIntro
git commit -m “added another line”
// This tutorial was very brief about the only git, It’s capabilities are maximized when we would be working with a remote like “GitHub hosting service” and learn about collaboration in my upcoming blogs. Mean while if you want to know more about git commands type:
git — this will list about 20 common commands.
git help -a
// this will list a lot of commands, and if you know how to use “manual command – man” of terminal then you would be able to learn a lot of stuff by yourself.
e.i:- man git-add
// or what you can do is: git has described its functionality in about 8 guides. That you can see with:
git help -g
now all the 7-8 guides are listed. To access any guide type command:
git help <guide_name>
e.i:- git help everyday
With this, I stop. Writing soon… till then I wish you Happy Learning and This, is GeekyShacklebolt
bidding you goodbye!