Git is a version control tool for tracking changes of files in working processes, which is commonly used in software development and among people in a project. Git now is integrated almost with IDE tools such as Eclipse, Android Studio, and also Katalon Studio. I prefer to use Git with command lines and in this article, I will introduce two ways of using Git, one is demo in Katalon studio and another is demo in command lines.
The basic Git workflow goes something like this:
- You modify files in your working tree.
- You stage the files, adding snapshots of them to your staging area.
- You do a commit, which takes the files as they are in the staging area and stores that snapshot permanently to your Git directory.
If a particular version of a file is in the Git directory, it’s considered committed. If it has been modified and was added to the staging area, it is staged. And if it was changed since it was checked out but has not been staged, it is modified. You can see here for more detail.
To go further more, you create a repository on the stored server (Github, Bitbucket) as below:
Three states of files: committed, modified and staged
Git defines three states for your file: committed, modified and staged. Once your files has been committed, it was safely stored in your local database, so these files were not lost. Modified means you changed the file compared to the previous commit but these files was not saved in the local database, if there is a problem occurs, your code will be lost. Staged means that you have marked the current modified version of the file to be ready for the next commit.
Working tree, staging area, and Git directory
The Git directory is where Git stores the metadata and object database for your project. This is the most important part of Git, and it is what is copied when you clone a repository from another computer.
Create a git directory by the command: C:\> Git init
The working tree stores all file in the repository
The staging area – Git add
Git stores internal everything called index, which is a snapshot of the files in your project. After you create a repo, it is empty and the index of git is empty (even if the directory already contains some files and folders).You need stage the files from your working directory into the index by using the git add command
Not only need to add files to the index, but also work if you edit a file at the working tree then you have to add those changes to the index also using the git add command.
Note that the index is a snapshot of all the files in the project, not just a list of changed files.
After adding the file to the index, you use git commit to get the content of the index and create a new commit. Commit is a purely local operation, it does not involve sending anything to a server on the network. It takes only the content of the index and saves a snapshot of the project as recorded in the index.
Similar to index, a commit is a snapshot of all the files in the project. Each commit is assigned a unique identifier (this label is generated from the SHA-1 hash of the captured content).
Because of the unique identifier of the commit label, it allows us to derive exactly what we have in the project at the time of the commit.
Push the files in the latest commit to the server repository. Before do it, you need to specify the network URL then Git refer to and place the code there, using the git remote add command.
Then using git push -u origin master to push the code. Origin means above URL , master means your current branch of the working tree (we will discuss later about branch), -u parameters means we need to provide authentication in each push times.
After that, check the repository in the server to get result.
Git Integration in Katalon Studios
Katalon integrates Git into its IDE so you may not need to use the boring commands above to control the project version. Instead of the command line, you will operate on a friendly and simple GUI interface.
First, you need enable Git in the Katalon studio:
- Go to Windows >> Katalon studio preferences
- Expand Katalon >> Git
Clone the remote repository into local machine
Note: Make sure the local machine has not existed repository yet, if existed, please choose another directory.
- Open Git on the task bar >> Select Clone project
- Fill the URL and your bitbucket authentication
- Modify files, Add, Commit, and Push upto the server
As mentioned above, in order to push files to the remote server, you need to add them to the index and commit by open Git >> Commit.
Look at the interface: Unstaged Changes contains un-index files, you need to move them into Staged Changes section by drag and drop.
Next, you fill in the commit message, which is useful when you want to find commit via the meaning name. If you just want to commit the file in local and do not push your changed files to the remote server, select Commit button, otherwise select Commit and Push button.
Katalon will do something such as checking your authentication, provide branch to push the code. After complete pushing code, you can check the result at the remote server address as below: