Looking back

We have seen the log of commits, but so far only used it to describe our changes and see if there is anything new to download.

The log can also be used to:

  • Find a commit based on its ID

  • See the parents, grandparents or great grandparents of a particular commit

  • Give a commit a name (like for a software release i.e. v1.3)

  • Compare any two commits with each other

  • Start a new branch at a specific commit, rather than the head of a branch

  • Reset a branch to a previous commit, undoing any changes made after that point

let’s look at those things one at a time:


Find a commit based on its ID

The ID is shown at the top of every commit, find an ID now by looking at the log:

  • Navigate to the repository with the command line (either repo will be fine for this)

  • Type git log to bring up the log as we have done before

Show Git log

The commit ID is in the first line of each commit, in the above example you can see the first commit has an ID or 16df8a8437fb080615b56e6756b36327ad72c709.

So you need to talk to a colleague about this commit, or make a note of it and reference it later, now you know the ID of the commit you can bring it up any time using the show command.

  • Get back into the command line and your repo

  • Type in git show 16df8a8437fb080615b56e6756b36327ad72c709 (use the actual ID of the commit not this example ID)

Commit ID

You have just used the ID to bring up all the details for that commit. Easy right? I am going to make it even easier by letting you know that you don’t even need to put in the whole ID, the first 7 or so characters are enough to identity it.

And yes, I am a dick for not mentioning this before you typed it all out the first time.
 

See the parents, grandparents or great grandparents of a particular commit

So you are in your repository, and you want to know what the last commit was, remember the HEAD file can tell you that? Bring it up any time by typing git show HEAD.

But what about the commit before the current one? The parent commit, to see that we type git show HEAD^.

And the grand parent? Type git show HEAD^^.

What about great great grand parent? Just through in another ^ and you are there, or if you don’t want to keep track of ^’s use ~ and a number, like this git show HEAD~19. For the 19th ancestor.

Of course in this modern world, our commit may have more than one parent, like if it has been merged, if a commit has multiple parents, pick one (Maybe on alternate weekends) like this git show HEAD^1 or git show HEAD^2.

 

Give a commit a name (like for a software release i.e. v1.3)

That long ID Git gives each commit is not very memorable, If we have a commit that is ready for review or release or whatever, we can give it a better name, Git calls this a tag.

Get back into the command line and the repo:

  • Type git log and pick any commit ID you like, copy/remember the first 7 or so characters

  • Type q to get out of the log

  • type git tag v1.5 55300357473771f96989b81bc039375b87708fbd – Use your real ID not mine

  • Type git show v1.5

Git tag

You just created a new name for that particular commit, and as we see, it can be referenced by that tag, making it super easy to share, reference and otherwise discuss.

 

Compare any two commits with each other

If you want to know the difference between two commits, all you need to know is their IDs or tags, ,in the below screenshot I have two commits, each has a tag, one is v1.5 the other is v2.5.

To see the difference between the two type in git diff v1.5 v2.5.

Showing the difference between teo Git commits

 

Start a new branch at a specific commit, rather than the head of a branch

Do you love the way things were going at a certain point in the process? Commit 27 was great, but that was many commits ago. You don’t want to roll back the whole project to Commit 27, maybe other people are working on the project from later commits.

The good news is we can create a branch from Commit 27 even though we have moved way past it.

We will create a branch from an older commit, it is going to be called release2 and be based on a commit with the tag v2.5.

Just type git branch release2 v2.5.

You can see it works just like how we created a branch in previous chapters, but after the new branch name, we have the name of the commit we want to branch from. If we don’t supply the commit tag (or ID) then the branch will be created from the current commit instead.

Reset a branch to a previous commit, undoing any changes made after that point

You messed up, its OK, it happens. Just like real life, with Git we can go back to any previous point we like. OK Git has real life beat here.

You have gone through your commit comments and you know the last point you were happy with things, to undo all the changes you have made past this point, fire up the command line and…

Type git reset –hard 48b4f75175dd88345028647ab9052dc78790c6ac

The long number is of course the commit ID, but a tag also works.

 

What did I just learn?

  • You can bring up details for any commit using its ID git show 16df8a8437fb080615 (where the long string is the ID)

  • You can look at the parent, grandparent etc of a commit using:

    • git show HEAD^ - Add a ^ for each backward step

    • git show HEAD~19 - Add the number for every backward step

    • git show HEAD^2 – If a merged commit has multiple parents

  • Commit names can be shortened using a tag like this git tag v1.5 5b81bc039375b87708fbd

  • You can show a commit based on its tag like this git show v1.5

  • You can compare any two commits using either their tag or ID git diff v1.5 v2.5

  • Create a branch from any commit git branch release2 v2.5

  • Undo changes after a specific commit like this git reset –hard 48b4f75175dd883458790c6ac

Tags

git show
git tag
Git head
git
git reset

Add new comment

CAPTCHA
Answer the below question to help us fight spam

What is 2 + 2?