light/dark

Git 101
2/22/2025·5 min read·20 views

Introduction to Git

What is Git?

  • A checkpoint system that tracks changes in files.
  • A Version Control System (VCS) created by Linus Torvalds.
  • Runs locally on a machine.

What is GitHub?

  • A cloud-based service for hosting Git repositories.
  • Facilitates collaboration and version control.

Command-Line Interfaces

  • Bash: Unix-based command-line interface (default shell for Linux and macOS).
  • Git Bash: Emulates Bash on Windows for a Unix-like experience.
  • Command Prompt: Default shell for Windows.

Configuring Git

git config --global user.name "Son Dao"
git config --global user.email "sondao@gmail.com"
  • Use --global to apply settings system-wide.
  • Use --local for per-repository configuration.

Basic Unix Commands

ls # List files
ls -a # Show hidden files
pwd # Print working directory
cd <dir> # Change directory
cd .. # Move up one directory
mkdir <dir> # Create directory
rm <file> # Remove file
rm -rf <dir> # Remove directory recursively

Git Workflow

  • Working Directorygit addStaging Areagit commitRepository
  • Always run git status to check the state of your repo.
  • Avoid nesting repositories inside each other.

Adding & Committing Changes

git init # Initialize a new repo
git status # Check status
git log # View commit history
git add <file> # Stage a file
git add . # Stage all changes
git commit -m "Commit message" # Commit changes

Atomic Commits

  • Each commit should represent a single logical change.
  • Use imperative mood for commit messages: "Fix bug", not "Fixed bug".

Amending Commits

git commit --amend # Modify last commit message or add files

Working with Branches

git branch # List branches
git branch <name> # Create a new branch
git switch <name> # Switch branches
git switch -c <name> # Create & switch branch
git 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 commit
git checkout <branch> # Switch branches
git checkout -- <file> # Discard changes to file

Restore (Newer Alternative)

git restore <file> # Restore to last committed state
git restore --staged <file> # Unstage file

Reset (Moving HEAD)

git reset <commit> # Reset to commit, keep changes
git 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 repo
git remote -v # List remotes
git remote add <name> <url> # Add a new remote
git remote rename <old> <new> # Rename remote
git push <remote> <branch> # Push changes
git 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

  • Everyone works directly on main.

Feature Branch Workflow

  • Developers work on separate branches, then merge into main.

Fork & Clone Workflow

  • Each developer maintains their own repository.

Merging & Rebasing

Merge (Default Method)

git merge <branch>

Rebase (Alternative to Merge)

git rebase <branch>
  • Moves branch commits to start at the latest main.
  • Do not rebase commits that have been pushed to a shared repository.

Interactive Rebase

git rebase -i HEAD~4 # Modify last 4 commits

Stashing (Saving Temporary Work)

git stash # Save uncommitted changes temporarily
git stash list # Show list of stashed changes
git stash apply # Apply the most recent stash
git stash pop # Apply and remove the most recent stash
git stash drop # Delete the most recent stash
git stash clear # Remove all stashes

Cherry-Picking (Picking Specific Commits)

git cherry-pick <commit-hash>

Diff (Comparing Changes)

git diff # Show unstaged changes
git diff --staged # Show staged changes
git 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 delete
git clean -f # Force delete untracked files
git clean -fd # Remove both untracked files and directories

Reflog (History of HEAD Movements)

git reflog # View history of HEAD changes
git 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 tags
git tag -l "*beta*" # Filter tags