The Most Popular Git Commands for Testers

A
Artem Bondar
5 min read

For easier navigation, I have organized the git commands into groups or use cases that test automation engineers may encounter. 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 an email address that must be associated with your commits. This is how others will recognize your commits on git branches.

12
git config --global user.name "<your-full-name>"git config --global user.email "<your-email-address>"

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.

1
git clone <repository-url>

Get the latest code changes from all branches on the remote repository.
Important: To execute this command, you should not have uncommitted code changes

1
git pull

Upload your code to the remote repository for the new branch. For the initial upload, you will need to define a branch name to create in the remote repository.

1
git push --set-upstream origin <desired-branch-name>

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

1
git push

How to Make the Initial Setup of the Local Repository

When you don't have a remote repository to clone from and are setting up git for a brand-new project, the first thing to do is 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" or "main" will be created.

1
git init

After initialising git, add the project files to be version-controlled. This operation is also known as "staged" files. Staged files a candidate for the commit. To add all files of the project at once, use this command

1
git add -A

To add individual files, use this command

1
git add <file1> <file2>

When files are added to the "staging" area, so that 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 a best practice, a descriptive commit message should be provided to highlight the details of the committed changes.

1
git commit –m "<Type your commit message here>"

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.

1
git branch <desired-branch-name>

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

1
git checkout <branch-name>

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.

1
git checkout -b <desired-branch-name>

To delete a not-needed local branches use this command

1
git branch -d <branch-name-to-delete>

How to Set Up 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 and remote repositories.

Important: the correct repository URL always ends with ".git". Example: https://github.com/user/repo.git

1
git remote add origin <repository-url>

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

1
git remote set-url origin <a-new-repository-url>

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 a particular commit in the commit history. To "uncommit" the performed changes and go back to the desired "sha" number, use this command:

1
git reset <target-sha-number>

If you want to revert to a particular commit in 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

1
git reset --hard <target-sha-number>

When you need to revert changes on your local branch 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

1
git reset origin/<name-of-remote-branch>

How to Merge Branches

It's a typical situation when you need to merge code from different branches 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, by merging your colleague's remote branch into your local working branch. 

Using this command, you can do this. Switch locally to your working branch and execute:

1
git merge origin <name-of-branch-you-want-to-merge>

Note: Keep in mind that if merge conflicts happen, you will need to resolve them, the 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.

1
git status

View the commit history using the compact single-line view

1
git log --oneline

Review the difference between branches. Useful to preview the changes before merging branches

1
git diff <source-branch> <target-branch>

More about Git and GitHub

Learning programs at Bondar Academy include the Git and GitHub Fundamentals course, which helps you understand how to manage code and collaborate. Also, during the program's practice assignments, you will use Git to perform all those operations in a real-world workflow.

Artem Bondar

About the Author

Hey, this is Artem - test engineer, educator, and the person behind this academy.

I like test automation because it drastically reduces the workload of manual testing. Also, it's a lot of fun when you build a system that autonomously does your job.

Since 2020, I have been teaching how to use the best frameworks on the market, their best practices, and how to approach test automation professionally. I enjoy helping QAs around the world elevate their careers to the next level.

If you want to get in touch, follow me on X, LinkedIn, and YouTube. Feel free to reach out if you have any questions.