Lecture 07 - More Git Commands, CLI and Git Practice
push
and pull
changes to and from remote repositories.gitignore
to avoid tracking certain filesgit branch
and git checkout
clone
and fork
repositoriesgit checkout
and git reset
Source: Lodato (2010)
Today we will cover:
git diff
git commit --amend
git cherry-pick
😅git rebase
and its uses
git diff
?git diff
shows you what has changed between commits, branches, or your working directory+
for additions and -
for deletionsmy-project
repository01-data-cleaning.py
filediff --git a/file b/file
shows Git is comparing two versions of the same file (a
= original, b
= modified)--- a/file
and +++ b/file
indicate the original file (before) and new file (after) being compared@@ -0,0 +1 @@
shows the line numbers affected, with +
lines indicating additions and -
lines indicating deletions. -0,0
means that the original file had no lines, and +1
means one line was addedgit diff
commandsBasic diff commands:
git diff
: shows unstaged changes in working directorygit diff --staged
: shows staged changes ready to commitgit diff HEAD
: shows all changes since last commitgit diff --name-only
: shows only filenames that changedComparing commits:
git diff commit1..commit2
: compares two specific commitsgit diff branch1..branch2
: compares two branchesgit diff --stat
: shows summary of changes (files modified, insertions, deletions)@@ -1,4 +1,4 @@
means that in the original file, lines 1 to 4 were present, and in the new file, lines 1 to 4 are also present, but with some changesgit commit --amend
?git commit --amend
allows you to modify your last commitAmending the commit message:
Adding forgotten files:
Both together:
Important rules:
git revert
insteadgit reset
git reset
can move the HEAD pointer to a previous commit--soft
keeps changes in the staging area--hard
discards all changes after the specified commitgit cherry-pick
?git cherry-pick
allows you to pick specific commits from one branch and apply them to anothergit cherry-pick
Basic cherry-pick:
# Cherry-pick a single commit
git cherry-pick abc1234
# Cherry-pick multiple commits
git cherry-pick abc1234 def5678
# Cherry-pick a range of commits
git cherry-pick abc1234..def5678
After cherry-picking:
Handling conflicts:
# If conflicts occur during cherry-pick
git cherry-pick --continue # After resolving conflicts
git cherry-pick --abort # Cancel the cherry-pick
git cherry-pick --skip # Skip the current commit
Common use cases:
git rebase
git rebase
allows you to move or combine commits to a new base commitInteractive rebase:
# Interactive rebase of last 3 commits
git rebase -i HEAD~3
# Interactive rebase to specific commit
git rebase -i abc1234
Rebase onto another branch:
During interactive rebase you can:
pick
: use the commit as-isreword
: change the commit messageedit
: modify the commit contentssquash
: combine with previous commitdrop
: remove the commit entirelyreorder
: change commit ordergh
) is the official command-line tool for GitHubWSL/Ubuntu:
# Install
(type -p wget >/dev/null || (sudo apt update && sudo apt install wget -y)) \
&& sudo mkdir -p -m 755 /etc/apt/keyrings \
&& out=$(mktemp) && wget -nv -O$out https://cli.github.com/packages/githubcli-archive-keyring.gpg \
&& cat $out | sudo tee /etc/apt/keyrings/githubcli-archive-keyring.gpg > /dev/null \
&& sudo chmod go+r /etc/apt/keyrings/githubcli-archive-keyring.gpg \
&& sudo mkdir -p -m 755 /etc/apt/sources.list.d \
&& echo "deb [arch=$(dpkg --print-architecture) signed-by=/etc/apt/keyrings/githubcli-archive-keyring.gpg] https://cli.github.com/packages stable main" | sudo tee /etc/apt/sources.list.d/github-cli.list > /dev/null \
&& sudo apt update \
&& sudo apt install gh -y
# Update (if already installed)
sudo apt update && sudo apt install gh -y
macOS (using Homebrew):
Repository operations:
Pull requests and issues:
# Create a new pull request
gh pr create --title "My PR" --body "Description of my PR"
# List pull requests
gh pr list
# View pull request details
gh pr view 123 --web
# Create a new issue
gh issue create --title "My Issue" --body "Description of my issue"
# List issues
gh issue list
# View issue details
gh issue view 456 --web
Please complete the following tasks:
git-practice
and initialise it as a Git repositoryREADME.md
with the content “# Git Practice Repository”src
and inside it create an empty file named main.py
hotfix
src
directory, create three files using brace expansion: utils.js
, utils.css
, utils.html
temp
and inside it create a file named debug.log
.gitignore
file and add temp/
to itsrc/main.py
to src/app.py
docs
and copy README.md
into it as guide.md
temp
directory and its contentsmain
branch and merge the hotfix
branchAfter completing these tasks, verify your work by checking the commit history and file structure.
Here are the answers to the practice quiz. Try to complete the quiz first before checking these answers!
mkdir git-practice && cd git-practice && git init
echo "# Git Practice Repository" > README.md
mkdir src && touch src/main.py
git add . && git commit -m "Initial commit with README and main.py"
git checkout -b hotfix
touch src/utils.{js,css,html}
mkdir temp && touch temp/debug.log
echo "temp/" > .gitignore
git add . && git commit -m "Add hotfix files and gitignore"
mv src/main.py src/app.py
mkdir docs && cp README.md docs/guide.md
rm -rf temp
git add . && git commit -m "Complete hotfix development"
git checkout main && git merge hotfix
git log --oneline
Additional verification commands:
git log --oneline
ls -la
git branch
cat .gitignore