web design

Git Beginners Guide : The Complete Guide to Git

Git is the world’s best version control System available. It is developed by Linus Torvalds who is the creator of Linux kernel to manage the source code of the Linux. In this git beginners guide you will learn how to use git in your projects.

Why You Need Git

Although there are many other version control systems available but Git makes developers life easier by providing the most powerful and easy-to-use version control tools. With a version control system (VCS) you can have track of every changes you or others made to the project. With Git you can roll back the previous changes and work with other developers at the same time by dividing the tasks and then merging all the new features which are developed by other developers in the project.

Git is a distributed version control system it means it allows many developers to work on a given project without requiring them to share a common network. Each developer has its own copy of the project and he/she can make changes without requiring an internet connection or other network connection . When a feature or a change in a project is complete by one developer then he/she can push the changes to the central repository. And other developers can easily update their current working repository with all the new changes and commits made to the central repository using a pull command. It’s the magic of using a Version Control System (VCS).

Getting Started With Git – git beginners guide

In this guide I’m assuming that you have git installed on your computer. So lets begin.

Repository

Repository is the place or a folder where you store the project source code. All the changes you make to the project will be saved in the same repository in a folder named .git, the .git folder is hidden by default in OSX.

Since all the changes in a project be stored in the repository, so creating a repository is our first step. Let get started.

Creating A Repository in Git

To create a repository we use the git init command.

There are two ways to create a repository. One is to create the repository in a new folder and the other is to create the repository in an existing folder.

The first method – Creating an empty repository

In this method we are going to create a repository which does not contain anything. The following command will create a repository in which you can copy and paste your project code. For example lets create a new repository called “my_project”.

git init my_project

Git Command to create a new empty repository
Git command to create a new empty repository

The second method – Creating a repository in an existing project folder

In this method we will create a repository in an existing folder which contains the project. We will again use the git init command to create the repository but this time we will first navigate to the folder where we want to create the repository.

For example we want to create a repository in a given folder which contains the project. We will do the following steps.

creating repository in an exsiting folder
Creating repository in an existing folder

1. First we will navigate to the project’s folder.

2. Use the git init command to set up a repository.

Now you know how to create a repository. Let move to the next step i.e. committing the changes you made to the code.

Committing changes

Before you commit changes in your project you need to tell the git which file(s) needs to be committed or tracked , this process is called staging a file. To make a commit in git when first need add file(s) to staging area.

But before you add the file(s) to staging area you have to find which file(s) are untracked. To do this you can use git status command.

git status

Checking for untracked files in git
git status

You can see from screenshot above that the “functions.php” file is untracked. So lets add it to the staging area.

To add files to staging area we use the git add command.

git add

Adding files to Staging area in git
git add

Now you can make your first commit to the project. To make a commit in git, git commit command is used.

Committing the changes in Git
git commit -m “My first commit”

git commit -m "My first commit"

-m tag specifies the description of this commit in this case it is “My first commit” .

Branches

Branches are the most useful feature of git version control system. Branches gives you the ability to work on different features of the project on the same time and when the features are complete you can merge them into one, so you don’t have to wait for one feature to complete before you work on other features.

Branches in Git
Branching in a project

Creating a feature branch in Git

In git creating a new branch is very easy. To create a new branch in a repository we use git branch command.

Creating new branch in git
git branch

git branch new_feature1

Where new_feature1 is the name of the new branch. You can use git checkout command to switch between branches.

If you check the commit log in new_feature1 branch using command git log , you will see all the commits you made to the master branch. Now if you make new commits in the new_feature1 branch you won’t see them in the master branch until you merge the changes to the master branch.

After the work on the new branch is complete you can merge it to the master or other branches you have using git merge.

Merging with Git

Merging with Git is easy. To merge a feature branch into another branch we use the git merge command.

For example lets merge the new_feature1 branch to the master  branch. To do so first we have to goto the branch in which we want to do the merge, In this case it is master branch.

Merging a branch to master in Git
git merge

After we are in the master branch we can merge any branch into the master branch.

git merge new_feature1

Now you know how to merge two branches in Git. You have covered the basics of Git and now you can use git in your projects.

Some more basic commands

  • git status ⇒ Show the current branch, files which are changed etc.
  • git log ⇒ Show all the commits with the description.

It’s time to learn some quick commands to make the operations fast

  • git -a -m “Description”   ⇒ The -a tag select all the files in the repository into the staging area at once. So that you don’t need to add each file manually in the staging area and -m tag commit the changes, all at once.
  • git branch -D branch_name  ⇒ Use this command to delete a branch.
  • git checkout -b new_branch_name ⇒ Use this command to create a new branch and move to that branch at once.