1. Intro
The tools we'll be using to review a repository's history are git log and git show.With git log, you'll be able to display information about the existing commits.
The git show command displays info about the given commit.
So, with this one, you provide it the commit ID, also known as the SHA, and the command displays info about just that one commit.
2. Displaying A Repository's Commits
Git Status & Opening The Project
You can see that
git status
tells us that there's "nothing to commit, working directory clean". That means we're good to go ahead and check out the project!
So open the project in your favorite code editor. If you haven't yet, take a minute or two to look at the project – look over the CSS and the JavaScript files, but look particularly at the HTML file.
3. Changing How Git Log Displays Information
You made it all this way - congrats on getting this far! Seriously! Learning Git is a challenging undertaking. I applaud you on your perseverance.
Take a look at this output from running
git log
:
We've been looking closely at all the detailed information that
git log
displays. But now, take a step back and look at all of the information as a whole.
Let's think about some of these questions:
- the SHA -
git log
will display the complete SHA for every single commit. Each SHA is unique, so we don't really need to see the entire SHA. We could get by perfectly fine with knowing just the first 6-8 characters. Wouldn't it be great if we could save some space and show just the first 5 or so characters of the SHA? - the author - the
git log
output displays the commit author for every single commit! It could be different for other repositories that have multiple people collaborating together, but for this one, there's only one person making all of the commits, so the commit author will be identical for all of them. Do we need to see the author for each one? What if we wanted to hide that information? - the date - By default,
git log
will display the date for each commit. But do we really care about the commit's date? Knowing the date might be important occasionally, but typically knowing the date isn't vitally important and can be ignored in a lot of cases. Is there a way we could hide that to save space? - the commit message - this is one of the most important parts of a commit message...we usually always want to see this
What could we do here to not waste a lot of space and make the output smaller? We can use a flag.
TIP: This isn't a course on the command line, but a flag is used to alter how a program functions. For example, thels
command will list all of the files in the current directory. Thels
command has a-l
flag (i.e.ls -l
) that runs the samels
command but alters how it works; it now displays the information in the long format (the-l
for long).Flags can be used to alter how a program functions and/or what is displayed. To learn more about command line programs and flags, check out our course Linux Command Line Basics.
git log --oneline
The
git log
command has a flag that can be used to alter how it displays the repository's information. That flag is --oneline
:$ git log --oneline
Check out how different the output is!
4. Viewing Modified Files
git log --stat
Intro
The
git log
command has a flag that can be used to display the files that have been changed in the commit, as well as the number of lines that have been added or deleted. The flag is --stat
("stat" is short for "statistics"):5. Viewing File Changes
Viewing Changes
We know that
git log
will show us the commits in a repository, and if we add the --stat
flag, we can see what files were modified and how many lines of code were added or removed. Wouldn't it be awesome if we could see exactly what those changes were?
If this isn't the best part of a version control system, I don't know what is! Being able to see the exact changes that were made to a file is incredibly important! Being able to say, "oh, ok, so this commit adds 5 pixels of border-radius to the button!".
For example, in the blog project, the commit
a3dc99a
has the message "center content on page" and modifies the CSS file by adding 5 lines. What are those five lines that were added? How can we figure out what those 5 lines are?
git log -p
The
git log
command has a flag that can be used to display the actual changes made to a file. The flag is --patch
which can be shortened to just -p
:$ git log -p
Run this command and check out what it displays.
6. Viewing A Specific Commit
Too Much Scrolling
The last few quizzes in the previous section had you scrolling and scrolling through the patch output just to get to the right commit so you could see its info. Wouldn't it be super handy if you could just display a specific commit's details without worrying about all of the others in the repo?
There are actually two ways to do this!
- providing the SHA of the commit you want to see to
git log
- use a new command
git show
They're both pretty simple, but let's look at the
git log
way and then we'll look at git show
.
You already know how to "log" information with:
git log
git log --oneline
git log --stat
git log -p
But did you know, you can supply the SHA of a commit as the final argument for all of these commands? For example:
$ git log -p fdf5493
By supplying a SHA, the
git log -p
command will start at that commit! No need to scroll through everything! Keep in mind that it will also show all of the commits that were made prior to the supplied SHA.
New Command: git show
The other command that shows a specific commit is
git show
:$ git show
Running it like the example above will only display the most recent commit. Typically, a SHA is provided as a final argument:
$ git show fdf5493
What does git show
do?
The
git show
command will show only one commit. So don't get alarmed when you can't find any other commits - it only shows one. The output of the git show
command is exactly the same as the git log -p
command. So by default, git show
displays:- the commit
- the author
- the date
- the commit message
- the patch information
However,
git show
can be combined with most of the other flags we've looked at:--stat
- to show the how many files were changed and the number of lines that were added/removed-p
or--patch
- this the default, but if--stat
is used, the patch won't display, so pass-p
to add it again-w
- to ignore changes to whitespace
7. Outro
Remember, that being able to display a repository's history can be especially helpful when you're first starting out. It's beneficial to know what's being stored in a commit, and how to view it after the commit is made.
0 개의 댓글:
댓글 쓰기