Introduction to Git

Why Git

Keeping track of file versions is hard.

What is Git?

So what is Git, and why does it help us?

Above all else, Git is a fast version control system, that allows you to efficiently handle projects large and small.

Here are some problems we face as developers, and how git solves them:

Reverting to past versions

Git allows us to make save points at any time. These save points are called 'commits'. Once a save point is made, it's permanent, and allows us to go back to that save point at any time.

Keeping track of what each version 'meant'

Every commit has a description (commit message), which allows us to describe what changes were made between the current and previous commit. This is usually a description of what features were added or what bugs were fixed.

Additionally, git supports tagging, which allows us to mark a specific commit as a specific version of our code (e.g. '2.4.5').

Comparing changes to past versions

It's often important to see content of the actual changes that were made. This can be useful when:

  • tracking down when and how a bug was introduced
  • understanding the changes a team member made so you can stay up-to-date with progress
  • reviewing code as a team for correctness or quality/style

Git allows us to easily see these changes (called a diff) for any given commit.

Fearlessness in making changes

In developing software, we often want to experiment in adding a feature or refactoring (rewriting) existing code. Because git makes it easy to go back to a known good state, we can experiment without worrying that we'll be unable to undo the experimental work.

Three components of a git repository

Claymation, Wallace & Gromit

  1. The working directory
    • git init creates a git repo inside current working directory
    • git init nameofrepo creates a new folder and a git repo inside that
    • git status
  2. The staging area
    • git add . adds changes from the working directory to the staging area
    • git add <filename> adds changes to filenames specified from the working directory to the staging area
  3. The repo or commit
    • git commit -m "commit message" adds changes in staging area to the repository
    • git log shows

Intro to GitHub

GitHub is a social network that allows developers to host remote repositories. A remote repository is a publicly (sometimes privately) accessible copy of a local repository.

Key Terms

  • remote - another repository that can be syncronized with a remote
  • upstream - the name for a remote read-only repository
  • origin - the name for a remote read-and-write repository
  • github - a service that hosts git remote repositories, and provides a web app to interact / collaborate on them
  • fork - make a copy of a remote repo on github.
  • clone - download an entire remote repository, to be used as a local repository
  • fetch - downloading the set of changes (commits) from a remote repository
  • merge - taking two histories (commits),
  • pull - fetching changes and merging them into the current branch
  • push - sending changes to a remote repository and merging them into the specified branch
  • pull request - ask the upstream maintainer to pull in changes from origin.
  • merge conflict - when two commits conflict, and thus can't be merged automatically.

We Do

  1. Generate ssh keys by following these instructions: https://help.github.com/articles/generating-an-ssh-key/
  2. Let's work through this step by step Git tutorial: http://gitref.org/

You Do (Exercise)

Replace yourusername below with your username when running commands.

  1. Go to your Github profile (github.com/yourusername) and create a new repository called test-repo by clicking on the plus sign on the top right.
  2. Now you should have a new repository at https://github.com/yourusername/test-repo.
  3. Clone the repository using the SSH link not the HTTPS link. The command looks like this:

    git clone git@github.com:yourusername/test-repo.git

    It will say "You appear to have cloned an empty repository."

  4. Add a blank file to the repository using the touch command. Be sure that you're current working directory (pwd) is the repository test-repo.

    touch README.md
  5. Edit README.md using your favorite text editor (mine is Sublime Text 3) and add one line with the text, "Hello GA".

  6. Run git status to view the changes. You will see one new "Untracked File".
  7. Run git add README.md followed by git status. You will see "Changes to be committed" and on the next line "new file: README.md".
  8. Run git commit -m "initial commit" followed by git push and git status. You will see "Your branch is up-to-date with 'origin/master'."
  9. Run git remote -v to see what "origin" is defined as.
  10. Navigate to the git repository's url on github: github.com/yourusername/test-repo to see your changes.
  11. Edit "README.md" in your favorite text editor to add a new line, "Goodbye GA". You will see "Changes not staged for commit".
  12. Run git add README.md and git status.
  13. Run git commit -m "my second commit" followed by git status and then git push.
  14. Observe the changes online once again. Look at the diff for your last commit.