• 20 hours
  • Easy

Free online content available in this course.



Got it!

Last updated on 4/25/22

Fix up individual commits with git logging techniques

Log in or subscribe for free to enjoy all this course has to offer!

Advanced logging techniques make exploring Git so much fun. Logging means keeping track of everything that's done to your project. All versions, changes, and commits are recorded and cannot be refuted! Let's dive in and have some fun.

Git log: What commits have been made on my branch?

Let's say you're working on a branch that you made on your local repository. You've added a few files and made a commit for all of them.  You made a mistake and used the  git reset  command to change your commit history. Now you want to look at your commit history on that branch.  What can you do?

This is an answer for git log!

When you use the command  git log, you will get a list of the commits that you've made in reverse chronological order. It runs through the ancestry of each one. 

This is helpful when you are looking for a specific commit, need to know the individual identifier for a commit (known as a hash), or if you want to know if you've made a specific commit. For example, a branch that has had four commits with each adding a new file with a message that identified the one that was committed:

Each commit has a  hash and a description of what file was committed on the branch
Each commit has a hash and a description of what file was committed

Remember how we said it's important to add a message to your commit?  This is why! It makes it a lot easier to know what was done.  As you can see, a hash (identifying number) won't help you figure out what you were doing - unless you memorized them! 🤓

You will see that the commits show up with the latest one at the top.  As you may remember, the latest commit is considered the tip of the branch.  In the example above, the latest is when tree.c was added with the identifier ending in fa05ec.

But what if we just wanted a list of all the commits and actions on a repo, not just a branch? 

Well, that's a job for git reflog!

Git reflog: What's been done on my repo?

"AHHH! I completely forgot what I did yesterday!" Yes, this has happened to many developers.  No need to panic. Luckily, git reflog is a handy tool that gives a list of all of the activities on a working directory in your local repository.  It will show the commits and other actions (checkout, reset, etc.) being used on the repository.  This makes it easy for you to get a complete idea of what's happened on your repo!

All of the commits also contain partial hashes, so if you have to go back and look for anything specific, it should be easy to identify. This way, if you're wondering about a specific commit that you're fixing on your local repository, you can type in this command: 

git reflog 

You'll get a full history of the work you did to revert the commit on your local repo. More specifically, you get the list of commits, and the actions that were a part of that commit.

Here's what it looks like:

Dan's branch activity
Branch activity with git reflog

Neat, but what if I want to see what I've done on the master branch? Can I use git reflog? 

Yes, you can! 🙂 You just have to checkout to the master branch and then type in git reflog to get your activities.

Git blame: So who messed up my repo?

Another command, git blame, allows you to get both the commits and the names of the users associated with them.  Now, despite what the silly name implies, it's not just used to get people in trouble. Although, since it's a way to see who added what line of code, you can figure out pretty quick who added that one line that created the bug.  😈 Watch out though, you might find out that it was you that added that buggy line of code! 😅

So how does it work? Let's say you want to track the history of all the files in a repo line by line.  More specifically, you want to see who coded mysong.c in the FixMe repo.  Type in:

git blame master -- mysong.c

Here's what it looks like:

Occlassprojects wrote every line of code in mysong.c, in case you wanted to know!
Occlassprojects wrote every line of code in mysong.c, in case you wanted to know!

In this case, the username Occlassprojects wrote the code in mysong.c. You can see that this user is the only one that has contributed to it.

Cherry-pick: Can we take that commit that's in your branch and put it in my branch too?

Cherry-pick is a fun way to run a search for a commit. Remember how all of your commits have their own unique identifier called a hash?  The characters that are unique to your commit can be used to run a search for it.  Think of it as a Google search through your entire tree of thousands of commits. 

Cherry-pick has another function. It will find the commit that you are looking for in another branch, and then commit it to yours. ✨ Let's take a look at two branches.  It looks like the master branch has two commits, and the wwbranch has five.

The master has two commits that added a file and the wwbranch has four commits that added a file
The master has two commits that added a file, and the wwbranch has four that added a file

To make this clear, let's look at the files that are in each branch.  You will see that the master branch has two files and the wwbranch has five.

Master branch has two files and the wwbranch has four files
Master branch has two files and the wwbranch has five

I want to take the commit where I added birds.c on wwbranch, and put that commit in my master branch.  This will add the birds.c file to my master branch.  But to do that, I have to find the commit for birds.c on the wwbranch using git log:

Using git log, I found the unique identifier for the commit to add birds.c on wwbranch.
Using git log, I found the unique identifier for the commit to add birds.c on wwbranch.

You can see the unique identifier, or hash, for this commit (ending in 06B3f). Now you want to add that commit with birds.c to your master branch. First, copy the hash for the commit you want to add to your master branch. Second,  checkout  to the master branch using  git checkout master.  You need to be on the branch that you are adding the commit to. While in the master branch, use the following command to add the commit to master:

git cherry-pick e5be498bd5e3d2a58cdc6d21e150e00ed6b06b3f

If successful, a new commit should be made on your master branch that adds the birds.c file. Let's see how this looks:

Commit from wwbranch added to master branch added file birds.c 

Awesome!  We learned a lot about how we can identify whats in our branches using advanced log techniques and the commit hashes!

Lets recap!

  • git log is used to traverse the ancestry of each commit providing details on who, when, and what was committed.

  • git reflog is a history of all the actions you've taken on your local repository.

  • git blame is a detailed look at who added what line in the source code.

  • git cherry-pick can add a commit from one branch to yours. 

Example of certificate of achievement
Example of certificate of achievement