10 Useful Git Commands You Should Know
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.
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](https://www.git-tower.com/blog/media/pages/posts/10-useful-git-commands/8205fa8343-1725625460/tower-search-by-author.jpg)
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".
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](https://www.git-tower.com/blog/media/pages/posts/10-useful-git-commands/e63613c257-1725625460/stash-specific-items.jpg)
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]".
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](https://www.git-tower.com/blog/media/pages/posts/10-useful-git-commands/9d66abe339-1725625460/stage-chunk.jpg)
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](https://www.git-tower.com/blog/media/pages/posts/10-useful-git-commands/3b46efa006-1725625460/tower-number-of-commits.jpg)
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](https://www.git-tower.com/blog/media/pages/posts/10-useful-git-commands/420776ef8c-1725625460/fully-merged-branches.jpg)
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](https://www.git-tower.com/blog/media/pages/posts/10-useful-git-commands/19a33fe3b2-1725625460/tower-compare-branches.jpg)
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](https://www.git-tower.com/blog/media/pages/posts/10-useful-git-commands/7c4f10bc69-1725625460/tower-filter-by-date-range.jpg)
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! 👇
Join Over 100,000 Developers & Designers
Be the first to know about new content from the Tower blog as well as giveaways and freebies via email.