If you are starting as a QA or want to learn new skills, learning about Git and Version Control is essential for your daily activities, and it will also be a differential for your career as a QA. Mastering Git will give you more power and effectiveness in controlling the test code as well as the application code 👩💻
This article will show you what Git and GitHub are, what are the basic concepts you should learn before starting hands-on, how to install Git and how to create a GitHub account. You will also learn the main Git workflow and the commands to each step, and finally, you will have access to a Git Cheat Sheet, where all learned Git commands are summarized.
Table of Contents
- What is Git?
- What is GitHub?
- Before We Start: Basic Concepts
- Installing Git and Creating a GitHub Account
- Git Workflow
- Git Cheat Sheet
- Conclusion
What is Git?
Version Control is vital when you need to control code updates by team members, especially in situations where more than one person is modifying the same file or when you need to roll back the code to a previous version, removing the updates made recently. In addition, a Version Control system makes a repository accessible to all members of a team or a company.
Git is the most popular and widely used Version Control system today. It’s compatible with most operating systems and development environments, whether on small projects or large company projects.
What is GitHub?
You have already heard about GitHub. By far, GitHub is the most popular code host today. Its main purpose is to allow developers or any registered user on the platform to contribute to private and/or open source projects from anywhere in the world. GitHub is also used as a source code portfolio platform.
GitHub is a platform based on Git. There are some alternatives used by companies to host code, such as Bitbucket and GitLab. For learning purposes, in this article, we will use GitHub as our code host platform.
Before We Start: Basic Concepts
For a better Git understanding, there are some important concepts that need to be clarified before we start. In this section, you will better understand what local and remote repositories are 💡
Local Repository
Local code is what you do on your computer. Everything you build, everything you develop, everything you update, is your local code. This local code is in your local repository, in your own machine.
Remote Repository
The server code is when you feel comfortable sharing your local code with the whole team, so you send the code from your local repository to your remote repository. After being in the remote repository, the code could be accessed by everyone and is now protected from loss.
Installing Git and Creating a GitHub Account
In this section, you will learn how to install Git and how to create your GitHub account to get started using Git 🔍
Git Setup
To install Git on Windows, download the latest stable version available from here.
After downloading the installer, just follow each step, keeping the default settings. After the installation, open your Windows search tool (Windows + S) and then type git bash and click ENTER to open it. Then, you will need to configure the name and email that will be associated with your activities.
If everything worked fine, you would not get any error message, meaning the settings were done successfully.
GitHub Account Creation
To create your GitHub account, simply go to the GitHub official website, and follow the steps below:
- Click on Sign Up
- Fill in your email and click on [CONTINUE]
- Fill in a password and click on [CONTINUE]
- Choose a username and click on [CONTINUE]
Right after this last step, GitHub asks if you would like to receive updates and product information via email, which is up to you. Also, a captcha-style check is shown to ensure you are not a robot. To finish:
- Click on [CREATE ACCOUNT]
After that, GitHub will ask you to enter a code you received by email and then it will ask how many people will use this account (you can select Just Me, it does not matter) and then what are the main features you want to use (you can select according to your purposes).
Choose the account type. A free account is enough for learning and personal use of GitHub. Congratulations, you now have a GitHub account!
Advice: I suggest you follow the steps in this GitHub Tutorial to set up an SSH key for your GitHub account.
Git Workflow
This section will show you which are the main commands that you can use throughout the Git workflow. You will learn how to perform common everyday tasks using Git through the command line ✍
Firstly, I suggest you create a workspace folder on your C:\drive. This is the place you will have all your repositories. It is not mandatory, but it is a best practice, in order to organize your work and to not get lost when you need to know where a particular repository is.
After creating your workspace, you can enter it in this new folder.
Create a Repository
To create your first repository, follow the steps below.
- Create a new folder with the repository name (e.g. my-first-git-repo)
- Enter in the created folder
- Run git init command to create the repository
You can check the (master) beside your repository path, related to the master branch. The master branch is where your application’s main code will be. It is the parent branch that is above all the others and where all the branches must converge.
Advice: Any time you want to know where you are, just look to the blue information besides your repository path, as shown in the above picture.
- In your GitHub account, create your repository by clicking on [CREATE REPOSITORY].
- Fill in the form with your repository name and click on [CREATE REPOSITORY]
The repository is now created! Copy the repository HTTPS URL, we will need it before pushing our code.
Add Files to the Repository and Check Repository Status
To illustrate the example of adding files to a repository, firstly I created a basic HTML file and saved it in our repository folder.
Here you can see the new file in the repository folder.
You can use the git status command to verify the status of your local work.
In this screen, you could see that the index.html file is untracked by Git, which recommends you add it. You can do this using git add command. This way, all files will be added to Git. Using git add <filename> will add only a specific file. In this example, we will use the first command:
If you request the Git status again, you can see that the file is now tracked by GIT and can now be committed.
Advice: This command must always be used before a commit.
Commit Code
Now it is time to commit your code. We can think that a commit is like a snapshot or a milestone along the timeline of a project. To make a commit, use git commit command.
The text inside the double quotes and after -m tag is the commit message informing what you did in that code. It is important that it is a well-described message because at any time you can know what was done in that code, just through this message.
Push Code
Before pushing your code, you should set your remote repository using git remote add followed by origin and the repository link https://github.com/your-github-user/your-git-repository-name.git.
Now you can send your code to the remote repository. You should push your code using the git push command, followed by origin (related to the remote repository and the name of your branch, in our case, the master branch.
Create a Branch
When you want to build something new or make an adjustment to something that is already on the master branch, you should create a new branch to add this new code. Development best practices suggest that a branch should only contain the specific code you want to add to the application at that moment.
For example, imagine you want to build a new user functionality into your application. You must create a branch just to develop this code. It makes it easier for you to do your work, and for your teammates who will later review your code.
To create a new branch named feature_one, and to switch to it to start developing your code, use git checkout command followed by -b tag and the branch name.
Switch, Diff Check, and Merge Branch
In order to use the git diff command, firstly I will update our index.html file. I changed line 7 and added line 10, as you can see in the picture below.
Add, commit and push your code, as you have learned earlier in this article. Now you can switch back to the master branch using git checkout command.
Before merging the feature_one branch inside the master branch, we can check the diff between both branches, using git diff command.
Then, you can merge the feature_one branch into master, through git merge command.
Pull Code
It is always important to make sure that your branch contains the most up-to-date code of the master branch, in addition to your changes. To do this, use the git pull command.
List Branches
You can list all the branches in your repository using the git branch command.
Delete Branch
After you finish the development on a specific branch and your code has been merged into the master, you can delete this branch using the git branch command followed by -d tag and the branch name.
View Logs
It is possible to check all the activities in a repository. To do this, use the git log command.
Advice: The yellow sequence of characters is the commit id. This will be important when tagging your code.
Create Tag
It is always important to create checkpoints in your code. For this, we have tags. You can create a tag using the git tag command, followed by the name of the tag you want to use, and the first 10 characters of the commit id you want to reference.
Checkout a Repository
In this article, you learned how to create your own repository, and used git commands in it. However, you can download any public repository from other developers and companies for your use, of course, you must pay attention to the repository copyrights. You can clone a repository following the steps below.
- Go to the GitHub page of the repository and click on [CODE]
- Copy the HTTPS URL to be cloned
- Use the git clone command in your workspace
Now you have that repository in your machine and can start using it.
Git Cheat Sheet
In this section, you will get access to the Git Cheat Sheet, a great summary of all the commands that have been explored throughout this article. It is a quick-access resource that you can use every day, whenever you need to remember some of the most important commands 📝
Conclusion
We already know that a QA must know much more than testing. Test automation is an activity performed in most companies today, and learning about Git commands is differential in the QA career.
In this article, we learned about Git and GitHub. We also learned the concepts of local repository and remote repository, as well as the most used Git commands that can be used in your daily work. Finally, you had access to a Cheat Sheet with all the commands presented in this article, for easy access whenever you need it.
In addition to knowing about Git commands, many tools have integration with GitHub and consequently are using Git, facilitating the Version Control. The TestProject tool easily integrates with GitHub, and you can see more about it at this link.
Knowing the commands and how they work will give you more power to manage your projects and to understand what is going on when you are controlling the version of your code 💪