The Most Popular Git Commands for Testers

Mar 5 / Artem Bondar

Git Commands Groups

For easier navigation, I have placed the git commands in different groups or use cases that test automation engineers may have. Navigate on this page using clickable links.
The angel braces <> in the commands below should be ignored. It's a placeholder that you have to replace with your data.

 How to Configure Git

Configure a username and email that has to be associated with your commits. This is how others will recognize your commits on git branches.

 How to Interact with the Remote Repository

Download (clone) the project from the remote Git repository to your computer. The correct repository URL always ends with ".git". This command is used only once when you set up the remote project on your computer.
Get the latest code changes from all branches on the remote repository.
Important: To execute this command you should not have uncommitted code changes
Upload your code to the remote repository for the new branch. For the initial upload, you will need to define a branch name that should be created on the remote repository.
When a remote repository branch is established, you can use a simple push command to upload commits to the branch on the remote repository
Note: for this command, you may need to provide the valid access credentials

 How to Make Initial Setup of the Local Repository

When you don't have a remote repository that you have cloned from and making the git setup for the brand new project, the first thing to do is to initialize the git repo in the project folder. A ".git" folder will be created in this folder as a result. A new default branch with the name "master" will be created.
After the git initialisation need to add the project files to be version-controlled. This operation is also known as "staged" files. Staged files a candidates for the commit. To add all files of the project at once use this command
To add individual files use this command
When files are added to the "staging" area, so we want them to be version-controlled, we can create a "commit" with those files on the git branch. Commit - is a saved snapshot of changes for the staged files on the git branch. As the best practice, a descriptive commit message should be provided to highlight details of committed changes.

 How to Operate with Git Branches Locally

As a best practice, never work on the default branch. For each new development initiative create a new branch
To create a new branch use this command.
To switch to any available branch (for example after you create a new branch), you can use this command.
Important: All file changes should be committed before switching to a different branch
To create a new branch and switch to this branch right away, you can use a single command instead of two separate commands shown above.
To delete a not needed local branches use this command

 How to Setup the Remote Repository

When you want to upload your new local project to the remote repository, first you need to create a new empty repository on the remote server, GitHub for example. Then you need to configure the git connection between the local repository and the remote repository.
Important: correct repository URL always ends with ".git". Example:
If you cloned the project from the remote repository but would like to switch the remote repository to the new one (for example to your private repository), you can update the remote repository URL with this command
After that, you can interact with the repository using the pull and push commands described here.

 How to Reset/Revert Changes on Git Branch

Sometimes you may need to "undo" the commits on your branch and go back in time to the particular commit in the commit history. To "uncommit" the performed changes and go back to the desired "sha" number, use this command:
If you want to revert to a particular commit back on the commit history and you would like to delete all commits after the target commit, you can use this command. 
Note: make sure you understand what you are doing
When you need to revert changes on your local branch up to the state that is currently on the remote branch. So the commits of local and remote branches will be synchronized, and all local changes will be unstaged

 How to Merge Branches

It's a typical situation when you need to merge code from different branches together into a single branch. Normally this operation is performed on a remote repository using a pull request. But you can also do this manually using git, for example merging the code from the remote branch of your colleague to your local working branch. 
Using this command you can do this. Switch locally to your working branch and execute:
Note: Keep in mind that if merge conflicts happen, you will need to resolve it, otherwise merge will not be successful

 Different Useful Service Commands

This command gives you an understanding of the current git status. If you are a beginner, execute this command after any other command, it will help you to understand the status of things instead of assuming it.
View the commit history using the compact single-line view
Review the difference between branches. Useful to preview the changes before merging branches

More about Git and GitHub

Every course includes a section with lectures about Git and GitHub, since knowing how to work with Git is an essential skill for every test automation engineer. You can preview this section for free in the SDET with Playwright course for example. Also, you can check the Git and GitHub lessons on the YouTube channel.