Git

Git Commands Cheat Sheet

A practical Git command cheat sheet and beginner workflow tutorial covering setup, common commands, branches, commits, pull requests, and safe collaboration.

Git Commands Cheat Sheet thumbnail

A handy cheat sheet of the most common Git commands you will use, plus a simple end-to-end tutorial on the typical Git workflow. This guide is for beginners and anyone who wants to understand version control basics, from setting up repositories to managing commits, branches, and pull requests.

Configuration Steps

Before using Git seriously, configure your identity. This matters because every commit records who made the change.

Required For A Fresh Install

First, check whether Git is already configured:

git config --global --list

If user.name and user.email are empty, set them up:

git config --global user.name "Your Name"
git config --global user.email "[email protected]"

If you use VS Code, these settings make Git open VS Code for commit messages and merge conflict resolution:

git config --global core.editor "code --wait"
git config --global merge.tool "vscode"
git config --global mergetool.vscode.cmd "code --wait $MERGED"

Most Common Commands

These are the Git commands you will use most often when working on code, test automation, and team projects.

CommandWhat It Does
git initInitialize a new Git repository.
git clone [url]Clone a repository from a URL.
git statusCheck the status of your repository.
git add [file]Add a file to the staging area.
git commit -m "message"Commit changes with a message.
git pushPush changes to a remote repository.
git pullPull updates from a remote repository.
git branchList branches.
git checkout [branch]Switch to a branch.
git merge [branch]Merge a branch into the current branch.
git init
git clone [url]
git status
git add [file]
git commit -m "message"
git push
git pull
git branch
git checkout [branch]
git merge [branch]

Additional Commands

These commands are less frequent, but they become important once you start working with real projects, remote repositories, and code reviews.

CommandWhat It Does
git logShow commit logs.
git reset [file]Unstage a file.
git reset --hard [commit]Reset to a specific commit. Use carefully because it can discard work.
git stashTemporarily stash changes.
git stash applyApply stashed changes.
git remote -vShow remote URLs.
git tag [name]Create a tag.
git fetchFetch updates from a remote repository.
git diffShow changes between commits, branches, or files.
git rm [file]Remove a file from Git.
git mv [old] [new]Rename or move a file.
git rebase [branch]Reapply commits on top of another base branch.
git cherry-pick [commit]Apply changes introduced by a specific commit.
git bisectUse binary search to find the commit that introduced a bug.
git log
git reset [file]
git reset --hard [commit]
git stash
git stash apply
git remote -v
git tag [name]
git fetch
git diff
git rm [file]
git mv [old] [new]
git rebase [branch]
git cherry-pick [commit]
git bisect

Examples And Tutorial

This section demonstrates an end-to-end flow using the most common Git commands.

1. Initialize A New Git Repository

git init

This command creates a new Git repository in your current directory. A Git repository is where Git tracks the history of your project and stores the changes you commit.

2. Clone An Existing Repository

git clone https://github.com/username/repo.git

This command copies an existing repository from a remote URL to your local machine. "Clone" means you are creating a local copy so you can work on the project.

3. Check The Status Of Your Repository

git status

This command shows the state of your working directory and staging area. It tells you which files changed, which changes are staged, and which files are not tracked yet.

4. Add A File To The Staging Area

git add file.txt

This command adds the specified file to the staging area. The staging area is like a waiting room where your changes sit before you commit them.

You can also stage all changed files:

git add .

5. Commit Changes With A Message

git commit -m "Added file.txt"

This command saves your staged changes in the local repository. The message should describe what changed so your future self and your teammates can understand the project history.

6. Create A New Branch And Switch To It

git branch new-feature
git checkout new-feature

The first command creates a new branch called new-feature. A branch is a separate line of development. The second command switches to that branch so you can work independently from the main branch.

Modern Git also supports creating and switching in one command:

git switch -c new-feature

7. Push The New Branch To The Remote Repository

git push origin new-feature

This command uploads your branch to the remote repository, such as GitHub. "Push" means sending your local commits to the remote server so others can access or review them.

8. Create A Pull Request Or Merge Request

Go to your remote repository, such as GitHub, and create a Pull Request or Merge Request from the new-feature branch to the main branch.

This allows other people to review your changes before merging. In a professional QA automation workflow, this is where your test code gets reviewed before it becomes part of the shared codebase.

9. Merge The Branch After Approval

After the pull request is approved and merged, update your local main branch:

git checkout main
git pull origin main

This switches you back to main and pulls the latest changes. Your local main branch is now up to date.

Practical QA Automation Workflow

Here is a realistic Git workflow for test automation work:

git checkout main
git pull origin main
git switch -c feature/login-test-cases

# edit your test files

git status
git add tests/login/test_login.py
git commit -m "Add login validation tests"
git push origin feature/login-test-cases

Then open a pull request, wait for review, fix any requested changes, and merge after approval.

Common Mistakes To Avoid

  • Do not commit secrets, passwords, .env files, tokens, or private keys.
  • Do not use git reset --hard unless you understand that it can discard work.
  • Do not work directly on main for team projects.
  • Do not make commit messages like fix, stuff, or changes; write what changed.
  • Always run git status before committing or switching branches.
  • Pull the latest main before starting new work.

Free Software Testing Fundamentals

If you are learning Git because you want to move into QA or automation, start with the foundations first.

The free Software Testing Fundamentals course covers:

  • Testing basics, test cases, bug reporting, and core QA concepts
  • SQL and database testing
  • API testing
  • Jira and real team workflow

Start Learning Free

Git Commands Cheat Sheet | SuperSQA