Git

Overview

You will need to become proficient in the use of GIT for managing source code files. It turns out to be very complex (and powerful). As such, don’t be worried that it is taking quite some time to feel comfortable with it. You will find yourself constantly referring to reference materials for a while.

Install

sudo apt install git

Lecture

On May 6, 2020 we had a Zoom-based Git Tutorial by Prof Nelson. The video is embedded below and you can find the Zoom chat transcript here.

Timestamps

0:00 Introduction to Git
3:03 Initializing a Git repository
4:10 Checking the status of your repository
4:40 Adding files to your repository
5:38 .gitignore files
8:05 .gitconfig files
9:20 Committing changes
14:32 Going back to previous commits
18:36 Using “git diff”
22:00 Branching
27:15 Merging
30:07 Cloning repositories
32:09 Group usage of Git with GitHub

Follow-Up Activities

Git is one of those things that you can only read so much about until you really start using it for a project, which is when you will get good with it. Thus, it probably merits only a few hours’ work to get familiar with it.

Try the Basics

Re-watch the video from above and replicate the basic steps you see there with some files of your own (make a copy into a new directory so you don’t mess with your existing good copies):

  1. Initialize a new repository.
  2. Add some files to it, and commit them.
  3. Change one or more of them, see what has changed with “git status”, see the changes with “git diff”, and commit the changes.
  4. Check the history with git log.
    • What other features does git log offer? For example, try it with --stat.
  5. Add a .gitignore file to the repo and commit it.
    • Can you have multiple .gitignore files in your repo?
    • When you add an entry in a .gitignore file, is it recursive? Ie, are all files in the repo that match the entry ignored, or only those in the current directory? Can you ignore files only in the current directory (not recursive)?

ACTIVITY

Match the git log feature on the left with its corresponding functionality on the right

  • -(number)
  • --reverse
  • --parents
  • --graph
  • --skip=(number)
  • --grep=(pattern)
  • Limit the number of commits to output
  • Output the commits chosen to be shown in reverse order
  • Print also the parents of the commit
  • Draw a text-based graphical representation of the commit history on the left hand side of the output
  • Limit the commits output to ones with log message that matches the specified pattern (regular expression)

Use git log --help

Learn About Git Aliases

  1. Find some handy git aliases online.
  2. Set some of these aliases in ~/.gitconfig and try them out for yourself!

Play With Branches

  1. Create a new branch and then check it out.
  2. Check what branch you are on by noting where it shows up in git status. Also see what git branch tells you.
  3. Now, change a file and recommit.
  4. Merge the branch’s changes back into master (you must checkout master to do this since a merge is always “merge something else into my current version”)
  5. Look at the change log again to see how master vs. your branch are represented.
  6. See the diffs between your current version and some previous version from the change log history (use the first few characters of the big hash value for that other version as the identifier)
  7. See the diffs between your current version and what is in some other branch (as identified by the branch name)

Merging and Conflicts

  1. Experiment with merging between branches.
  2. Can you forcibly create a conflict?
  3. If you start a merge and run into a conflict, how do you back out and abort?
  4. Get comfortable with the steps to resolve a conflict.

Fun With Remotes

  1. Clone a repo from Github. You could try:
  2. Check what branches you have locally. Can you view all the remote branches locally?
  3. Can you have multiple remotes? Why would you want to do that?

Git Merge vs Rebase

Learn about merge vs rebase:

Git GUI Tools

There are many great tools to visualize your Git repositories and histories. Try one out.

Git is Always Changing

To Learn More

There are hundreds of printed tutorials on the web, find some and read them. Here are some we have found:

Certify Your Skills

Git Badge

For those who believe they have mastered Git, we present the Git badge! This badge can be viewed in its entirety on Badgr.com here: Git Badge. The Git badge can be used to prove your Git knowledge to potential employers, educational institutions, or anyone else! To earn it, you’ll have to complete the Git test and use your knowledge to perform Git commands for various situations. Attempt the Git test and earn the Git badge with the button below!

EARN THE GIT BADGE

Good luck to those who attempt the test, and if you pass, congratulations! You are now certified in Git by the BYU Computing Boot Camp.