9 Basic GIT Commands Tutorial for Beginners | Learn to Master

9 Basic GIT Commands Tutorial for Beginners | Learn to Master

9 Basic GIT Commands Tutorial for Beginners | Learn to Master

Are you new to the git? Do you want to learn git commands? Did you try to learn Git and found it difficult?

No Worries.

In this tutorial, I will explain all essential git commands you should learn to start with your first project.

When I started learning Git, I read numerous websites and tutorials. The more I read, it started confusing me even more. Then I decided to write this tutorial so that everyone can understand; even if you come from a non-technical background.

For beginners, I’m explaining each and every point from starting. Right now, I’m avoiding a deep technical explanation.

git tutorial

In this tutorial, you will learn every basic you need to work on your git project, along with all required git commands.

Let’s starts with the basics first.

What is Git?

Git is the source code management tool where you can manage multiple versions of the project code.

Not familiar with the version?

Let’s take an example. You are looking for a job. You create your resume.

If you are sending the same resume to all the companies, you are losing a big opportunity.

Not every company has the same requirement. They are not looking for the same qualities in candidates for different jobs.

You have to make the changes in your resume to flash some important skills that match the requirement.

Over the period, you start creating multiple resumes.

Example:

Here are my different versions of my CV.

Aniruddha_CV_05Aug.doc
Aniruddha_CV_08Aug.doc
Aniruddha_CV_15Aug.doc
Aniruddha_CV_20Aug.doc

Every resume has some differences.

This looks very simple in this case. I can easily go back back to the older CV and add new changes.

This goes very complex if we talk about project code files then these CVs.

What are the challenges working on project source code and maintaining different versions?

  • There will be multiple team members working on the same project and source code files.
  • Your team members are sitting elsewhere and you will never know what changes they are making.

Why does your project need version control?

There can be a scenario when the testing teams raise an issue. Defects come to you and you found the issue is because of certain changes you made in the earlier version. You have to roll back to the previous version to test and fix the issue.

To avoid the overlapping of code changes done by multiple team members and to get easy access to the previous version, you need a version control mechanism.

Git is the most popular and widely accepted by the IT industries’ version control mechanism.

With the Git, it is possible to maintain multiple versions of your source code. In the future, you can revert to any of the older versions.

What is Code Repository?

If you have a team working on the project, it is good to have a remote repository of the code. The developer can access the code anywhere they are working from.

It’s a kind of centralized file system where all your source code is saved.

There are so many code repositories available on the internet. Here are some popular and widely used.

  • GitHub
  • GitLab
  • BitBucket

Among all these, GitHub is more popular. Even many of the company uses GitHub repo for hosting their project source code.

These repos provide different access and visibility. If you are working on an open-source project, you can mark the repo as public so that everyone can see the project code.

If you are working on any private or office project, you can also restrict the user accessing code repo. Only your project development team members can access and see the code.

Update: GitHub is now acquired by Microsoft.

About GitHub

Git is just a management tool and it does not have any provision to save your files.

GitHub is one of the popular source code repositories where you can store your project codes and all project-related files.

In this demo, we are considering GitHub as our code repository. Every project member can access that repository to check-in and checkout source code.

Note: Whenever I say remote repository, it is nothing but the GitHub.

Do you want to understand how Git manages multiple code versions?

The complete source code is managed through the Git command. Let’s see what are those important Git commands you need to learn.

Note: All the git commands work on any repository.

Installing Git

You can install the Git using the apt-get command in the Linux system.

sudo apt-get install git

To install a git package on your Linux system, you should have an internet connection.

If you are working on Windows or macOS system, you can download the Git installation files from the official website. After downloading, you can install just like any other software you install on your system.

Remember, Git is just a version control management tool. It does not store your code. You need a code repository to host and to store your source code.

Learn Important Git Commands

You can use the Git in any environment like Linux, Windows, and macOS. You have to use the same commands in all the environments.

Though the repository is located at remote hosting, you need Git installed on your local system. Git command gives the provision of accessing remote repository code on your local system.

Let’s see the commands one-by-one. These commands will help you master your DevOps skills.

Step 1. Configuring Git | Configure Git User

If you are doing the Git setup first time, you have to configure your identity so that all your contribution and code changes will be identified. Otherwise, if multiple developers are working on projects, no one will never know who has made certain changes.

Git stores config options in three files.

1. <repo>/.git/config

All the settings in this file will be repo specific. It is a local setting.

2. /.gitconfig

All the settings in this file will be user-specific. It is a global setting.

3. $(prefix)/etc/gitconfig

This is a system-wide setting.

If there is conflict in terms of setting in these three files, it resolves the conflict with the priority. These three files have priority from high to low (in order).

Run the following command to set your username.

git config --global user.name "<Your_Name>"

Using the –global option, you mark this setting as global for a user.

  • If you want to set the username different for each project (repo), use –local option (by default).
  • If you want to set the username for the entire system, use –system option.

Run the following command to set your email ID.

git config --global user.email "<[email protected]>"

This configuration is required only for the first time. Now, you have configured your local git account.

You can also use the editor for configuration.

git config --gloabal --edit

The next step is to initialize the git repo for your project.

Step 2. Git Init | Initialize Git Local Directory

This command is used to initialize or to create the new repository on your local system.

Use this command if you want to create a new repo for the new project.

Go to your working directory and use git command.

git init

Here is what happens when you run this command.

  • initialize the setup of new repo in the current directory
  • creates .git (hidden) directory
  • create a new master branch

This will tell the system as it’s your new local git directory where you will keep your source code.

Refer following the Git lifecycle diagram to understand further commands.

git command lifecycle

Step 3. Git Clone | Copy Content from Remote Repo to your Local Directory

Suppose there is already a project on remote repo and you want to get that code on your local git directory. Use the git clone command.

This command is used to copy or clone the complete code to your local git directory from a remote repository.

You need to provide the remote repository URL.

git clone <url-of-reomote-repository>

It will start to copy all files.

Here is the git repo URL looks like.

[email protected]:ani/my-first-project.git

Where,

  • bitbucket.org is hostname
  • ani is a user name
  • my-first-project is repo name

You can get the git URL from the hosted git service.

Once you run the clone command and everything goes well,  you can see all the source files available in your local git directory.

Now you can make as many changes as you want to your local code without impacting source code in the remote repository.

But, what if you want to push these changes to the remote repository?

You need to run the following three commands.

  • Git Checkout
  • Git Add
  • Git Commit
  • Git Push

Let’s see them one-by-one.

Step 4. Git Checkout | Create New Local Branch

Before start working on any defect or future, you need to create a new branch.

Here is the simple git command to create new branch.

git checkout -b <new_branch_name>

This command usually performs two tasks.

  • create the new local branch
  • activate (switch to) this new branch

There can be multiple branches in your local directory for each defect/feature.

Any time you can switch to any branch to work on a specific defect/feature.

git checkout <branch_name>

After that whatever changes you make will be reflected only for the activated branch.

Step 5. Git Add | Add Modified Files in Staging Area

Suppose you have created a new file or made changes in the existing source code file. It’s time to add those changes to the remote repository.

Before checking in your changes from the local repository to the remote repository,  there is a staging area where you can add files to commit.

The staging area is nothing but an intermediate space between local and remote repository. This is also called a buffer or snapshot.

With below git add command you stag the new or modified files to commit.

git add <file-name>

You can also add multiple files with a single command. Separate files with a comma.

git add <file-name1>, <file-name2>

Or you can run the add command for each file.

If you make the changes in all the files and want all the files to be added, use “*” instead of mentioning all file names.

git add *

Now all the new and modified files will be in stag for commit.

Step 6. Git Status | Check the Git Current Status

Do you want to verify if everything is fine so far?

Use the git status command to check the detailed status.

git status

you get the following information.

  • your current/activated local branch name
  • a list of all the added/modified files that are staged to commit
  • a list of all the modified files which are not staged for commit.

This command is very useful to check the status of all the modified files before you commit your changes.

Step 7. Git Commit | Commit All the Changes

It creates a new commit object with all the staged files. It is nothing but the way of binding all stagging files into one commit object in the local repo.

git commit -m "<your-message>"

Option ‘-m’ is used for adding a commit message. Usually commit message is used to add some note. You can refer this note to lean about the commit, going forward in the future.

You can also create a new commit to replace the tip of the current branch. For example, if you make new changes, after the previous commit, use the following command.

git commit --amend

Option ‘–amend’ is used to modify the last commit or to appends newly modified files to the commit object.

Now you have all the new and modified files in the local repo. Its time to push them to the remote repo.

Step 8. Git Push | Push All the Changes to the Remote Repo

Git push command is used to push all the committed changes from the local repo to the remote repo.

git push <remote-repo-url> <local-branch-name>

This command performs two major tasks.

  • create a new branch on the remote repo and
  • pushes all the committed changes to the remote repo.

Instead of mentioning the remote repo URL, you can also write “origin”. The keyword “origin” is nothing but the canonical name for the remote repo URL.

Step 9. Git Revert | Undo All the Changes

Git revert command is used to undo or revert wrongly committed changes to the Git repository. Reverting changes back involves a couple of commands, you can follow them here.

Learn Git Commands from Basic to Advance

Congratulations! You are not a noob.

This is the fundamentals of git. There are many things to learn in Git. But, this is enough to start with your git project.

Also, check the git FAQ to learn and clear your further git concepts. This can also help you in job interview preparation. Irrespective of technology, today, almost every company uses git for managing their project source code.

To be honest, the more you practice or use it in your project, you will start learning more.

If you are new to the git, it seems to be quite cumbersome activities just to change one file. But, when you do it regularly, it will be easy as Thanos snaps of his finger 😀

Let me know if you have any difficulty to understand and to learn git commands.

Keep Practicing!

 

 

 

 

 

 

 

 

 

 

4 Comments

  1. Thank you for sharing Aniruddha. I’ve been trying to get my head around git for a long time. I’m finally able to understand 😃

Leave a Reply

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