Web Development with Rails and Git Tutorial

Demo 4: Version Control Using Git and Local Repositories

▶️ Play from the beginning (~1 hour)

In this demonstration, I will illustrate how to perform some common version control tasks using a local repository (ignoring GitHub and remote repos for now).

It is first important to understand what data is associated with a local repo and how that data is structured. To that end, I have created a figure that illustrates the anatomy of a local repo.

Next, I will cover some tasks commonly performed with a local repo, explaining the Git commands used to perform them and how those command affect the repo data.

▶️ 1. Setting User Configuration

Git needs to know some information about the user to include in the log records. This data needs to be set only once.

  1. ▶️ Set the user’s name (see git config --global user.name ...)
  2. ▶️ Set the user’s email address (see git config --global user.email ...)

▶️ 2. Creating a Local Repo

  1. ▶️ Create a project file tree.
  2. ▶️ Initialize a repo in the file tree (see git init).
  3. ▶️ Stage the current version of the file tree to be committed (see git add -A).
  4. ▶️ Commit the staged version to the repo (see git commit ...).
  5. ▶️ Review the state of the repo (see git log --graph --oneline --all --decorate).

▶️ 3. Committing a New Version to a Local Repo

  1. ▶️ Modify a file in the working tree.
  2. ▶️ Review the changes made to the working tree (see git status, git diff).
  3. ▶️ Stage the changes to be committed (see git add -A).
  4. ▶️ Review the changes that have been staged (see git status, git diff --staged).
  5. ▶️ Commit the changes (see git commit ...).
  6. ▶️ Review the state of the repo (see git log --graph --oneline --all --decorate).

▶️ 4. Undoing Changes to the Working Tree

  1. ▶️ Make an “accidental” change to a file in the working tree.
  2. ▶️ Review the changes made to the working tree (see git status, git diff).
  3. ▶️ Undo the changes made to the working tree (see git checkout -- ...).

▶️ 5. Unstaging Changes

  1. ▶️ Make an “accidental” change to a file in the working tree.
  2. ▶️ Stage the changes (see git add -A).
  3. ▶️ Review the changes that have been staged (see git status, git diff --staged).
  4. ▶️ Unstage the changes (see git reset HEAD ...).
  5. ▶️ Review the changes made to the working tree (see git status, git diff).
  6. ▶️ Undo the changes made to the working tree (see git checkout -- ...).

▶️ 6. Inspecting Past Versions

  1. ▶️ View the commit log (see git log).
  2. ▶️ View the difference between two commits (see git diff).
  3. ▶️ Check out an old commit (see git checkout ...).
  4. ▶️ Inspect the working tree, which now holds the old version.
  5. ▶️ Checkout the current version (see git checkout ...).
  6. ▶️ Inspect the working tree, which now holds the current version.

▶️ 7. Undoing the Last Commit

  1. ▶️ View the commit log to get the hash for the latest commit (see git log).
  2. ▶️ Revert the most recent commit (see git revert ...).
  3. ▶️ View the commit log to see how it has changed (see git log).

▶️ 8. Undoing a Commit Further Back in the History

  1. ▶️ View the commit log to get the hash of a commit to revert (see git log).
  2. ▶️ Revert the chosen commit (see git revert ...).
  3. ▶️ Fix the merge conflicts in the working tree.
  4. ▶️ Stage and commit the change, reviewing the repo status before/after each command.

▶️ 9. Renaming a Committed File

  1. ▶️ Select an existing file to rename.
  2. ▶️ Rename the file (see git mv ...).
  3. ▶️ Stage and commit the change, reviewing the repo status before/after each command.

▶️ 10. Removing a Committed File

  1. ▶️ Select an existing file to remove.
  2. ▶️ Remove the file (see git rm ...).
  3. ▶️ Stage and commit the change, reviewing the repo status before/after each command.
© Scott D. Fleming 2018 • Made with GitHub Pages and Markdown