10 Useful Git Commands You Should Know
September 2022 by Bruno Brito

10 Useful Git Commands You Should Know

Table of Contents

Git is such a powerful tool that there's always more to know. Today, we bring you 10 Git commands worth learning. We will also show you how easy it is to perform them in Tower!

1. Viewing the Differences between Two Commits

git diff HEAD~1 HEAD~0

The git diff command is very powerful and flexible. In the above example, it will display the diff between the latest commit and the previous one. This will, effectively, show you what happened on the last recorded change. To highlight the changes, you may also want to add the --color-words flag.

In Tower, you can simply click on any commit to access the list of modified files and visualize what changed.

Tower — View Commit

2. Searching for Commits from an Author

git log --author="Bruno Brito" --oneline

You can type the command above to get all the commits added by a specific team member. The output is easier to read if you add the --one-line flag.

In Tower, you can search for an author to quickly find all the commits from that person.

Tower — Searching for Commits from an Author
Tower — Searching for Commits from an Author

3. Amending Commits

git commit --amend --no-edit

Sometimes we add a commit and realize that we forgot to stage a file. As long as you haven't pushed anything to the remote repository yet, you can amend your very last commit, adding those missing staged files (the commit message will remain the same thanks to the --no-edit flag).

In Tower, you can perform the same operation by pressing the key to transform the "Commit" button into "Amend".

Tower — Amend Commit

4. Stashing Files

git stash --include-untracked

Stashing is an important action in Git-land. In case you need to start working on something else, the git stash command will save any modified files for later, leaving you with a clean working copy. However, if you’ve created any new, unstaged files, this command won’t touch them — unless you add the --include-untracked flag, of course!

In Tower, you can stash your entire working copy by pressing ⇧ + ⌘ + S. You can also pick individual files by right-clicking on any selected files and choosing the appropriate option. All stashes can then be retrieved in the "Stashes" view.

Tower — Stashing Individual Files
Tower — Stashing Individual Files

5. Inspecting a File

git log -p <path>

This command will definitely come in handy when you need to analyze how a file has evolved over time. It will show you not only the file diff but also all the important commit info, such as the Author, Date, and Commit Message.

You can perform the same task in Tower in several ways. My favorite is to simply right-click on a file and choose "Show File History of [FILE]".

Tower — Show File History

We make Tower, the best Git client.

Not a Tower user yet?
Download our 30-day free trial and experience a better way to work with Git!


6. Staging Chunks

git add -p

If you're a fan of small, granular commits, this one is for you — it allows you to stage parts of a changed file, instead of the entire document. Git will display chunks of the file diff and ask if you would like to stage each one of them, one by one.

If you messed up during this process, you can type git reset HEAD <file-name> to unstage a staged file.

In Tower, you can easily stage/unstage chunks with a single click.

Tower — Staging a Chunk
Tower — Staging a Chunk

7. Counting the Number of Commits the Branch Has

git rev-list --count <branch>

Curious about the number of commits the branch has? This command will return the exact number.

Tower also displays this information on top, along with the name of the branch you're working on.

Tower — Number of Commits
Tower — Number of Commits

8. Listing Merged Branches

git branch --merged

Looking for branches that can be safely deleted? This command will point you in the right direction. We have an entire blog post on this topic, should you need to learn more.

Tower also neatly displays this information for you. In the "Branches Review" view, choose the "Fully Merged" option.

Tower — Accessing Fully Merged Branches
Tower — Accessing Fully Merged Branches

9. Comparing Branches

git log --oneline main..feature/login

Branch comparison can be useful when you're looking for a certain branch's exclusive commits. In the above example, we're comparing the main branch to the feature/login branch. This article can provide more information on how to successfully perform branch comparisons.

Tower has a "Compare Branch" button that allows you to quickly compare your HEAD branch to any other local branch in your project.

Tower — Compare Branches
Tower — Compare Branches

10. Filtering Commits by Date

git whatchanged --since='2 months ago' --until='09/05/2022'

whatchanged can help you identify the commits that were added during a certain time period.

To perform the same action in Tower, simply choose "is in range" in the Date filter to add a "before" and "after" date.

Tower — Date is in Range
Tower — Date is in Range

We hope you found these tips useful!

For more Git-related resources, have a look at our Learn section, which features videos, cheat sheets, webinars, and even an online book — all for FREE! 🔥

For monthly updates and goodies, sign up for our newsletter below! 👇

Your Download is in Progress…

Giveaways. Cheat Sheets. eBooks. Discounts. And great content from our blog!