Learn Git – Complete Git Tutorial for Beginners

In this tutorials, we will learn Git and the usage of the distributed version control system Git via the command line. Whether you are a developer or a designer, we all deal with files every day. Our whole circle revolves around creating files, editing files, saving files etc. I started using Git a few years ago, and believe me, I had no idea what Git was for.

Why use Git?

So why anyone should use Git? What are the benefits?

  • Git Works offline: The Great thing about git is that it works both Offline and Online unlike other version control systems like SVN or CVS which requires internet.
  • Restore deleted commits: This feature is very helpful while dealing with large projects, while trying out experimental changes.
  • Git is Secure: Git provides protection against alteration of any files and helps maintaining an original content history of the source file.
  • Git is Fast: Git has an optimized performance due to its feature like committing new changes, branching, merging and comparing previous versions of the source file.
  • Flexibility: Git supports mutiple kinds of development workflows, for both small to large projects.

So What is Git Actually?

Git is a free and open source distributed version control system. Git’s purpose is to keep track of projects and files as they change over time from different users. Git helps keeping track the history of the project files by storing different versions. Git stores information about the project’s progress on a repository.

All this information is stored in the same folder as the project in a sub-folder called .git and will mostly be hidden by default in most systems. You can then upload the code to GitHub or BitBucket where authorised users can either view, pull the code or push changes.

What is a Git Repository?

A Git repository contains the history of a group of files in a directory. The process of copying an existing Git repository via the Git tooling is called cloning. After cloning a repository the user has the complete repository with its history on his local machine.

What is staging and committing in Git

After modifying your files you need to perform the following two steps to persist these changes in your local repository:

  • Add the selected changes to the staging area via the git add command. The git add command stores a snapshot of the specified files in the staging area.
  • Commit the staged changes into the Git repository via the git commit command.

The core Git development team provides tooling for the command line via the the git command. Without any arguments, this command lists its options and the most common commands. You can get help for a certain Git command via the help command online option followed by the command.

git help [command to get help for]

See all possible commands, use the git help --all command.

Let’s Start

Before we start, you need to download Git and install it into your machine. Head over to https://git-scm.com and download the version according to your operating system. During the installation of Git, make sure you choose to run Git on the normal console window as well, this will enable you to run Git on your command prompt using the git command.

Once installed, open Git bash and create a working folder where you’ll test out your project and keep track of changes. Once you “cd” into the working folder, create a file and add random code to it. In my example I’ll create a file called app.js and add the following lines.

function EnterName(name) {
 return "Hi " + name;

console.log(EnterName("John Doe"));

Initializing Git Repository.

First we need to initialize a git repository into our current project folder. Every change we will make will be tracked in this repository, So type this into the command line in the current folder.

git init

The above command will create the hidden git folder as you can see below.

To keep track of the changes, you can make changes to your existing code by adding an extra line of code. To keep track of when or who added this feature, you have to add changes to an index then when satisfied with the new lines, you commit the changes from the index to the main branch. Think of the index like a platform that keeps track of how far you’ve gone before adding the incrementing lines of code collectively to the main project. To add files after making changes use this command.

git add app.js 

// To add multiple files, replace the filename with a period ( . ) to add all files.

When we are done adding our code changes like you’ve completely added a new feature or removed a bug, it’s high time we commit our changes to the master branch. Use this command to check what files are on the stage.

git status

The files with the green font are files that have been successfully added to the stage. The ones marked with red are the ones within the project but haven’t been added to the stage. This means you can have three files, modify all three but commit the changes you’ve made to only one of those files. Your Git bash should look like this. Note: i am using a different command line theme, so your result can be different from mine.

To commit the changes to master, use the following command.

git commit -m "This is the commit message"

The -m flag specifies that what follows is the commit message. This is a custom message intended to let your future self or other developers what was added in that commit. You can use something like git commit -m “Successfully fixed layout issue.” to keep track of the things you’ve added to the master branch.

Storing your files to Github

Now we will store our code online so we can access it anywhere, anytime. you can use GitHub or BitBucket for that.

Git has a default file where you can list out folders and files you don’t want to upload called the .gitignore file. All the files and folders listed in this .gitignore file are ignored automatically when items are being uploaded. You can add the file names simply by using the echo command in your terminal as shown below.

echo node_modules >> .gitignore

Once you sign up for any of these two services, there should be an option “new repository”. Click on it and a new screen will pop up. The further process will be straight forward.

Note: Private repositories cannot be pulled without owner permission while public repositories are free to be pulled by anyone.

Now, to link your remote git repository to your online repository on GitHub or BitBucket, you attach an origin to your remote git project to specify the origin will be hosted online. You’ll use this command.

git remote add origin https://github.com/username/yourrepositoryname.git

To quickly add origins, just remember your GitHub or BitBucket username, add a slash and the repository name of the repository you created. When naming a repository, don’t add the .git, it’s added by default when you simply add the name like repositoryname.

To push the code to your repo, use the following command.

git push -u origin master

This will push your app to the master branch…

To pull the code from a different machine, you’ll initialize an empty git repository then use the following command to pull the repo.

git pull https://github.com/username/yourrepositoryname.git

If there are any dependencies listed in a package.json file, all you’ll have to do then is npm install and your project is good to go.

Using this technique, you can work on a project at work and continue to add some features at home using different machines. You wouldn’t need to carry your machine everywhere or your code in a flash drive.

With this basic knowledge about Git, you can now use it to share code with a team of developers, keep track of their changes, commit changes to your project, authorize contributors, create experimental features with branches, merge code, pull code for templates and code snippets.

Leave a Reply

Your email address will not be published. Required fields are marked *