[Ctrl J]
[Ctrl K]
light/dark
Introduction to Git
What is Git?
What is GitHub?
Command-Line Interfaces
Configuring Git
git config --global user.name "Son Dao"git config --global user.email "sondao@gmail.com"
--global
to apply settings system-wide.--local
for per-repository configuration.Basic Unix Commands
ls # List filesls -a # Show hidden filespwd # Print working directorycd <dir> # Change directorycd .. # Move up one directorymkdir <dir> # Create directoryrm <file> # Remove filerm -rf <dir> # Remove directory recursively
Git Workflow
git add
→ Staging Area → git commit
→ Repositorygit status
to check the state of your repo.Adding & Committing Changes
git init # Initialize a new repogit status # Check statusgit log # View commit historygit add <file> # Stage a filegit add . # Stage all changesgit commit -m "Commit message" # Commit changes
Atomic Commits
Amending Commits
git commit --amend # Modify last commit message or add files
Working with Branches
git branch # List branchesgit branch <name> # Create a new branchgit switch <name> # Switch branchesgit switch -c <name> # Create & switch branchgit switch - # Switch to last branch
HEAD
points to the current commit/branch.git commit -a -m "msg"
combines add
and commit
(excludes untracked files).Undoing Changes & Time Travel
Checkout
git checkout <commit> # View previous commitgit checkout <branch> # Switch branchesgit checkout -- <file> # Discard changes to file
Restore (Newer Alternative)
git restore <file> # Restore to last committed stategit restore --staged <file> # Unstage file
Reset (Moving HEAD)
git reset <commit> # Reset to commit, keep changesgit reset --hard <commit> # Reset and discard changes
Revert (Safe Alternative)
git revert <commit> # Create a commit that undoes a previous commit
Working with Remote Repositories
git clone <url> # Clone repogit remote -v # List remotesgit remote add <name> <url> # Add a new remotegit remote rename <old> <new> # Rename remotegit push <remote> <branch> # Push changesgit push -u origin master # Set upstream branch
Fetching & Pulling Updates
git fetch <remote> # Get latest changes (no merge)git pull <remote> <branch> # Fetch and merge
Remote Tracking Branches
origin/master
tracks the remote master
branch.git switch <remote-branch>
creates a local branch from a remote branch.Git Workflows
Centralized Workflow
main
.Feature Branch Workflow
main
.Fork & Clone Workflow
Merging & Rebasing
Merge (Default Method)
git merge <branch>
Rebase (Alternative to Merge)
git rebase <branch>
main
.Interactive Rebase
git rebase -i HEAD~4 # Modify last 4 commits
Stashing (Saving Temporary Work)
git stash # Save uncommitted changes temporarilygit stash list # Show list of stashed changesgit stash apply # Apply the most recent stashgit stash pop # Apply and remove the most recent stashgit stash drop # Delete the most recent stashgit stash clear # Remove all stashes
Cherry-Picking (Picking Specific Commits)
git cherry-pick <commit-hash>
Diff (Comparing Changes)
git diff # Show unstaged changesgit diff --staged # Show staged changesgit diff <commit1> <commit2> # Compare two commits
Blame (Tracking Who Changed What)
git blame <file>
Clean (Removing Untracked Files)
git clean -n # Show untracked files to deletegit clean -f # Force delete untracked filesgit clean -fd # Remove both untracked files and directories
Reflog (History of HEAD Movements)
git reflog # View history of HEAD changesgit reset --hard HEAD@{n} # Restore repo to a previous state
Squashing Commits (Combining Multiple Commits)
git rebase -i HEAD~3 # Squash last 3 commits interactively
Git Tags
git tag # List tagsgit tag -l "*beta*" # Filter tags