• 6 hours
  • Easy

Free online content available in this course.

course.header.alt.is_video

course.header.alt.is_certifying

Got it!

Last updated on 6/7/23

Correct Commit Mistakes

You’ve been developing your projects for weeks, and everything’s going swimmingly. Except, one fine day, your project manager asks you to go back to a previous version of the project. He also needs to know who made which commit! 

Luckily, Git has history tools specifically designed for this type of situation. 

The log
The log

Forgotten Something? Git Reflog!

The purpose of a version control system is to save the changes made to your code. It allows you to consult your project history to:

  • Find who has contributed to what.

  • Pinpoint where bugs were introduced.

  • Undo changes that have caused problems.

Sounds great, but the history is totally useless if you don’t know how to use it! This is where the command  git log  comes into play.

git log

By default,  git log  lists commits in reverse chronological order. This means that the most recent commits appear first. This command shows every commit made, along with its SHA ID, the commit author, the date, and the commit message.

Git has an even more powerful tool, which takes logs to the extreme!

git reflog

git reflog  logs commits as well as all other local actions—your changes to messages, merges, resets… everything!  Like  git log  ,  git reflog  shows a SHA-1 ID for every action. Thanks to the SHA, this makes it very easy to go back to a specific action. This command is a lifeline if you make a mistake. To get back to any specific action, take the first eight characters of its SHA and enter the following:

git checkout e789e7c

Whoosh! You’ve gone back in time!  

Who’s Been Messing Around in Your Repository? Git Blame

If you discover a bug in your project, you’ll need to find out where it came from and who changed each line of code. This is where  git blame  comes in useful. 

The command  git blame  allows you to examine the content of a file, line by line, and find out the date when each line was changed and the name of the person who did it.

git blame myFile.php

git blame  shows the following for each line changed:

  • Its ID.

  • The author.

  • The timestamp.

  • The line number.

  • The line content.

Withgit blame, you can use your detective skills to solve crimes against your project!

You Want to Cherry-Pick Specific Commits

When you’re working with a team of developers on a medium to large project, managing changes between several Git branches can get complicated. Sometimes, you don’t want to merge a whole branch with another, you just want to choose one or two specific commits. This process is called cherry-picking!

Let’s imagine you’re working on the branch “My changes,” and you’ve already made several commits. Your colleague needs one of these changes to deliver it to the client, but not the others. In this very specific case, you can use  git cherry-pick  ! Using this command, you can select one or several commits via their SHA (these things are everywhere!) and migrate them to the main branch without merging the whole “My changes” branch.

git cherry-pick d356940 de966d4

Here, we’re taking the two commits with SHA ID d356940 and de966d4, and adding them to the main branch, without removing them from the current branch. We’re duplicating them!

Let’s Recap!

  • Using git log, you can view the commit history on the current branch.

  • git reflog  is identical to git log. This command also shows all local actions carried out.

  • You can use  git checkout  with a SHA-1 ID to go back to a specific action.

  • git blame  shows you who made which changes to a file on what date, line by line.

  • Using  git cherry-pick  with the SHA-1 ID of the commit concerned, you can select a commit and apply it to the current branch. 

You’re now equipped with all the knowledge you need to correct your mistakes in Git! Move to the last chapter of the course to recap what you’ve learned and access extra resources!

Example of certificate of achievement
Example of certificate of achievement