Important Git commands that every QA engineer should learn

Bhushan Trivedi
7 min readApr 3, 2024

--

Important Git commands for QA Engineers

This article delves into the fundamentals of Git, a powerful version control system designed to track code changes and facilitate collaboration among developers. By outlining essential commands, it aims to equip QA professionals and developers with the necessary skills to effectively manage GitHub repositories. Whether you’re new to Git or seasoned in its usage, this comprehensive guide offers valuable insights into mastering the core commands required for efficient repository management on a day-to-day basis.

Let’s Catch up all Commands step by step

1. Setting your username in Git

To set your username in Git, you can use the git config command with the --global flag to set a global configuration that applies to all repositories on your system. Here's how to do it:

  1. Open a terminal or command prompt on your computer.
  2. Use the following command to set your username:
git config - global user. name "Your Name"

Replace "Your Name" with your desired Git username.

Setting your username in Git is essential for identifying your contributions to repositories when you push commits. It helps other collaborators and services like GitHub attribute your changes accurately.

You can also change the email address associated with your git commits with the git config command.

git config --global user.email "john.doe@ymail.com"

2. Setting up a repository

Initiating a Git repository involves creating a .git subdirectory within the current working directory. This subdirectory encompasses all the essential Git metadata for the repository, including directories for objects, references, and template files, either for a brand-new repository or as a reinitialization of an existing one.

git init

3. Add files to the staging area

The git add command adds new or changed files in your working directory to the Git staging area.

3.1 Add specific files to the staging area

git add file1.ts

3.2 Add all files:

git add .

4. Repository status check

When you execute the git status command, you gain insight into the current state of both the working directory and the staging area. This command provides a comprehensive overview of which changes have been staged, and which ones haven't, and also highlights any untracked files within the repository.

git status

5. Create a snapshot of the current changes in the repository.

Record changes to the repository. This command is used to save your changes to the local repository.

  • - m to add a message to your commit
  • - a to stage all files to your commit
  • --amend to rewrite the last commit with any currently staged changes or new commit message
git commit -m "Commit message"
git commit --amend
git commit --amend -m "New message"

6. Check git Track history

git user you can use git log command in a more advanced way, just adding some keys to your git log command.

git log

Also, you can limit the number of commits to log output:

git log -5

Here’s what each part of the command means:

  • git log: This command is used to view the commit history of the repository.
  • -5: This option limits the output to the most recent five commits. You can replace 5 with any number to view a different number of commits.

When you run git log -5, Git will display information about the five most recent commits in the repository. This includes details such as the commit ID (SHA-1 hash), author, date, and commit message for each commit.

Here’s an example of what the output might look like:

git log --after="yesterday" --before="2024-04-03"
git log --author="john doe"
git log -- file1.ts
git log -S "fix"
commit abcd1234
Author: John Doe <john@example.com>
Date: Thu Mar 24 12:00:00 2024 +0100

Added new feature XYZ

commit efgh5678
Author: Jane Smith <jane@example.com>
Date: Wed Mar 23 11:00:00 2024 +0100

s Fixed bug in component ABC

...s

7. Display changes

git diff show changes between commits, commit, and working trees.

git diff

Specify a filename to display ongoing changes to its file:

git diff file1.ts

Displays changes between the branches master and develop:

git diff main..new_feature

8. Files renaming

To rename a file or directory, you can utilize the mv command, providing both the source and destination paths. The source refers to the current file or directory, while the destination denotes an existing directory where the file or directory will be relocated and renamed.

git mv directory/file1.ts directory

9. Create Branch

Branches act as distinct paths of development, allowing for parallel progress on different features or fixes.

It provides a streamlined workflow for editing, staging, and committing changes. By utilizing the git branch command, you can undertake various branch operations, including creation, listing, renaming, and deletion.

git branch branch_name

Also, you can pass some keys to git branch command:

  • git branch -m <branch> to rename a current branch.
  • git branch -d <branch> to delete the branch locally.
  • git push origin --delete <branch> to push changes to remote informing of deleting branch to the remote origin repository (require use with the previous command)
  • git branch -a to show a list of all branches.

10. Undo file changes

git restore the command helps to unstage or even discard uncommitted local changes. The command can be used to undo the effects of git add and unstaged changes you have previously added, also can be used to discard local changes in a file, thereby restoring its last committed state.

git restore newFile.ts
git restore --staged index.ts

11. Working with remote commands

git remote manage the set of tracked remote repositories.
To show a list of all remote connections:

git remote -v

To change the remote URL:

git remote set-url <url> <new_url>

To rename current connections, the next command can be used:

git remote rename <old_name> <new_name>

To delete connection:

git remote remove <remote_name>

12. Save changes to the clipboard

git stash Stash the changes in a dirty working directory away. This command takes your uncommitted changes (both staged and unstaged), and saves them away for later use.

git stash

Several keys can be added to the command:

  • git stash to stash tracked files.
  • git stash -u to stash untracked files.
  • git stash -a to stash all files (including ignored files).

The stash command saves your changes to some kind of list of changes, you can access this just by using:

git stash list

Also, you can add messages to your stash, and annotate them using git stash save "message" command:

git stash save "some comment"

Also, it supports viewing stash diffs:

git stash show

To apply stash saved changes (it will apply the last stash from the stash list):

git stash apply

And to be able to clear all stashes:

git stash clear

13. Tagging

git tag tag-specific points in a repository’s history.

git tag v1.1

To access list of tags use git tag -l. To delete just pass a specific key git tag -d v1.0. To list remote tags: git ls-remote --tags. To retag (renaming of existing tag) just send with force key: git tag -f v1 v1.1 , in this case, we renaming v1 with new v.1.1

14. Get the latest remote changes

To get the latest changes to your local there are 2 git commands: git pull and git fetch. The main difference between them is that git fetch will download the remote content but not update your local repo’s working state, leaving your current work intact. I use a git fetch command with --prune key, which is the best utility for cleaning outdated branches. Before fetching, remove any remote-tracking references that no longer exist on the remote. git pull instead will download the remote content for the active local branch and immediately execute merge onto your files. Also git pull can be used with a rebase common key: git pull -r to pull and rebase

15. Undo the last commit

In git there are two ways to undo the last changes: git revert and git reset. git revert command creates a new commit that undoes the changes from a previous commit. This command adds new history to the project. git reset is used to undo changes in your working directory that haven’t been committed yet. The reset command can be used with arguments --soft, --mixed, --hard. By default, git uses reset with --mixedkey (uncommit + unstaged changes). Frequently used by developers is --hard an option (uncommit + unstage + delete changes). When passed --hard commit history ref pointers are updated to the specified commit. This --soft is a more accurate way if you want to uncommit changes, in this case, changes are left staged.

For example to hard reset files to HEAD on git:

git reset --hard HEAD

16. Switching between commits or branches

git checkout command is used. You can switch between commits and branches, by just passing branch_name/commit_sha to git checkout command. Also, you can create a new no-existing branch using the checkout command, it will create a new branch and switch on it:

git checkout -b new_branch

To check some commits, where 5939518 is commit sha:

git checkout 5939518

17. Show who made changes to the selected file

git blame command is used for this. The main purpose is to show log of the selected file, showing who and when made changes to this file.

git blame somefile.js

It will show a list of commits made to this file, authors, dates, and commit messages. This command can be used by passing some keys like -e to show the email address of authors in a log, -L 1-7 to limit and display just 7 output lines. The main difference between git blame and git log is that blame can tell you who was the last person to modify each line of code and when.

Happy Testing !!!

References:

--

--

Bhushan Trivedi

Learning Enthusiast | Future Forward | Knowledge Sharing Guy