Connected Lab 4

Using Git History, Aliases, and Tags

In this lab, you'll work through some simple examples of using the git log command to see the flexibility it offers, and also create an alias to help simplify using it. You'll also look at how to tag commits to have another way to reference them.

Prerequisites

This lab assumes that you have done Connected Lab 3: Tracking Content through the File Status Life Cycle. You should start out in the same directory as that lab.

Steps

  1. Starting in the same directory that you used for Connected Lab 3, begin by making another change to the repository to make the history more interesting. Add a line to the first file you committed into the repository and then stage and commit. Note that you can use the following shortcut:
    $ echo new >> file1.c
    $ git commit -am "add a line"
  2. Look at the history you have so far in your small repository. To do this, run the log command. (In some terminals, your history may be longer than the screen and so you will need to press a key to continue. If you are paging through the log output on a Unix terminal and want to end the listing, press the q key.)
    $ git log
    $ git status –s
  3. Often when looking at Git history information, users only want to see the first line of each entry, the subject line. This is why it is important to make that first line meaningful when using Git. (Note that I do not do that in this book.)

    To see only the first line of each log message, you can use the --oneline option. Try it now.

    $ git log --oneline
  4. You can create a more complex version of the log command that includes selected pieces of history information formatted in a specific way. (Refer to Chapter 7 or the log command help page to clarify what each part of this command is doing.) Be careful of your typing: note the colon after format, the double hyphens, and the double quotes.
    $ git log --pretty=format:"%h %ad | %s%d [%an]" --date=short
  5. Because this is a lot to type, you can create an alias to simplify running this command. You do this by configuring the alias name to stand for the command and its options. Type the following, paying attention to the punctuation (double hyphens, colon, vertical bars, single and double quotes, and so on).
    $ git config --global alias.hist 'log --pretty=format:"%h %ad | %s%d [%an]"  --date=short'
  6. Run your new hist alias. You will see the same output as the original log command from step 4. If you encounter any problems, go back and double-check what you typed in step 5.
    $ git hist
  7. You can also use the log command (and your hist alias) on individual files. Pick one of your files and run the hist alias against it.
    $ git hist <filename>  
  8. You're interested in seeing the differences between a couple of the revisions. However, there are no version numbers. So we'll need to use a different way with Git. In Git, we specify revisions using the SHA1 (hash) values (the first 7 bytes are enough). In our hist output, the first column is the SHA1 value.
  9. Run the git hist alias again and find the SHA1 values of the earliest and latest lines in the history. (Yours will, of course, be different from mine in the following example.)
           $ git hist
      latest -> 1db49cf 2016-08-20 | add a line (HEAD -> master) [Brent Laster]
                ece66a5 2016-08-20 | committing another change [Brent Laster]
                8103190 2016-08-20 | update [Brent Laster]
                581c751 2016-08-20 | another update [Brent Laster]
    earliest -> c6a82d2 2016-08-20 | first commit [Brent Laster]
  10. You can use these SHA1 values similarly to how you might use version numbers in other systems. Take a look at the history between your earliest and latest commits. To do this, you run the hist alias and specify the range of values using the SHA1 values. Execute the following command, substituting the appropriate SHA1 values from the history in your repository. (Use the format, git diff <earliest SHA1>..<latest SHA1>.)
    $ git hist c6a82d2..1db49cf
  11. You see a similar history to what you saw previously. One thing to note here is that you don't see the original (first) commit. This is because when specifying ranges using the “..” syntax, Git defines that syntax as essentially everything after the first revision. (See the section on “Specifying a Range of Commits” in Chapter 9 for more information.) Note that you can also run this command against an individual file. Try the following command with your SHA1 values and the first file you added in the repository. (Use the format, git hist <earliest SHA1>..<latest SHA1> <first file>.)
    $ git diff c6a82d2..1db49cf file1.c
  12. This is useful, but finding and typing SHA1 values each time for operations like this can be cumbersome. Instead, you can use tags to point to commits, and then use those tag names instead of the SHA1 values in commands. You'll now create tags for the earliest and latest commits in your repository, using the tags first and last, respectively. The commands are as follows (using the format, git tag <tagname> <hash>):
    $ git tag first c6a82d2
    $ git tag last 1db49cf
  13. Now that you have the tags, you can use them anywhere you used the SHA1 values before. Try out the hist alias with the tags.
    $ git hist first..last
  14. You may not have thought about it, but this is giving you the history for all of the files in the repository. This is because a tag applies to an entire commit, not a specific file in the commit. To see this more clearly, add the --name-only option to the command and run it again.
    $ git hist first..last --name-only
  15. If you only want to do an operation using a tag for one file, you can simply add the filename onto the command, as in the following example:
    $ git hist first..last --name-only file1.c
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset