In the previous tutorial by Avinash Mishra, we discussed another automation tool that is Sikuli Automation tool and its integration with Selenium. Well, we share our automation code at central repository so that our team could take the pull of the code for further development. When we store our code at any central repository like GitHub or Bitbucket, we do it in certain branches and we use a certain mechanism to perform operations on these mechanisms. Git has commands to perform actions like push, pull, clone, etc through Git bash terminal. Thus, our agenda is on Git commands and the way to use them to perform actions on the central repository.
Before we jump to the main contents, I have some suggestions for you. You can read below articles to build your foundation on Git, Git hub and Git Bash.
- How to integrate GitHub repository with Jenkins?
- Jenkins integration with Maven project
- How to create a GitHub repository with Eclipse integration?
- A quick tutorial on Sikuli with Selenium
What is Git?
Git is a source code management and distributed version control system.
Why is Git SCM a distributed VCS?
A version control system is a software which enables the developer to work simultaneously and maintains the history of every version of the code. There are two types of version control system:
- Centralized version control system
- Distributed version control system
Let’s understand the concept of the Distributed version control system.
Git checkout the latest and complete replica of the repository. We do not need to be online to perform operations, hence, it allows us to perform certain operations offline as well, like branch creation, doing commit and other similar operations. If the server goes down then the repository can be restored from the client with the full backup for every checkout. That’s why git is called Distributed version control system. But the case is opposite with Centralized version control system, means, you cannot restore the repo when the server goes down. Also, if your central server is corrupted and you have not taken the proper backup then you will lose the entire history of project data.
Git Lifecycle
It also has a lifecycle like other tools. Here are the steps in Git lifecycle:
- Cloning the git repo as a working repository
- Adding or editing the file for a modification of working copy
- Take the others changes if required and review it
- Review the changes and commit it if everything is fine and push the changes to the repository
- If anything is wrong then correct the last changes and push it again.
Some of the important Git Commands
Now I believe you got the basic idea about Git and its tools. Now it’s time to stick to our today’s agenda, which is about Git commands used with Git Bash. Hence, let’s start with project repository initialization git commands.
How to initialize a project repository?
To initialize the project repository, we need to create the project folder first and then run the below git commands
git init
If you want to keep your project in C drive. Create the folder and name it as Project_git as shown in the below image:
If Git is already installed in your system then do right click on the local repository folder and select Git bash here option, then you see Git bash terminal opened where you can execute all your git commands.
Now the initialization part is completed.
How to clone the GitHub repository to our Local?
I have already created one demo GitHub repository and I will tell you how I cloned my GitHub repository through Git commands.
Repository HTTPS path: https://github.com/sachinandan14/Automation.git
Click on Clone or Download button and copy the repo URL as shown in the image below.
Go to git bash where you have initialized your project in the local directory then type following git commands:
git clone https://github.com/sachinandan14/Automation.git
Your project will start importing codes to your local repo from the GitHub or Bitbucket. It may prompt for username and password. Enter the credentials and click ok.
Git command to validate the Git version
git - -version
Git command to check Git status
git status
Git status displays the difference between the working directory and index.
Git command to commit changes
If you want to commit all the changes at one go.
git commit -a -m "All the changes added"
Git command to push the changes
git push <Remotename><Branchname>
git push origin master
You can validate the pushes changes by login into your GitHub account. See screenshot below:
Git command to pull changes from the repository
git pull
If your other members of your team are making changes to the repository then you can take the latest updated code by using this pull command.
If changes are made then the log of changes will be displayed on the git bash terminal, else “Already up to date” message will appear.
Git commands to check all the available branches
The following command will list all the available branches in your repository.
git branch
Git command to merge with current branch
If you want to merge with the current branch
git merge <branch>
Git command to see un-staged changes
It will show unstaged changes between the index and working directory.
git diff master
Git command to create a new branch
It will create a new branch and switched to it.
git checkout –b sachin
Git command to check logs
It will display all the commit history along with author details with date and time.
git log
Git command to switch to a different branch
git checkout <branchname>
git checkout master
Git command to delete a branch
git branch –d <branch_name>
git branch –d tutorial
Before deleting the branch make sure that you are not in deleting your working branch. It throws an error, better switch to another branch.
Git command to unstage the files.
It upstages the file.
git reset
Git command to check metadata
git show
It will show the metadata and content changes in the commit
Git command to merge one branch with another one
git merge
git merge sachin
It will merge the branch to the other branch.
Git stash command
git stash
Check Current changesets
git stash pop
Check all stash changes
git stash list
Discard the recent stash changesets
git stash drop
This was all about some of the important git commands which you may use to perform operations on your repository. You can suggest more commands as well; we will add in the upgraded version of this post.