Master the Basics: A Git Cheat Sheet for QA Testers

As a technical tester, working with Git is an integral part of your daily routine. Mastering the basic Git commands can streamline your workflow and make collaboration more efficient. In this comprehensive Git Cheat Sheet, we will cover the most common Git commands you’ll need to know for your day-to-day work.
Table of Contents:
- Initial Git Setup
- Git Branch
- Git Staging
- Git Status
- Git Commits
- Git Push
- Git Fetch and Pull
- Git Merging and Rebasing
Step 1: Initial Git Setup
Before you can use Git, you’ll need to initialize a repository or clone an existing one. Here’s how you can do that:
- To initialize a new Git repo:
$ git init
- To clone an existing repo:
$ git clone https://github.com/<username>/<repo-name>.git <local-folder-name>
Step 2: Git Branch
When working on a new feature or bugfix, creating a new branch in Git is essential. Here’s how you can create, list, switch, and delete branches:
- To create a new branch:
$ git checkout -b <new-branch-name>
- To list all available branches:
$ git branch
- To switch to a different branch:
$ git checkout <branch-name>
- To delete a local branch:
$ git branch -d <branch-name>
- To delete a remote branch on origin:
$ git push origin --delete <remote-branch-name>
Step 3: Git Staging
Staging is the process of preparing changes for a commit. Here’s how you can stage and unstage files in Git:
- To stage individual files:
$ git add <file-name>
- To stage all files at once:
$ git add .
- To unstage a file:
$ git reset HEAD <file-name>
- To unstage all files:
$ git reset HEAD .
Step 4: Git Status
Git status provides a report on what files have been created, modified, or deleted. Here’s how you can use Git status:
- To check the status of your repo:
$ git status
Step 5: Git Commits
Committing your changes often is a best practice. Here’s how you can commit, undo, and squash commits in Git:
- To commit your changes:
$ git commit -m "<commit-message>"
- To undo your most recent commit and put those changes back into staging:
$ git reset --soft HEAD~1
- To completely delete the commit and discard any changes:
$ git reset --hard HEAD~1
- To squash multiple commits into one:
$ git rebase -i HEAD~<num-of-commits>
Step 6: Git Push
Once you’ve committed your changes, you’ll want to push them to a remote repository. Here’s how you can push your changes to a remote repo:
- To push a local branch for the first time:
$ git push --set-upstream origin <branch-name>
- To push a local branch to a different remote branch:
$ git push origin <local-branch-name>:<remote-branch-name>
- To undo your last push:
$ git reset --hard HEAD~1 && git push -f origin <branch-name>
Step 7: Git Fetch and Pull
Git fetch allows you to keep your repository up-to-date without merging other commits with your current branch. Git pull, on the other hand, merges other commits automatically. Here’s how you can fetch and pull changes in Git:
- To fetch changes from upstream:
$ git fetch upstream
- To pull a specific branch:
$ git pull origin <branch-name>
- To pull all changes and all other branches:
$ git pull
Step 8: Git Merging and Rebasing
Merging and rebasing allow you to integrate changes from one branch into another. Here’s how you can merge and rebase in Git:
- To merge the master branch into a feature branch:
$ git checkout <feature-branch-name> && git merge master
- To rebase changes from the master branch onto a feature branch:
$ git checkout <feature-branch-name> && git rebase master
Bonus: Resolving Merge Conflicts
Sometimes, merge conflicts may occur during merging or rebasing. Here’s how you can resolve merge conflicts in Git:
- Open the conflicting file in a text editor.
- Locate the conflict markers (
<<<<<<<
,=======
, and>>>>>>>
). - Edit the file to resolve the conflict, removing the conflict markers.
- Save the file.
- Stage the resolved file with
$ git add <file-name>
. - Commit the changes with
$ git commit -m "Resolved merge conflict"
.
Conclusion
This comprehensive Git Cheat Sheet is designed to help technical testers navigate Git commands with ease. By mastering these basic commands, you’ll improve your workflow, making collaboration more efficient and effective.
As you continue to develop your Git skills, consider exploring more advanced features and best practices, such as using Git hooks, configuring Git aliases, and working with submodules. Armed with this knowledge, you’ll be well on your way to becoming a Git expert in your field.