Version Control

Version Control is the management of changes to documents, computer programs, large websites and other collection of information.

Version Controls keeps track of all the version of your project, every small change create a new version of your project

For example : You may have a Java code, and now you are saving the file as xyz.java, after couple of days you feel like some code has to be changed so you performed that changes. After a week you realize that you made changes to some other rather than required file.

What happens now, your job and the code you developed both are gone.

To avoid such kind of scenarios a tool takes a back up of your code, every time there is any change.

The tool which handles the above backups are called as Version control tools, I have listed few below:

  • GIT [ we are gonna explore ]
  • SVN
  • Bazaar
  • Mercurial

Git software

Git is a version control system for tracking changes in computer files and coordinating work on those files among multiple people.

Git is distributed control system

It is primarily used for source code management in software development, but it can be used to keep track of changes in any set of files

Git is an open source program to keep track of changes in your files. It just takes screen shots of your files when you save them, and then it compares the files for differences.

Install Git :

  • Navigate to https://git-scm.com/downloads
  • Choose the to download based on your operating system [ I am using WIN 10]
  • Open the installer and follow just standard procedure, donot change any
  • If you want you can bring the git shortcut to Desktop

User Details in Git

Setting user details like user name and email is important for tracking purpose; follow below commands to set details.

Before setting any values in git you need to open git bash terminal, --global is nothing but the scope of the user details

Set the user name to the git


								git config --global user.name "karthiQ"			
								


Verify the user name that you had set


								git config --global user.name			
								


Set the user email address to the git [ to follow this tutorial, you can use gmail account as well ]


								git config --global user.email "karthiq@chercher.tech"			
								


Verify the user email address that you had set


								git config --global user.email			
								


git-username-email-selenium

Basics of Git CMD :

It is important to learn command line/CMD commands in git, even though we may not use them along without modern work IDEs but they will be helpful

Get the current folder details:


								pwd			
								


Create a new folder,


								mkdir folderToCreate		
								


Move into a sub-folder:


								cd folderName			
								


create-folder-navigate Create any file and save it in the folder that we have created just before, In below step I am using the vim to create a file.

Create a file in git:


								touch abc.php			
								


List the files in the current folder


								ls		
								


Remove/delete a file:


								rm abc.php		
								


Step out of current directory:


								cd ..		
								


How to remove a directory:


								rmdir directoryName		
								


create-remove-git-files-folder

Create a Git Repository :

Create folder called SampleRepo and move inside the folder for creating the git repository

Convert the created folder into git repository with below command.


								git init		
								


if you see the highlighted line in the out put then your repository got created successfully. create-repository-in-git Create two files inside the repository using touch command


								touch abc.php
								index.html								
								

git status :

git status command shows the difference between the working folder and the local repository, just because we have created above two files in the current directory doesnot mean they are present in the local repository. Those two files are just present in the working directory, even though the working directory and local repository are same.

Files will be moved to local repository only when we commit them into local repository

The untracked files are nothing but the files that you have created but not committed.


								$ git status
								
								On branch master

								No commits yet

								Untracked files:
								  (use "git add ..." to include in what will be committed)

										abc.php
										index.html

								nothing added to commit but untracked files present (use "git add" to track)							
								

Stage the files :

Stage is nothing but the pre-step for committing the files, Committing files staging is important.

We can add files to stage using add command in the git.

								git add abc.php		
								


								user@DESKTOP-3U7SD8F MINGW64 ~/SampleRepo (master)
								$ git add abc.php

								user@DESKTOP-3U7SD8F MINGW64 ~/SampleRepo (master)
								$ git status
								On branch master

								No commits yet

								Changes to be committed:
								  (use "git rm --cached ..." to unstage)

										new file:   abc.php

								Untracked files:
								  (use "git add ..." to include in what will be committed)

										index.html	
								


You can differentiate the added and not added/untracked files adding-files-local-repository-git How to remove a file from the stage


								git rm --cached abc.php	
								

We can add all the untracked files into stage using .(dot) command


								git add .	
								

add-all-untracked-files-git-selenium

Commit the files to Local repository :

Committing is nothing but adding our staged files into local repository

Committing is important and you should commit whenever you feel like you have completed a unit.

commit command is used to commit the staged files, we can also set the message for the commit, messages could be your simple description about the changes, -m represents that following is a message


								git commit -m "added two basic files"	
								

commit outcome


								user@DESKTOP-3U7SD8F MINGW64 ~/SampleRepo (master)
								$ git commit -m "added two basic files"
								[master (root-commit) c9979a2] added two basic files
								 2 files changed, 0 insertions(+), 0 deletions(-)
								 create mode 100644 abc.php
								 create mode 100644 index.html
								

we can see the commit history by using log command, this will tell us what branch we are in and he author, date, commit message.


								git log	
								

outcome of the log, HEAD represents, to which repository user committed


								user@DESKTOP-3U7SD8F MINGW64 ~/SampleRepo (master)
								$ git log
								commit c9979a285091d7a4f2a35382a071c84b98bdfaee (HEAD -> master)
								Author: karthiQ 
								Date:   Sat May 19 02:37:01 2018 +0530

									added two basic files
								

Branch

A branch is a mirror of your current repo. When you branch, your current repo (usually your main branch) goes untouched. This is great when you want to create a feature in your application, because you do not have to worry about breaking a working application since

Brach is nothing but independent line of development. For example : People create dev branches from the Master Branch. Now developers will do all the edits or features in dev branch. After the edits or developments of feature is completed then one after testing the dev branch will be merged into Master branch.

We can create check the branches present in the git using git branch command, if we provide new name along with git branch it will create new branch.

The branch that we are currently in will be in different color with * asterik in the front.


								user@DESKTOP-3U7SD8F MINGW64 ~/SampleRepo (master)
								$ git branch
								* master
								


Create your first Branch:


								user@DESKTOP-3U7SD8F MINGW64 ~/SampleRepo (master)
								$ git branch
								  FirstBranch
								* master
								


Switch to newly created branch :


								user@DESKTOP-3U7SD8F MINGW64 ~/SampleRepo (master)
								$ git checkout FirstBranch
								Switched to branch 'FirstBranch'

								user@DESKTOP-3U7SD8F MINGW64 ~/SampleRepo (FirstBranch)
								$ git branch
								* FirstBranch
								  master
								


branching-git-selenium Now if you create any files, it will be created in new branch only.

Git Ignore :

Sometimes you may have files which are related with execution or some log files, these files are to be omitted when we push our source code.

Let's create files: report.log, error.log in our current working folder. With git status command you can see tracked and untracked files,


								user@DESKTOP-3U7SD8F MINGW64 ~/SampleRepo (FirstBranch)
								$ git status
								On branch FirstBranch
								Untracked files:
								  (use "git add <file>..." to include in what will be committed)

										error.log
										report.log

								nothing added to commit but untracked files present (use "git add" to track)
								


Now in above thing we always get to see these useless log file in our untracked work, if we have two log files we donot have much issues but if we have hundreds of log files then it will create bigger confusion, to avoid this kind of situation we can use git ignore.

When we want to ignore the files we have to make an entry of those files in .gitignore Below is content that I have created in .gitignore file, we can use # symbol for commenting.


								#.gitignore file content
								error.log
								report.log
								


We can also ignore files based on regex, below file content ignores all the log files


								#.gitignore file content
								*.log
								


If we try to see the tracked and untracked files, then we will not have those log files as part of untracked files.


								user@DESKTOP-3U7SD8F MINGW64 ~/SampleRepo (FirstBranch)
								$ git status
								On branch FirstBranch
								Untracked files:
								  (use "git add <file>..." to include in what will be committed)

										.gitignore

								nothing added to commit but untracked files present (use "git add" to track)
								


Just creating .gitignore file will not help us; we have to commit the .gitignore file so that it will become active

Merge

Merge is a definition within itself. If you were working on a branch in which you have your working feature built, and you want to merge that feature back to the master branch, you will need to do a merge. It combines them into one.

We use git merge command to merge two branches.

You should be in a branch, to which you are going to merge some new branch [ here master]

If you are not in a target branch then you have to checkout to that branch and then only you should use merge command.

You should make sure you donot have any uncommitted work in the secondary [FirstBranch here] before merging.


								user@DESKTOP-3U7SD8F MINGW64 ~/SampleRepo (FirstBranch)
								$ git commit -m "description of change"
								[FirstBranch ab72069] description of change
								 1 file changed, 1 insertion(+)
								 create mode 100644 .gitignore

								user@DESKTOP-3U7SD8F MINGW64 ~/SampleRepo (FirstBranch)
								$ git checkout master
								Switched to branch 'master'

								user@DESKTOP-3U7SD8F MINGW64 ~/SampleRepo (master)
								$ git merge FirstBranch
								Updating c9979a2..ab72069
								Fast-forward
								 .gitignore | 1 +
								 1 file changed, 1 insertion(+)
								 create mode 100644 .gitignore

								


Clone

A clone is when you “clone� a remote repository and receive a local copy for your own modification. Git will keep track of all your modifications locally, so you don’t have to depend on the reliability of the server.

Push

A push is how you get your local files to a remote repo so your team will have the option to pull your latest modifications.

Pull

A pull gets the latest code from a remote repository. When you do a pull, it also does a merge. A pull is basically a fetch and a merge.

Fetch

A fetch is a pull without the merge. This is helpful if you want to compare the code your team has put together before deciding to merge it to your local repository.

Fetch

Checkout lets you navigate between branches.

Good practices for pushing to the remote repository

  • Make sure to communicate what I am working on with teammates before I start any change or feature to avoid duplication of efforts.
  • Before pushing, make sure I have pulled all current files or changes from the master/remote.
  • Be sure there are no bugs in my current commit, and it is thoroughly tested.
  • Never commit anything that is not complete.
  • Use feature branches for anything but the most minor changes.
  • Commit often.
  • Use descriptive commit messages, something like “added new fileâ€? is meaningless, “added new JS file for handling login modal behaviorâ€?, for example, is much better.

BitBucket

BitBucket is a remote repository, which was later acquired by a company called Atlassian ( yeah, the same company which owns JIRA).

BitBucket enables user to use Git and Mecrucial software for integration from local repositories to remote repositories.

Features of BitBucket :


  • Pull requests and code reviews
  • Unlimited private repos
  • Branch comparison and commit history
  • Bitbucket Mac and Windows client called SourceTree; Android app called BitBeaker
  • Bitbucket for Enterprises, called Stash
  • Integration with tools like Jira, Crucible, Bamboo, Jenkins

Create Repository on BitBucket for Free

Bit bucket provides free repositories for team which has less than 5 member, if your team is bigger may be you need to buy some premium stuff.

With free itself you can create n-number of repositories, which include private and public.

Steps to Create Repository in BitBucket :


  • Navigate to https://bitbucket.org
  • Click get Started and sign up for the bit bucket by providing your email id
  • Enter you username and password
  • Verify your email address
  • Create a unique user name for you
  • Click on Create repository
  • create-repository-bitbucket
  • Provide Name for your repository and select private repository, donot forget to select Git as version control and click Create Repository button
  • repository-name-bitbucket

Clone Bitbucket Repository :

Cloning repository is nothing but copying the remote repository into our local system, similar to downloading.

Steps to clone Bitbucket repository :

  • Navigate to bitbucket repository and click + icon on the left.
  • Select Clone this Repository under get to work
  • It will open a Modal box, in that you can choose how you want to clone your repository either using https or ssh method.
  • I clone via https method
  • Copy the url, that starts with git clone
  • clone-repository-bitbucket-selenium
  • Go to your Git Bash and choose the folder where you want to download remote repository [ I am cloning into SampleRepo folder ]
  • Paste the url thing that copied in bitbucket and press enter key/return key
  • git-clone-repo-bitbucket
  • Sometimes bitbucket asks to enter password in git bash itself (password is masked to not appear, so when you enter you cannot see your password, so after entering your password press enter
  • Instead of above step, sometimes bitbucket shows an pop up to enter password [ with me this is the case] password-bitbucket
  • If everything successful then you will see below message
  • clone-successful-bitbucket

Commit and Push to Bitbucket with Git

In above created clone folder, lets create few files inside the demorepo, you can create it using file explorer itself. testfiles-create-into-remote Add the files into stage using add command in git, and commit using commit command


								user@DESKTOP-3U7SD8F MINGW64 ~/SampleRepo (master)
								$ cd demorepo/

								user@DESKTOP-3U7SD8F MINGW64 ~/SampleRepo/demorepo (master)
								$ git add .

								user@DESKTOP-3U7SD8F MINGW64 ~/SampleRepo/demorepo (master)
								$ git commit -m "first commit to remte repo"
								[master 87ed4ae] first commit to remte repo
								 2 files changed, 3 insertions(+)
								 create mode 100644 testfile.txt
								 create mode 100644 urlfile.txt
								


Push the commit into remote repository using push command.


								user@DESKTOP-3U7SD8F MINGW64 ~/SampleRepo/demorepo (master)
								$ git push
								Counting objects: 4, done.
								Delta compression using up to 4 threads.
								Compressing objects: 100% (3/3), done.
								Writing objects: 100% (4/4), 415 bytes | 207.00 KiB/s, done.
								Total 4 (delta 0), reused 0 (delta 0)
								To https://bitbucket.org/karthiq/demorepo.git
								   40bd670..87ed4ae  master -> master
								


Note : In some tutorials, you may see git push orgin master, that is also right but it didnot work.

You can verify the push by navigating into bitbucket repository in your browser under source tab. verify-the-push-to-bitbucket You can also can get the details of the person who committed the change under Commits tab commits-bitbucket

Pull the code from BitBucket

When there are more than one person working in a project, we may need to use the files created by other person.

To get the files from the bitbucket, we can use git pull command. But make sure that other person first pushes the code in to bitbucket before you try for pull.

As I am only person working on this, so I am gonna create a file in bitbucket repository for pulling to our local repository.

Navigate to our repositor; click the three dots next to clone button. Select Add file option and provide name and content. addfile-bitbucket-git Add some name and content to file demo-pull-file-bitbucket Now commit the file and provide some message for the commit in bitbucket.

Once creation is done, open your git and use git pull command


								user@DESKTOP-3U7SD8F MINGW64 ~/SampleRepo/demorepo (master)
								$ git pull
								remote: Counting objects: 3, done.
								remote: Compressing objects: 100% (3/3), done.
								remote: Total 3 (delta 0), reused 0 (delta 0)
								Unpacking objects: 100% (3/3), done.
								From https://bitbucket.org/karthiq/demorepo
								   87ed4ae..f4b93c6  master     -> origin/master
								Updating 87ed4ae..f4b93c6
								Fast-forward
								 AddFilePullDemo.text | 1 +
								 1 file changed, 1 insertion(+)
								 create mode 100644 AddFilePullDemo.text
								


At the end of message you can see 'AddFilePullDemo.text' got pulled you can ensure by navigating to the respective folder in your local system.

Pull command performs two operations 1. fetch, 2. merge.

fetch brings the remote changes to the local system.
merge merge the change from the fetch into you local files.

In next, we will learn how use an UI interface to connect bitbucket and git rather than git bash console.

SourceTree

SourceTree is freeware, which provides Graphical User Interface for version control softwares.

By default git comes with Git Bash console, which looks similar to your terminal/cmd, with terminal handling changes becomes overhead.

To avoid this we would be using the Graphical UI provided by SourceTree software.

Install SourceTree :

  • Navigate to https://www.SourceTreeapp.com/
  • Download the SourceTree software based on you operating system
  • Agree he Terms and select to Atlassian account
  • Enter your user name ( we have created on bitbucket), and password for the same
  • If you have not installed or if want to install some version controls, you can install here

Clone repository using SourceTree :

Once we complete the SourceTree installation, open the source tree software, you see below interface in win 10. source-tree-bitbucket-git Now you can clone either local repository or remote repository, I am cloning the same remote repository that we have created sometime back.

Click Clone icon in SourceTree, I hope You know how to take clone url, please do provide without the git clone and choose local folder to which you want to download the remote repository. clone-details-bitbucket Once the clone is over, you should be able to see a window similar to below one. details-clone-sourcetree

Basic Operations of SourceTree

We can perform all the operations SourceTree that we had performed using git bash, With SourceTree we can perform more operations with simple clicks.

Stage Files in SourceTree :

Create a new file in the repository that we have cloned in above step. I am creating a file called style.css and placing some text inside the file. stage-sourcetree-git-bitbucket We have create a file, now we can perform the staging operation in SourceTree.

If you open SourceTree, you can see some un-committed changes in the top pane, and the file (style.css) we have created is in the bottom pane under Unstaged files section. unstaged-files-sourcetree-bitbucket-selenium The SourceTree automatically detects the changes in the repository.

To Stage any file, we need to select the file and click stage selected or stage all if you want to stage all the files.


staged-files-sourcetree-bitbucket

Similar to stage we can un-stage the files by selecting a file and then clicking unstage selected, unstage all buttons.

Commit files in SourceTree :

We can commit the files in SourceTree by clicking the commit icon at top left corner in the SourceTree.

Once we click the commit button then we have to select the staged files to commit, and provide a decription for the changes and press commit button at the bottom of SourceTree.
commit-sourcetree-bitbucket-selenium

Branching in SourceTree :

We can create the dev branch in SourceTree by clicking the master branch and then clicking branch icon branching-sourcetree-bitbucket

Provide the branch name, also select working copy parent option to get all the details and code from the master. If you want to start working on new then you have to select 'Checkout New branch' option branch-details-sourcetree-bitbucket

We can verify which branch is currently checked out by looking at the Branches section in SourceTree. Current branch will a have circle at the front of the branch. current-branch-sourcetree-bitbucket-selenium

Merge in SourceTree :

Merge is an headache in any version control tool, because if we remove something that we have not developed then it may create a scene in the team.

Below example shows merge of two different branches without any conflicts: Lets add and commit a file in the repository, remember we are working in dev branch now. Do you remember, earlier I said we have to be in the main [master] branch to which we are going to merge some sub[dev] branch.

We are currently in dev branch, so lets switch to master branch. We can switch by right clicking on the required Branch and selecting Checkout branch. checkout-branch-sourcetree-bitbucket

To Merge the sub branch, we have to right click on the sub branch and select Merge sub branch with current branch ( we already switched to Master branch, so our current branch is master). merge-sub-branch-master-branch

We have see how to merge without any conflicts, but if we have any conflicts between files then we have to resolve them and then we have to merge.

Lets create branch called complexity from the master branch and edit content of a file. merge-conflicts-sourcetree-bitbucket

Switch to master and try to merge the sub branch (complexity) with Master, now will see below pop up. Click Close button merge-conflict-source-tree

Now select the file status tab at the bottom, you can see the file conflicts at right top corner. conflicts-sourcetree-bitbucket

In below image the first changes is with <<< which means that it is the Main branch value

Second change is with >>> which means this is sub branch value conflict-explanation

We have to keep only the change that we want to keep, some editor may give you option to choose / remove the values but if you are using normal editor then we have to remove lines manually. conflicts-removal

Once you resolve the conflicts then you only need to push the code.

Push the code :

You just need to click the Push icon in the top, it will ask for the branches to push. Select the branches the carefully and push it. push-sourcetree-bitbucket

You can verify the pushed code in bitbucket. bitbucket-sourcetree

Pull in SourceTree :

Pull is nothing but making our local repository up to date with remote repository, oull performs two operations 1. fetch, 2. merge pull-source-tree

If you select rebase instead of merge then it will remove all the save commits and un committed changes in the local repository.

Result of the pull request pull-sourcetree

Selenium With SourceTree & BitBucket & Git

Now it is time for cooking, so far we have been learning what are the raw materials required for cooking, so lets start cooking.

Steps to push the Selenium Code to Bit Bucket :

  • Create a repository in bitbucket repository.
  • create-selenium-repository

  • Open the SourceTree and clone the bitbucket repository into your local
  • clone-selenium-sourcetree-bitbucket

  • Switch your workspace into the sample folder where we have cloned our remote repository
  • switch-workspace-selenium-source-tree-bitbucket

  • Create sample java project in eclipse, and create few packages and Classes in it, you may see question marks on file name.
  • sourcetree-bitbucket

  • Before pushing, you should make sure what are the files you want to push, more specifically, what you donot want in remote repository. As per me, I want only src folder files and i want to ignore every other folder.
  • 
    								# .gitignore file
    								/*
    								SeleniumGitSourceTreeBitBucket/jars
    								SeleniumGitSourceTreeBitBucket/bin
    								!/SeleniumGitSourceTreeBitBucket/	
    								


    /* ignore everything -> in the current folder.

    SeleniumGitSourceTreeBitBucket/jars -> ignore everything under SeleniumGitSourceTreeBitBucket/jars folder

    SeleniumGitSourceTreeBitBucket/bin -> ignore everything under SeleniumGitSourceTreeBitBucket/bin folder

    !/SeleniumGitSourceTreeBitBucket/ -> donot ignore the files under this folder [ basically consider this folder for pushing]

    Folder structure: folder-structure

  • Open your sourceTree, and check the unstaged files, it should match with gitignore
  • unstaged-selenium-files

  • Stage the files present in the unstaged list using Stage All
  • staged-files-selenium-sourcetree-bitbucket

  • Commit the files using the commit icon on the top
  • commit-selenium-sourcetree-bitbucket

  • Before pushing always take a pull request, now Push the files that we have committed in last step using push icon in the top
  • push-selenium-sourcetree-bitbucket

  • Goto BitBucket repository and check whether push is successful or not, if you see the content you pushed then push was success
  • bitbucket-push-verification

How to revert/rollback the push

Reverting is nothing but you have pushed something into remote repository which was not necessary or wrong code or detail.

If it is change of one 1- 10 lines in same file means you can changes them in your local and makes new push to override the wrong details but if code affected was huge then it is better to revert back.

For reverting a particular push you need to get the commit id, you can get it from the bitbucket or from the sourcetree.


								git revert <commit id>
								# I am reverting above push.
								git revert 1f51b93
								


revert-commit-git-selenium-sourcetree-bitbucket

If you see bitbucket now you would be still seeing the files, the reason is we have not pushed the reverted files back

Once we execute the revert command then we will have files unstaged (waiting as removal of change), now stage all files and commit and push them to clear the changes. stage-delete-files

We have to verify the bitbucket that all the changes are removed revert-bitbucket

 
Join My Facebook Group
Join Group
 

About Author

Myself KarthiQ, I am the author of this blog, I know ways to write a good article but some how I donot have the skills to make it to reach people, would you like help me to reach more people By sharing this Article in the social media.

Share this Article Facebook
Comment / Suggestion Section
Point our Mistakes and Post Your Suggestions
 
Selenium-Webdriver.Com [Group]
Facebook Group · 801 members
Join Group
Learn Selenium Webdriver Concepts and Interview Questions Learn and code
 
Copyright © CherCher Tech