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 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
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 "firstname.lastname@example.org"
Verify the user email address that you had set
git config --global user.email
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:
Create a new folder,
Move into a sub-folder:
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:
List the files in the current folder
Remove/delete a file:
Step out of current directory:
How to remove a directory:
Create folder called SampleRepo and move inside the folder for creating the git repository
Convert the created folder into git repository with below command.
if you see the highlighted line in the out put then your repository got created successfully. Create two files inside the repository using touch command
touch abc.php index.html
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 is nothing but the pre-step for committing the files, Committing files staging is important.
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 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 .
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"
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.
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
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
Now if you create any files, it will be created in new branch only.
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)
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
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.
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.
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.
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.
Checkout lets you navigate between branches.
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.
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.
Cloning repository is nothing but copying the remote repository into our local system, similar to downloading.
In above created clone folder, lets create few files inside the demorepo, you can create it using file explorer itself. 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
You can verify the push by navigating into bitbucket repository in your browser under source tab. You can also can get the details of the person who committed the change under Commits tab
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. Add some name and content to file 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 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.
Once we complete the SourceTree installation, open the source tree software, you see below interface in win 10.
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. Once the clone is over, you should be able to see a window similar to below one.
We can perform all the operations SourceTree that we had performed using git bash, With SourceTree we can perform more operations with simple clicks.
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.
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. 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.
Similar to stage we can un-stage the files by selecting a file and then clicking unstage selected, unstage all buttons.
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.
We can create the dev branch in SourceTree by clicking the master branch and then clicking branch icon
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
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.
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.
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).
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.
Switch to master and try to merge the sub branch (complexity) with Master, now will see below pop up. Click Close button
Now select the file status tab at the bottom, you can see the file conflicts at right top corner.
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
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.
Once you resolve the conflicts then you only need to 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.
You can verify the pushed code in bitbucket.
Pull is nothing but making our local repository up to date with remote repository, oull performs two operations 1. fetch, 2. merge
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
Now it is time for cooking, so far we have been learning what are the raw materials required for cooking, so lets start cooking.
# .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]
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
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.
We have to verify the bitbucket that all the changes are removed