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 checkoutclone and fork repositoriesgit checkout and git resetmaster/mainSource: Lodato (2010)
Today we will cover:
git diffgit commit --amendgit cherry-pick 😅git rebase and its usesgit stashgit diff?git diff shows you what has changed between commits, branches, or your working directorymy-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?Amending the commit message:
Adding forgotten files:
Both together:
Important rules:
git revert insteadgit resetgit 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-pickBasic cherry-pick:
After cherry-picking:
Handling conflicts:
Common use cases:
git rebasegit rebase allows you to move or combine commits to a new base commitInteractive rebase:
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 ordergit stash?git stash temporarily saves your uncommitted changes without committing themWhen to use git stash:
git stash commandsSaving and restoring stashes:
# Stash current changes (tracked files only)
git stash
# Stash with a descriptive message
git stash save "WIP: adding user authentication"
# Stash including untracked files
git stash -u
# Stash including untracked and ignored files
git stash -a
# Restore most recent stash and remove from stack
git stash pop
# Restore most recent stash but keep in stack
git stash applyManaging multiple stashes:
# List all stashes
git stash list
# Apply a specific stash
git stash apply stash@{2}
# Drop a specific stash
git stash drop stash@{1}
# Clear all stashes (use with caution!)
git stash clear
# Show changes in most recent stash
git stash show
# Show changes in stash with diff
git stash show -p stash@{0}git stash 🔀A very common scenario:
You’ve been coding away, only to realise you’re on the wrong branch! 😱
git stash makes it easy to move your uncommitted changes to the correct branch:
This technique works for both tracked and untracked files (use git stash -u for untracked).
Creating a new branch from stash:
If the branch doesn’t exist yet, you can create it directly from the stash:
This command:
Pro tip: This is the safest way to recover stashed work if you’re unsure about conflicts!
gh) 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 -ymacOS (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 --webPlease 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.pyhotfixsrc directory, create three files using brace expansion: utils.js, utils.css, utils.html.gitignore file and add temp/ to it, then stage and commit with the message “Add hotfix files and gitignore”src/main.py to src/app.pymain branch and merge the hotfix branch, then view the commit history in a compact formatAfter 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 initecho "# Git Practice Repository" > README.mdmkdir src && touch src/main.pygit add . && git commit -m "Initial commit with README and main.py"git checkout -b hotfixtouch src/utils.{js,css,html}echo "temp/" > .gitignore && git add . && git commit -m "Add hotfix files and gitignore"mv src/main.py src/app.pygit add . && git commit -m "Complete hotfix development"git checkout main && git merge hotfix && git log --onelineAdditional verification commands:
git log --onelinels -lagit branchcat .gitignore