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 what has changed, line by linemy-project and look at itgit 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 your changes staged--mixed (the default) keeps them, but unstaged--hard throws them away entirelygit 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 the previous commitdrop: remove the commit entirelygit stash?git stash puts your uncommitted changes aside, leaving a clean working directoryUse it when:
pull but have local changes in the waygit stash commandsSaving and restoring stashes:
# Stash current changes (tracked files only)
git stash
# Stash with a descriptive message
git stash push -m "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 stashA 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 GitHubmacOS (using Homebrew):
WSL/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 -yRepository 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 --webSet up the repository
git-practice directory and initialise itREADME.md containing # Git Practice Repositorysrc/main.py, emptyWork on a branch
hotfixsrc, use brace expansion to create utils.js, utils.css, and utils.html in one command.gitignore containing temp/, then commit: “Add hotfix files and gitignore”src/main.py to src/app.pyBring it back
hotfix, and show the history in compact formCheck your work with git log --oneline and git branch
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.py
git mv src/main.py src/app.py does the same and stages it in one stepgit add . && git commit -m "Complete hotfix development"git checkout main && git merge hotfix && git log --oneline
git init gave you a branch called master, use that name instead. Check with git branchAdditional verification commands:
git log --onelinels -lagit branchcat .gitignore