From 7bd74eb64567ce96a19e4b780ef34b69db3e3e62 Mon Sep 17 00:00:00 2001 From: Ngu Cheng Jie <95898617+BensonNgu@users.noreply.github.com> Date: Fri, 8 Aug 2025 13:02:30 +0800 Subject: [PATCH] docs(git-guide): expand and clarify all command reference pages MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - π Clone an Existing Repository: added multiple cloning methods (SSH, shallow, branch-only, mirror, submodules, custom dir) - π§Ύ Initialize a Git Repository: added use cases, --initial-branch, --bare, and cautions - π View Current Git Settings: explained scopes (local/global/system) and precedence - π Other Useful Git Settings: added context, credential cache timeout, merge tool note - π§½ Remove or Edit a Git Setting: explained scopes, showed unset local example, file paths - π Basic Git Workflow: added git diff, git commit -am, quick summary - π Remote Repository Setup: added SSH alternative, verification, -u explanation - πΏ Branching: added git branch listing, git switch alternative, branch naming tips - π Merging: explained merge types, fast-forward, conflict resolution - π₯ Pull Strategies: grouped safe vs risky methods, added intro - π View Commit History: added filters, graph view, diff output - π§Ή Undoing Changes: added git restore, unstage all, stash drop - π§ Git Aliases: added descriptions, extra aliases, listing command --- .github/workflows/docs.yml | 22 +++++ docs/branching/branching.md | 37 +++++++++ docs/extras/aliases.md | 36 ++++++++ docs/history/log.md | 56 +++++++++++++ docs/index.md | 45 ++++++++++ docs/merging/merging.md | 63 ++++++++++++++ docs/remote/clone.md | 70 ++++++++++++++++ docs/remote/pull-strategies.md | 91 ++++++++++++++++++++ docs/remote/setup.md | 21 +++++ docs/setup/config.md | 8 ++ docs/setup/edit-settings.md | 20 +++++ docs/setup/init.md | 48 +++++++++++ docs/setup/other-settings.md | 36 ++++++++ docs/setup/view-settings.md | 27 ++++++ docs/undo/undo.md | 58 +++++++++++++ docs/workflow/basic.md | 148 +++++++++++++++++++++++++++++++++ mkdocs.yml | 51 ++++++++++++ requirements.txt | 2 + 18 files changed, 839 insertions(+) create mode 100644 .github/workflows/docs.yml create mode 100644 docs/branching/branching.md create mode 100644 docs/extras/aliases.md create mode 100644 docs/history/log.md create mode 100644 docs/index.md create mode 100644 docs/merging/merging.md create mode 100644 docs/remote/clone.md create mode 100644 docs/remote/pull-strategies.md create mode 100644 docs/remote/setup.md create mode 100644 docs/setup/config.md create mode 100644 docs/setup/edit-settings.md create mode 100644 docs/setup/init.md create mode 100644 docs/setup/other-settings.md create mode 100644 docs/setup/view-settings.md create mode 100644 docs/undo/undo.md create mode 100644 docs/workflow/basic.md create mode 100644 mkdocs.yml create mode 100644 requirements.txt diff --git a/.github/workflows/docs.yml b/.github/workflows/docs.yml new file mode 100644 index 0000000..0295b02 --- /dev/null +++ b/.github/workflows/docs.yml @@ -0,0 +1,22 @@ +name: Deploy docs + +on: +push: +branches: [ main ] +workflow_dispatch: + +jobs: +build-deploy: +runs-on: ubuntu-latest +steps: +- uses: actions/checkout@v4 +- uses: actions/setup-python@v5 +with: +python-version: "3.x" +- run: pip install -r requirements.txt +- run: mkdocs build --strict +- name: Deploy to GitHub Pages +uses: peaceiris/actions-gh-pages@v3 +with: +github_token: ${{ secrets.GITHUB_TOKEN }} +publish_dir: ./site \ No newline at end of file diff --git a/docs/branching/branching.md b/docs/branching/branching.md new file mode 100644 index 0000000..153c662 --- /dev/null +++ b/docs/branching/branching.md @@ -0,0 +1,37 @@ +# πΏ Branching +Branches let you work on features, fixes, or experiments **without affecting** the `main` branch. + +--- + +## π List Branches + +```bash +git branch +``` + +* `*` marks the branch youβre currently on. +* Add `-a` to see remote branches too: + +```bash +git branch -a +``` + +--- + +## π± Create a New Branch + +```bash +git checkout -b feature-name # Old way +git switch -c feature-name # Newer, simpler way +``` + +**Tip:** Start branch names with a short prefix, like `feature/`, `bugfix/`, or `hotfix/`. + +--- + +## π Switch to an Existing Branch + +```bash +git checkout branch-name # Old way +git switch branch-name # New way +``` \ No newline at end of file diff --git a/docs/extras/aliases.md b/docs/extras/aliases.md new file mode 100644 index 0000000..1b1a08e --- /dev/null +++ b/docs/extras/aliases.md @@ -0,0 +1,36 @@ +# π§ Useful Git Aliases + +Aliases let you create short commands for commonly used Git operations. + +--- + +## Common Aliases + +```bash +git config --global alias.st status # git st β git status +git config --global alias.co checkout # git co β git checkout +git config --global alias.br branch # git br β git branch +git config --global alias.ci commit # git ci β git commit +git config --global alias.lg "log --oneline --graph --all" +# git lg β compact visual commit history +``` + +--- + +## More Handy Aliases + +```bash +git config --global alias.unstage "reset HEAD --" +# git unstage file.txt β remove file from staging area + +git config --global alias.last "log -1 HEAD" +# git last β view details of the last commit +``` + +--- + +## View All Aliases + +```bash +git config --get-regexp alias +``` \ No newline at end of file diff --git a/docs/history/log.md b/docs/history/log.md new file mode 100644 index 0000000..7adb2d0 --- /dev/null +++ b/docs/history/log.md @@ -0,0 +1,56 @@ +# π View Commit History + +## Basic Log + +```bash +git log +``` + +Shows detailed commit history (hash, author, date, message). + +--- + +## One-Line Summary + +```bash +git log --oneline +``` + +Short commit hash + message (good for quick scanning). + +--- + +## With Graph View + +```bash +git log --oneline --graph --all +``` + +Shows branch structure and merge history in a compact view. + +--- + +## Filter by Author + +```bash +git log --author="Your Name" +``` + +--- + +## Filter by Date + +```bash +git log --after="2024-01-01" +git log --before="2024-06-30" +``` + +--- + +## View Changes in Each Commit + +```bash +git log -p +``` + +Adds the diff for each commit. \ No newline at end of file diff --git a/docs/index.md b/docs/index.md new file mode 100644 index 0000000..f328695 --- /dev/null +++ b/docs/index.md @@ -0,0 +1,45 @@ +
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+A concise guide demonstrating the fundamental Git commands and typical workflow for managing repositories on a local machine.
+
+## π§ Quick Links
+
+- π§Ύ [Initialize & Configure](setup/init.md)
+- βοΈ [Configure Git (First Time Only)](setup/config.md)
+- π [View Current Git Settings](setup/view-settings.md)
+- π [Other Useful Git Settings](setup/other-settings.md)
+- π§½ [Remove or Edit a Git Setting](setup/edit-settings.md)
+- π [Basic Git Workflow](workflow/basic.md)
+- π [Remote Repository Setup](remote/setup.md)
+- πΏ [Branching](branching/branching.md)
+- π [Merging](merging/merging.md)
+- π₯ [Pull Strategies](remote/pull-strategies.md)
+- π [Clone an Existing Repository](remote/clone.md)
+- π [View Commit History](history/log.md)
+- π§Ή [Undoing Changes](undo/undo.md)
+- π§ [Useful Git Aliases](extras/aliases.md)
+
+!!! tip "Copy buttons"
+ Every code block has a copy button on the rightβuse it on mobile too.
\ No newline at end of file
diff --git a/docs/merging/merging.md b/docs/merging/merging.md
new file mode 100644
index 0000000..feddea4
--- /dev/null
+++ b/docs/merging/merging.md
@@ -0,0 +1,63 @@
+# π Merging
+
+Merging combines changes from one branch into the branch youβre currently on.
+
+---
+
+## Basic Merge
+
+```bash
+git merge feature-name
+```
+
+This merges the branch `feature-name` into your current branch (often `main` or `develop`).
+
+---
+
+## Fast-Forward Merge *(no new commit created)*
+
+If no other changes have been made in the target branch since branching:
+
+```bash
+git merge --ff-only feature-name
+```
+
+β
Keeps history clean without extra merge commits.
+
+---
+
+## When Conflicts Appear
+
+If you see a merge conflict:
+
+1. Open the affected files in your editor.
+2. Look for conflict markers:
+
+ ```
+ <<<<<<< HEAD
+ Your changes
+ =======
+ Their changes
+ >>>>>>> feature-name
+ ```
+3. Edit to keep the correct content.
+4. Stage the resolved files:
+
+ ```bash
+ git add .
+ ```
+5. Complete the merge:
+
+ ```bash
+ git commit
+ ```
+
+---
+
+## Check Merge Status
+
+```bash
+git status
+```
+
+Shows if the merge is in progress or complete.
diff --git a/docs/remote/clone.md b/docs/remote/clone.md
new file mode 100644
index 0000000..bc787e1
--- /dev/null
+++ b/docs/remote/clone.md
@@ -0,0 +1,70 @@
+# π Clone an Existing Repository
+
+## 1. Standard HTTPS Clone *(Most Common)*
+
+```bash
+git clone https://github.com/username/repo-name.git
+```
+
+Copies the entire repository (all branches, full history) into a new local folder named after the repo.
+
+---
+
+## 2. Clone via SSH
+
+```bash
+git clone git@github.com:username/repo-name.git
+```
+
+**When to use:** If youβve set up an SSH key with GitHub (or another provider) and want to avoid typing your username/password every time.
+
+---
+
+## 3. Clone a Specific Branch Only
+
+```bash
+git clone --branch branch-name --single-branch https://github.com/username/repo-name.git
+```
+
+**When to use:** If you only need one branch and want to save bandwidth/time.
+
+---
+
+## 4. Shallow Clone (Latest Commit Only)
+
+```bash
+git clone --depth 1 https://github.com/username/repo-name.git
+```
+
+**When to use:** If you only need the latest snapshot and donβt care about full commit history.
+
+---
+
+## 5. Clone into a Custom Directory
+
+```bash
+git clone https://github.com/username/repo-name.git my-folder
+```
+
+**When to use:** If you want the local directory name to be something other than the repo name.
+
+---
+
+## 6. Clone with Submodules
+
+```bash
+git clone --recurse-submodules https://github.com/username/repo-name.git
+```
+
+**When to use:** If the repo contains submodules and you want them initialized and checked out automatically.
+
+---
+
+## 7. Mirror Clone (Full Backup/Migration)
+
+```bash
+git clone --mirror https://github.com/username/repo-name.git
+```
+
+**When to use:** For backups or repository migration.
+Includes all refs (branches, tags, remote-tracking branches).
diff --git a/docs/remote/pull-strategies.md b/docs/remote/pull-strategies.md
new file mode 100644
index 0000000..b27e20c
--- /dev/null
+++ b/docs/remote/pull-strategies.md
@@ -0,0 +1,91 @@
+# π₯ Different Ways of Pulling Updates from Remote
+
+Keeping your local branch in sync with the remote ensures youβre working with the latest code.
+Below are common ways to do it β starting from safest to most destructive.
+
+---
+
+## β
Safe / Common Methods
+
+### 1. `git pull`
+
+```bash
+git pull origin main
+```
+
+* **What it does:** `fetch` + `merge`
+* **Use case:** Automatically fetch and merge the latest changes.
+* **Pros:** Simple and convenient.
+* **Cons:** Can create messy merge commits.
+
+---
+
+### 2. `git fetch` + `git merge`
+
+```bash
+git fetch origin main
+git merge origin/main
+```
+
+* **What it does:** Downloads changes without applying, then merges.
+* **Use case:** Review before merging.
+* **Pros:** Safer than direct pull.
+* **Cons:** Two-step process.
+
+---
+
+### 3. `git fetch` + `git rebase`
+
+```bash
+git fetch origin main
+git rebase origin/main
+```
+
+* **What it does:** Reapplies your commits on top of fetched changes.
+* **Use case:** Keep a clean linear history.
+* **Pros:** Preferred in many teams.
+* **Cons:** Risky if rebasing public branches.
+
+---
+
+### 3b. `git pull --rebase`
+
+```bash
+git pull --rebase origin main
+```
+
+* Shortcut for:
+
+```bash
+git fetch origin main
+git rebase origin/main
+```
+
+---
+
+## π¦ Starting Fresh
+
+### 4. `git clone`
+
+```bash
+git clone https://github.com/username/repo-name.git
+```
+
+* **What it does:** Creates a full local copy of the repo.
+* **Use case:** First-time setup.
+* **Cons:** Not for updating existing repos.
+
+---
+
+## β οΈ Risky / Destructive
+
+### 5. `git reset --hard origin/main`
+
+```bash
+git fetch origin main
+git reset --hard origin/main
+```
+
+* **What it does:** Forces local branch to match remote exactly.
+* **Use case:** Discard all local changes and start fresh.
+* **Warning:** This deletes uncommitted changes.
diff --git a/docs/remote/setup.md b/docs/remote/setup.md
new file mode 100644
index 0000000..06905f0
--- /dev/null
+++ b/docs/remote/setup.md
@@ -0,0 +1,21 @@
+# π Remote Repository Setup
+
+1. Create a new repo on GitHub.
+ - Leave it empty if you already have files locally (no README/license).
+2. Link local repo to remote:
+ ```bash
+ git remote add origin https://github.com/your-username/repo-name.git
+ ```
+ π‘ Alternative (SSH):
+ ```bas
+ git remote add origin git@github.com:your-username/repo-name.git
+ ```
+3. Verify the remote link:
+ ```bash
+ git remote -v
+ ```
+4. Push for the first time:
+ ```bash
+ git push -u origin main
+ ```
+ π The `-u` flag sets `origin main` as the default for future `git push/git pull`.
diff --git a/docs/setup/config.md b/docs/setup/config.md
new file mode 100644
index 0000000..de0a4b8
--- /dev/null
+++ b/docs/setup/config.md
@@ -0,0 +1,8 @@
+# βοΈ Configure Git (First Time Only)
+## β
Set Name & Email
+These are **essential** for commit history:
+
+```bash
+git config --global user.name "Your Name"
+git config --global user.email "you@example.com"
+```
\ No newline at end of file
diff --git a/docs/setup/edit-settings.md b/docs/setup/edit-settings.md
new file mode 100644
index 0000000..a751cf3
--- /dev/null
+++ b/docs/setup/edit-settings.md
@@ -0,0 +1,20 @@
+# π§½ Remove or Edit a Git Setting
+## To unset a setting
+Removes a specific config value from the chosen scope.
+```bash
+git config --global --unset user.name # Remove from global config
+git config --local --unset user.name # Remove from local repo config
+```
+**Tip:**
+- `--global` β affects all repos for your user account.
+- `--local` β affects only the current repository.
+
+## To edit the Git config file manually
+Opens the config file in your default editor so you can directly edit values.
+```bash
+git config --global --edit # Edit the global config (~/.gitconfig)
+git config --local --edit # Edit the repo-specific config (.git/config)
+```
+:bangbang:Pro Tip:
+Use this if you want to make multiple changes at once instead of running multiple git config commands.
+
diff --git a/docs/setup/init.md b/docs/setup/init.md
new file mode 100644
index 0000000..ac19768
--- /dev/null
+++ b/docs/setup/init.md
@@ -0,0 +1,48 @@
+# π§Ύ Initialize a Git Repository
+
+```bash
+git init
+```
+Creates a new local Git repository in the current folder.
+This adds a hidden .git/ directory where Git stores all version history, configuration, and tracking info.
+
+## Initialize in a New Folder
+```bash
+mkdir my-project && cd my-project
+git init
+```
+*When to use:*
+ Starting a project from scratch.
+
+## Initialize with a Specific Branch Name
+```bash
+git init --initial-branch=main
+```
+*When to use:*
+ If you want main instead of master as the default branch name (or any custom name).
+
+## Initialize a Bare Repository
+```bash
+git init --bare
+```
+*When to use:*
+ For a central/shared repo that wonβt have a working directory β often used on servers for collaboration.
+
+## Initialize in an Existing Project Folder
+```bash
+cd existing-project
+git init
+```
+*When to use:*
+ If you already have files and want to start tracking them in Git.
+After initializing:
+
+```bash
+git add .
+git commit -m "Initial commit"
+```
+:bangbang:**Pro Tip**
+You can run `git status` right after `git init` to confirm your repo is ready.
+
+:bangbang: :warning:**Important**
+Running `git init` inside an existing Git repo will reinitialize it, which may cause confusion or overwrite config. Use with caution.
\ No newline at end of file
diff --git a/docs/setup/other-settings.md b/docs/setup/other-settings.md
new file mode 100644
index 0000000..58b51ce
--- /dev/null
+++ b/docs/setup/other-settings.md
@@ -0,0 +1,36 @@
+# π Other Useful Git Settings
+## Default Text Editor (optional)
+Sets which editor opens when Git needs you to type a message (e.g., for merge commits).
+
+```bash
+git config --global core.editor "code --wait" # VS Code
+git config --global core.editor "nano" # Nano
+git config --global core.editor "vim" # Vim
+```
+Tip: `--wait` tells VS Code to pause Git until you close the editor.
+
+## Default Branch Name (if you prefer main)
+```bash
+git config --global init.defaultBranch main
+```
+Ensures new repos use `main` instead of `master` as the initial branch.
+
+## Enable Colored Output
+```bash
+git config --global color.ui auto
+```
+Makes Gitβs output easier to read by coloring status, diffs, and logs.
+
+## Credential Caching (saves login for a while)
+```bash
+git config --global credential.helper 'cache --timeout=3600'
+```
+- `cache`: Store credentials in memory temporarily.
+- `--timeout=3600`: Keep them for 1 hour (3600 seconds).
+
+## Set Merge Tool (e.g., VS Code)
+```bash
+git config --global merge.tool code
+```
+Tells Git which tool to launch for resolving merge conflicts.
+**Note**: The tool must be installed and available in your systemβs PATH.
\ No newline at end of file
diff --git a/docs/setup/view-settings.md b/docs/setup/view-settings.md
new file mode 100644
index 0000000..819926a
--- /dev/null
+++ b/docs/setup/view-settings.md
@@ -0,0 +1,27 @@
+# π View Current Git Settings
+## View All Git Config Settings (Global + Local + System)
+```bash
+git config --list
+```
+
+Shows every config value Git is using for the current repo, combining:
+- System: applies to all users on the machine.
+- Global: applies to the current user account.
+- Local: applies only to the current repository.
+
+## View Global Settings Only
+```bash
+git config --global --list
+```
+*When to use:*
+To check settings that affect all repositories for your user account (e.g., your name & email).
+
+## View Local Settings (specific to the repo)
+```bash
+git config --local --list
+```
+*When to use:*
+To check settings that override global ones in this specific repository (e.g., different email for work projects).
+
+:bangbang: **Tip**
+If a setting appears in multiple scopes, Local > Global > System β meaning local settings win.
\ No newline at end of file
diff --git a/docs/undo/undo.md b/docs/undo/undo.md
new file mode 100644
index 0000000..01baf67
--- /dev/null
+++ b/docs/undo/undo.md
@@ -0,0 +1,58 @@
+# π§Ή Undoing Changes
+
+## Unstage a File
+
+Remove a file from the staging area but keep its changes.
+
+```bash
+git reset filename
+```
+
+Unstage **all** staged files:
+
+```bash
+git reset
+```
+
+---
+
+## Discard Unstaged Changes
+
+**Old syntax**:
+
+```bash
+git checkout -- filename
+```
+
+**Newer syntax** *(recommended)*:
+
+```bash
+git restore filename
+```
+
+β οΈ This will permanently discard your changes for that file.
+
+---
+
+## Amend Last Commit
+
+Change the last commitβs message or add new staged changes to it.
+
+```bash
+git commit --amend
+```
+
+**Tip:** Only amend if you havenβt pushed the commit yet.
+
+---
+
+## Temporarily Saving Work (Stash)
+
+Save uncommitted changes for later without committing.
+
+```bash
+git stash # Save local modifications
+git stash apply # Reapply most recent stash
+git stash list # View all stashes
+git stash drop # Delete most recent stash
+```
\ No newline at end of file
diff --git a/docs/workflow/basic.md b/docs/workflow/basic.md
new file mode 100644
index 0000000..95821f8
--- /dev/null
+++ b/docs/workflow/basic.md
@@ -0,0 +1,148 @@
+# π Basic Git Workflow
+## π Check Git Status
+```bash
+git status
+```
+See changes (staged/unstaged/untracked files).
+
+What are staged, unstaged, and untracked files
+
+---
+1. π‘ **Untracked Files**
+
+> Files that are **not yet being tracked by Git**.
+
+* These are new files in your working directory.
+* Git doesnβt know about them until you explicitly add them.
+
+π¦ **Example:**
+
+You create a new file:
+
+```bash
+touch newfile.txt
+```
+
+Then run:
+
+```bash
+git status
+```
+
+Git says:
+
+```bash
+Untracked files:
+ (use "git add