In this post, we will learn about Git and why it is so useful for developers.
Git is a widely used version control system for software development.
In simple language, you can save your code in repositories and can access them anywhere in the world.
Problems with not using version system
- Code stored on hard disks are on risk if hard disk crashes
- Physical damage to disks or workplace can lead to loss of code and ultimately loss of money and time
- You need to carry your code in external disks if you want to move it from one place to another
- You need to create multiple folders specifying work done on a particular day
- You cannot share code with other users at the same time
Advantages of version system
- Your code is safe on web, so if your computer crashes, you can always get it back.
- Every time you make changes to your code, version system tracks it for you. So any time you can check who, when and what changes are made to the code.
- You can revert back your code to a previous date if you find some problem.
- You can share your code with multiple developers and they can contribute to it.
Let’s start with GIT
Register yourself on github.com and login.
Now, create a new repository. A repository holds our code. It has a unique URL that is used to identify it on the internet.
To create a new repository, click on + sign on the top right corner. Give it a name, let us say: html_examples
The repository type would be Public for now as Private is available for paid accounts.
On the next page, you will see the URL of your repository something similar to this:
and you will also see an example like this:
git init git add README.md git commit -m "first commit" git remote add origin https://github.com/username/html_examples.git git push -u origin master
Note it down for now.
Bitbucket also hosts our source code repositories. It also allow us to create fix number of private repositories for free.
Register yourself on github.com and login.
To create a new repository, on the top, click on Repositories => Create repository
Choose following settings:-
Repository name : html_examples Access level : This is a private repository Repository type : Git
To view more information, now click on Command line => I’m starting from scratch
Here you will see an example, how to setup your repository with your source code. We are interested in following lines:
git init git remote add origin https://firstname.lastname@example.org/username/html_examples.git git add contributors.txt git commit -m 'Initial commit with contributors' git push -u origin master
We will see more in the next examples.
Our first setup
Let us create a folder somewhere with the name html_examples
Create a new file in it, let us give it the name first.html
Open command prompt and navigate to the directory html_examples
Execute following commands:
git init git remote add origin https://github.com/username/html_examples.git git status git add first.html git commit -m "my first commit" git push origin master
Enter your username/password. The file now will be pushed to your repository.
The commands git init and git remote are used only once in the beginning.
git init => initialize git in the current directory
git remote add origin url => tell git the code will be pushed to repository represented by URL
git status => lists the changes made in the current directory
git add filename => adds file to be pushed to repository
git commit -m message => saves all the changes and are ready to be pushed
git push origin branch_name => pushes the changes to the remote repository
Git files are stored/mapped at four locations. These are:
Workspace => The directory in which our files exists
Staging => Where GIT stores all added files
Local Repository => Where GIT stores all the committed files
Remote Repository => Where we push our files on internet
Whenever we create a new file in our folder, it is marked as untracked if you use:
Untracked means git do not know about this file. To add this file to git, use:
git add filename
Now, if you see git status, it will say the file is modified. This file is now in staging
If you have changed an existing file, git status will display the file as modified.
Next, we commit our changes. This moves all added files to local repository
The last step is to execute the command git push origin master, which pushes all the changes in local repository to remote repository.
Branch in Git represents independent line of development that does not affect the original development files.
The default branch for git is called master
So, if we are not creating any branches, we will always write:
git push origin master
Why should I create a new branch?
Let us say you are already running a website. Now you want to create mobile view of the site as well as make changes to the website as well.
So, if we don’t want to mix mobile changes with website, we should create a new branch for mobile and make changes for mobile on that branch only.
Once mobile development is complete, we can merge the mobile branch with master branch.
To create a new branch, write:
git branch new_branch
To switch to other branch, write:
git checkout other_branch
To rename a branch, write:
git branch -m old_branch new_branch
To delete a branch, write:
git branch -d branch_name
To compare two branches, write:
git diff branch1..branch2
To merge a branch into current branch, write:
git merge branch_name
Cloning existing repository
To clone (or get files from) existing repository, we can write:
git clone https://github.com/username/html_examples.git directory_name
Replace username with your user name and directory_name with name of directory of your choice
Pull changes made by others
If multiple developers are working on the same repository, then it is quite common to get changes made by others. For this, we can write:
git pull origin master
git pull origin branch_name