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:
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:
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:
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:
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.
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.
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
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:
Awesome! We learned a lot about how we can identify whats in our branches using advanced log techniques and the commit hashes!
git logis used to traverse the ancestry of each commit providing details on who, when, and what was committed.
git reflogis a history of all the actions you've taken on your local repository.
git blameis a detailed look at who added what line in the source code.
git cherry-pickcan add a commit from one branch to yours.