11 Useful Git Commands Every Programmer Should Know

Useful Git commands

Hey there! Some links on this page may be affiliate links which means that, if you choose to make a purchase, I may earn a small commission at no extra cost to you. I greatly appreciate your support!

When I was using Git on a daily basis, I compiled a list of useful Git commands for my own personal use. Rather than keep these Git commands to myself, I decided to write this blog post and share them with you.

1. Make New Git Branch

Let’s start out with a simple yet commonly used git command—creating a Git branch. Although this one is basic, I find myself looking it up all the time.

git checkout -b name_of_branch

2. Tag a Git Commit

Sometimes it’s useful to tag a Git commit. Consequently, you can associate an annotated Git tag with the most recent commit by issuing the following command.

git tag -a v1.0 -m "Version 1.0"

3. Search Git Logs for String

One of my favorite Git tricks is this one, especially when working with new or large repos. You can search through the entire Git log history for a specific string with this command.

git grep findme $(git rev-list --all)

4. Edit Git Commit Message

If you find yourself needing to edit a Git commit message, you can easily do so with the amend flag.

git commit --amend

5. List Changed Files for Each Commit

In addition to displaying the default author, commit hash, date, and commit message, you can also print out a list of files changed for each commit.

git log --stat

6. ASCII Graph of Commits and Branches

Here’s a beautiful way to display an ASCII graphical representation of your repo’s Git tree and history.

git log --graph --oneline --all

7. Another ASCII Graph of Commits and Branches

I present to you another way to display a textual representation of you Git tree and history. I recommend you alias this command with git hist or similar so you don’t have to memorize it. In fact, there’s so much you can do with git log. I’ll refer you to the git log docs for more.

git log --pretty=format:"%h %s" --graph

8. Diff a File Between Two Commits

Often times, you may find that you want to know what has changed in a file from one commit to the next. As a result, you can determine this by passing the commit hashes for the two commits of interest followed by the name of the file.

git diff start_hash..end_hash -- path/to/file

9. Look at Staged Changes

If you have already staged changes using git add, you can see what these changes are by issuing the following command.

git diff --staged

10. Create a Patch Based on a Git Diff

You can create a patch file based on a git diff with the following command. This is useful if you want to send somebody your changes and don’t have a remote repository set up or accessible. The result is essentially a file containing unstaged changes made.

git diff --no-prefix > patchfile

11. Apply a Git Patch

Finally, this is how you apply the patch file from the previous command.

patch -p0 < patchfile

Although this post isn’t specifically about software development, the reason I am familiar with Git commands like these are from my six years as a software engineer. Consequently, check out some of my other software tutorials here.

Also, if you have any questions, let me know below.


Meet Tony

Tony from Tony Teaches Tech headshot

With a strong software engineering background, Tony is determined to demystify the web. Discover why Tony quit his job to pursue this mission. You can join the Tony Teaches Tech community here.

Leave a Reply

Your email address will not be published. Required fields are marked *