Because practice makes perfect.
This repository is a place to learn and practice git. Learn using the lessons in this README. Practice using any other files in the repo.
- Log
- Log the last 5 commits on your branch
- Log the last 10 commits on your branch, each with
oneline
of information - Log the last 10 commits on your branch, each with
oneline
of information, ingraph
format - Log the last 10 commits for
all
branches, each withoneline
of information, ingraph
format (depending on your repo, the output here may be the same as the previous step)
Show answer
i. git log -5 ii. git log --oneline -10 ii. git log --oneline --graph -10 ii. git log --oneline --graph --all -10
- Grep
- Find all occurences of
"mac"
in the repo - Find all occurences of
"mac"
in the repo, ignoring case - Find all occurences of
"mac"
in.txt
files, ignoring case - Find all occurences of
"mac"
in.txt
files, ignoring case, with empty lines btwn matches from different files, headers, and line numbers
- Find all occurences of
Show answer
i. git grep "mac" ii. git grep -i "mac" iii. git grep -i "mac" -- "*.txt" iv. git grep --break --heading --line-number -i "mac" -- "*.txt"
- Changing History
- Make changes and commit
- Change commit message
- Make changes and commit
- Make more changes
- Add recent changes to latest commit without changing commit message
Show answer
# make changes and commit ii. git commit --amend # make changes and commit # make more changes v. git commit --amend --no-edit
- The Three Stages of Git
- Make changes, stage them, and commit
- Undo commit only (HEAD should now reference previous commit)
- Unstage current changes
- Undo changes in working directory
Use git show
and git status
to see the effect of each command
Show answer
i. # make changes, stage them, and commit ii. git reset --soft HEAD^ iii. git reset . iv. git checkout .
- What Is A Repo?
- View the directory containing all git objects in repo
Show answer
i. tree -I "info|pack" .git/objects # OR ls .git/objects
- Git Object Model: Investigate Master
master
refers to a git object. Print the object type. Does it refer to a blob, tree, or commit?- Pretty print a summary of the object.
- List the contents of the object (hint: use
ls-tree
). - Use a different command to get the same output (hint: use the checksum of the tree from the previous command).
- Print the object type of that tree.
- Print the object type of one of the blobs in that tree.
- Pretty print the blob.
Show answer
i. git cat-file -t master (master is a reference to a commit) ii. git cat-file -p master iii. git ls-tree master iv. git cat-file -p <checksum of tree> v. git cat-file -t <checksum of tree> vi. git cat-file -t <checksum of a blob> vii. git cat-file -p <checksum of a blob>
- Git Object Model: All Refs Lead to Commits
- View all references in your git repository
cat
the checksum (commit) to which master currently points toshow
the commit message related to that checksum- Use
checkout
to create a new ref callednew-branch
- Print the checksum to which
new-branch
currently points to (should be the same asmaster
) - Make a change, stage change, commit it
- Print the checksum to which
new-branch
currently points to (should be different thanmaster
) - Delete that
new-branch
ref
Show answer
# in root directory of repositoryi. tree -C .git/refs # OR ls .git/refs
ii. cat .git/refs/heads/master
iii. git show <checksum printed in previous step>
iv. git checkout -b new-branch
v. cat .git/refs/heads/new-branch
vi. # make your changes, stage, and commit however you like
vii. cat .git/refs/head/new-branch
viii. git checkout master && git branch -d new-branch