Tools We Love – Git Commands

  • Product Reviews

A version control system, or source control, plays an important role in any development project, including test automation. It tracks changes to the source code and provides control over them. Since Git is one of the most common tools for version control, we should get familiar with some of the useful commands for Git.

The command line interface for Git can be confusing. To help you, we’ve created this guide which lists some useful Git commands—what they mean and how to use them. Our hope is that this will help you use Git more easily on a regular basis.

There are many great clients for Git that let you use Git without running the command line. Understanding what the client is doing in the background has a direct correlation to a better understanding of how Git works.

1. Compare files

git diff will display the changes you made to your worktree since your last commit.

To compare two files in different branches, for example, type:

git diff branch1:path/to/file branch2:path/to/file

And here’s a synopsis:

2. Manage branches

  1. To list all of the branches in your repository, type
    git branch
    To list all branches, both remote branches and local branches, type 

    git branch -a
  2. To create a new branch, type
    git branch <branch>
    To create a new branch and checkout to the created branch, then type
    git checkout -b <new-branch>
  3. To rename the current branch, type
    git branch -m <branch>
  4. To safe delete a branch, type
    git branch -d <branch>
    and to force delete a branch, type
    git branch -D <branch>

And here’s a synopsis:

3. Save your uncommitted changes

git stash takes your uncommitted changes, saves them for later use, and then removes them from your current working copy.

 It is possible to reapply previously stashed changes with git stash pop.

You can also reapply the changes to your working copy and keep them in your stash with git stash apply.

It is possible to run git stash several times to create multiple stashes, and with git stash list to view them all.

To give your stashes more context, you can use git stash save "message" to add a description.

git stash drop can be used to remove a particular stash if it is no longer needed:

To remove all your stashes, use

And here’s a synopsis:

4. See git logs

With the git log command, you can browse the history of commits made to a repository. This command allows you to see a list of all Git commits, the message associated with each commit, and other details.

In shortlog mode, each commit is grouped by author, and the first line of each commit message is displayed. This makes it easy to see who has been working on what.

An ASCII graph representing the branch structure of the commit history is drawn with the --graph option. This is commonly used in conjunction with --oneline and --decorate commands to make it easier to identify which commit belongs to which branch:

And here’s a synopsis:

5. Git grep

git grep allows you to search your codebase for a phrase directly from the command line, and then it will print the lines that contain the phrase and highlight the files that contain it.

For case-insensitive matching:

git grep -i

And here’s a synopsis:

For more information

Git has many commands. These are a few of them, which will dramatically increase your productivity. For more commands you can consult this Git reference page.

Thanks for reading!

About the author

Ann Mary Jacob is an Application Developer at AOT Technologies. Her areas of expertise include Python, Vue, Node.js, and TypeScript. Besides being a techie, she’s fond of gardening and sports.