Introduction to Git & Some Basic Commands
In this blog we are going to discuss a few things that every developer, automation tester or any newbie in IT should aware of – We are going to discuss a useful concept called Git & some of its basic commands.
Before beginning the first question that comes in mind is – Hey what do you mean by Git exactly?
Simple answer Git is Distributed Version Control System (DVCS) – for newbies, this might be some fancy word, right? So let’s elaborate more on this.
Consider you have a team of 2 or more developers and they are working together to develop some piece of software.
For instance, let’s name this developer as Sachin, John, and Rakesh. All these are using Git as their DVCS meaning they all have one common code repository which is placed on remotely (Master repository) and on their local system they can do their own coding work also. (Feature branch). Every developer commits their code changes at the end of the day while working.
Now consider one scenario, John’s machine gets corrupted because of some XYZ issue. Now John does not have any code to work on. But hang on we are using Git!! We can always pull the latest changes from the remote repository. So main advantage as compared to the Centralized Version System (CVS) is each of these guys – Sachin, John, and Rakesh have a working copy of the remote code plus their own branch changes. The cherry on the cake git is open source and can support multiple OS platforms. SevenMentor is a well-known Linux training in Pune and an authorized Linux training partner of Red Hat.
Hope this is clear to you- why Git is required and what are its core advantages. Now, let’s go over common git commands which are useful for every developer.
Tip – We are assuming here that Git is already installed on the required system. If not go ahead and install it.We are using git bash to enter below-listed git commands.
- Git init
To start communicating with the remote repository we need to make our local folder as git folder. To do that we need to go inside folder and right-click and select Git Bash here. This will open a command prompt for the git bash like the below –
Enter “git init” in the bash window. This will give you the message below
Once the folder is initialized as git folder then we are ready to make any changes on local and also ready to push our changes to remote repository.
- Git clone
This command is used to download existing code which is already present on the remote repository. Remote repository could be placed on GitHub, Bitbucket etc. You need to type this command inside Git bash, which we have already open as in previous command. Below is syntax
Git clone <your remote repository url>
- Git branch
This command is very useful if there are multiple developers are working on same project. In our above example they are Sachin, John and Rakesh.
John has been assigned to develop new feature so first step that John will do is create branch locally i.e on his system where he will write a code. He will use below syntax –
Git branch <your desired branch name>
Once John develop the assigned feature he will push this new branch into the remote repository. For that he will use –
Git push -u <remote> <your branch name>
To view all branches inside our git folder we can use
- Git status
So consider John made some changes inside his local branch. Now before committing changes he want to see the current “status” of the branch. He will enter the command “Git status”
This will give information like – What changes he did, which are modified files, which are committed and which are not. In other words he will get information like
– Whether the present branch is up to date or not
– Whether he missed some changes to be committed, push or pull
– Whether files are created, modified or deleted.
- Git add
When John create, modify or delete any file these changes will get reflected in the local repository only. To include these changes John need to use git add command.
Below is syntax
To add single file
Git add <file name>
To add all changed/modified files at a once
Git add –A
- Git commit
To save our changes in local repository we generally use git commit command. We can also provide short commit message with this command to describe what we have changed in the code.
Below is syntax
Git commit –m “some meaningful commit message”
- Git Push
Git commit helps us to save your code changes into the local repository. Next thing you need to do is send these changes to the remote repository. In short, upload your code changes to the remote.
Below is syntax
Git push <remote> <name of develop branch>
So these are some basic git commands that every developer can use. Git is vast and have many other things but this article will help you to get started with.
Call the Trainer and Book your free demo class for now!!!
© Copyright 2020 | Sevenmentor Pvt Ltd.