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.
- ▶️ Set the user’s name (see
git config --global user.name ...
)
- ▶️ Set the user’s email address (see
git config --global user.email ...
)
▶️ 2. Creating a Local Repo
- ▶️ Create a project file tree.
- ▶️ Initialize a repo in the file tree (see
git init
).
- ▶️ Stage the current version of the file tree to be committed (see
git add -A
).
- ▶️ Commit the staged version to the repo (see
git commit ...
).
- ▶️ Review the state of the repo (see
git log --graph --oneline --all --decorate
).
▶️ 3. Committing a New Version to a Local Repo
- ▶️ Modify a file in the working tree.
- ▶️ Review the changes made to the working tree (see
git status
, git diff
).
- ▶️ Stage the changes to be committed (see
git add -A
).
- ▶️ Review the changes that have been staged (see
git status
, git diff --staged
).
- ▶️ Commit the changes (see
git commit ...
).
- ▶️ Review the state of the repo (see
git log --graph --oneline --all --decorate
).
▶️ 4. Undoing Changes to the Working Tree
- ▶️ Make an “accidental” change to a file in the working tree.
- ▶️ Review the changes made to the working tree (see
git status
, git diff
).
- ▶️ Undo the changes made to the working tree (see
git checkout -- ...
).
▶️ 5. Unstaging Changes
- ▶️ Make an “accidental” change to a file in the working tree.
- ▶️ Stage the changes (see
git add -A
).
- ▶️ Review the changes that have been staged (see
git status
, git diff --staged
).
- ▶️ Unstage the changes (see
git reset HEAD ...
).
- ▶️ Review the changes made to the working tree (see
git status
, git diff
).
- ▶️ Undo the changes made to the working tree (see
git checkout -- ...
).
▶️ 6. Inspecting Past Versions
- ▶️ View the commit log (see
git log
).
- ▶️ View the difference between two commits (see
git diff
).
- ▶️ Check out an old commit (see
git checkout ...
).
- ▶️ Inspect the working tree, which now holds the old version.
- ▶️ Checkout the current version (see
git checkout ...
).
- ▶️ Inspect the working tree, which now holds the current version.
▶️ 7. Undoing the Last Commit
- ▶️ View the commit log to get the hash for the latest commit (see
git log
).
- ▶️ Revert the most recent commit (see
git revert ...
).
- ▶️ View the commit log to see how it has changed (see
git log
).
▶️ 8. Undoing a Commit Further Back in the History
- ▶️ View the commit log to get the hash of a commit to revert (see
git log
).
- ▶️ Revert the chosen commit (see
git revert ...
).
- ▶️ Fix the merge conflicts in the working tree.
- ▶️ Stage and commit the change, reviewing the repo status before/after each command.
▶️ 9. Renaming a Committed File
- ▶️ Select an existing file to rename.
- ▶️ Rename the file (see
git mv ...
).
- ▶️ Stage and commit the change, reviewing the repo status before/after each command.
▶️ 10. Removing a Committed File
- ▶️ Select an existing file to remove.
- ▶️ Remove the file (see
git rm ...
).
- ▶️ Stage and commit the change, reviewing the repo status before/after each command.