Further Research

Git Init Recap

Use the git init command to create a new, empty repository in the current directory.

$ git init

Running this command creates a hidden .git directory. This .git directory is the brain/storage center for the repository. It holds all of the configuration files and directories and is where all of the commits are stored.

Git Clone Recap

The git clone command is used to create an identical copy of an existing repository.

$ git clone <path-to-repository-to-clone>

This command:

  • takes the path to an existing repository
  • by default will create a directory with the same name as the repository that’s being cloned
  • can be given a second argument that will be used as the name of the directory
  • will create the new repository inside of the current working directory

 

Git Status Recap

The git status command will display the current status of the repository.

$ git status

I can’t stress enough how important it is to use this command all the time as you’re first learning Git. This command will:

  • tell us about new files that have been created in the Working Directory that Git hasn’t started tracking, yet
  • files that Git is tracking that have been modified
  • a whole bunch of other things that we’ll be learning about throughout the rest of the course 😉

 

Git Log Recap

Fantastic job! Do you feel your Git-power growing?

Let’s do a quick recap of the git log command. The git log command is used to display all of the commits of a repository.

$ git log

By default, this command displays:

  • the SHA
  • the author
  • the date
  • and the message

…of every commit in the repository. I stress the “By default” part of what Git displays because the git log command can display a lot more information than just this.

Git uses the command line pager, Less, to page through all of the information. The important keys for Less are:

  • to scroll down by a line, use j or 
  • to scroll up by a line, use k or 
  • to scroll down by a page, use the spacebar or the Page Down button
  • to scroll up by a page, use b or the Page Up button
  • to quit, use q

We’ll increase our git log-wielding abilities in the next lesson when we look at displaying more info.

 

git log –oneline Recap

To recap, the --oneline flag is used to alter how git log displays information:

$ git log --oneline

This command:

  • lists one commit per line
  • shows the first 7 characters of the commit’s SHA
  • shows the commit’s message

 

git log --stat Recap

To recap, the --stat flag is used to alter how git log displays information:

$ git log --stat

This command:

  • displays the file(s) that have been modified
  • displays the number of lines that have been added/removed
  • displays a summary line with the total number of modified files and lines that have been added/removed

 

git log -p Recap

To recap, the -p flag (which is the same as the --patch flag) is used to alter how git log displays information:

$ git log -p

This command adds the following to the default output:

  • displays the files that have been modified
  • displays the location of the lines that have been added/removed
  • displays the actual changes that have been made

 

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

 

Git Add Recap

The git add command is used to move files from the Working Directory to the Staging Index.

$ git add <file1> <file2> … <fileN>

This command:

  • takes a space-separated list of file names
  • alternatively, the period . can be used in place of a list of files to tell Git to add the current directory (and all nested files)

 

Git Commit Recap

The git commit command takes files from the Staging Index and saves them in the repository.

$ git commit

This command:

  • will open the code editor that is specified in your configuration
    • (check out the Git configuration step from the first lesson to configure your editor)

Inside the code editor:

  • a commit message must be supplied
  • lines that start with a # are comments and will not be recorded
  • save the file after adding a commit message
  • close the editor to make the commit

Then, use git log to review the commit you just made!

 

This details section of a commit message is included in the git log. To see a commit message with a body, check out the Blog project repo and look at commit 8a11b3f.

Only the message (the first line) is included in git log --oneline, though!

Udacity’s Commit Style Requirements

As I’ve mentioned, there are a number of ways to write commit messages. If you’re working on a team, they might already have a predetermined way of writing commit messages. Here at Udacity, we have our own standard for commit messages. You can check it out on our Git Commit Message Style Guide.

If you haven’t chosen a commit message style, feel free to use ours. But if you’re working on an existing project, use their existing style; it’s much more important to be consistent with your actual team than to be consistent with us!

Now, what makes a “good” commit message? That’s a great question and has been written abouta numberof times. Here are some important things to think about when crafting a good commit message:

Do

  • do keep the message short (less than 60-ish characters)
  • do explain what the commit does (not how or why!)

Do not

  • do not explain why the changes are made (more on this below)
  • do not explain how the changes are made (that’s what git log -p is for!)
  • do not use the word “and”
    • if you have to use “and”, your commit message is probably doing too many changes – break the changes into separate commits
    • e.g. “make the background color pink and increase the size of the sidebar”

The best way that I’ve found to come up with a commit message is to finish this phrase, “This commit will…”. However, you finish that phrase, use that as your commit message.

Above all, be consistent in how you write your commit messages!

 

Git Diff Recap

To recap, the git diff command is used to see changes that have been made but haven’t been committed, yet:

$ git diff

This command displays:

  • the files that have been modified
  • the location of the lines that have been added/removed
  • the actual changes that have been made

Further Research

 

Git Ignore Recap

To recap, the .gitignore file is used to tell Git about the files that Git should not track. This file should be placed in the same directory that the .git directory is in.

Further Research

 

Git Tag Recap

To recap, the git tag command is used to add a marker on a specific commit. The tag does not move around as new commits are added.

$ git tag -a beta

This command will:

  • add a tag to the most recent commit
  • add a tag to a specific commit if a SHA is passed

Further Research

 

Git Branch Recap

To recap, the git branch command is used to manage branches in Git:

# to list all branches
$ git branch

# to create a new "footer-fix" branch
$ git branch footer-fix

# to delete the "footer-fix" branch
$ git branch -d footer-fix

This command is used to:

  • list out local branches
  • create new branches
  • remove branches

Further Research

 

 

Expand On Your Git Skills

  • take the companion GitHub course
  • create a repo to track your computer’s settings – https://dotfiles.github.io/
  • develop the next, awesome feature for your personal project
  • try tackling some Git challenges with the Git-it app

 

GITHUB

Quick setup — if you’ve done this kind of thing before

…or create a new repository on the command line

echo "# test" >> README.md
git init
git add README.md
git commit -m "first commit"
git remote add origin https://github.com/thomasdmtang/test.git
git push -u origin master

…or push an existing repository from the command line

git remote add origin https://github.com/thomasdmtang/test.git
git push -u origin master

…or import code from another repository

You can initialize this repository with code from a Subversion, Mercurial, or TFS project.