提交、分支、合并、變基、遠端等基本 Git 命令。
git config --global user.name "Name"設置與配置Set global author name
例如 git config --global user.name "Jane Doe"
git config --global user.email "email"設置與配置Set global author email
例如 git config --global user.email "[email protected]"
git config --list設置與配置List all configuration settings
git config --global core.editor "code"設置與配置Set VS Code as default editor
git config --global alias.<alias> <cmd>設置與配置Create a command alias
例如 git config --global alias.st status
git init設置與配置Initialize a new local repository
git init <dir>設置與配置Initialize a repo in a specific directory
例如 git init my-project
git clone <url>設置與配置Clone a remote repository
例如 git clone https://github.com/user/repo.git
git clone <url> <dir>設置與配置Clone into a specific folder
例如 git clone https://github.com/user/repo.git my-dir
git status暫存與基礎Show working tree status
git status -s暫存與基礎Short/compact status output
git add <file>暫存與基礎Stage a specific file
例如 git add README.md
git add .暫存與基礎Stage all changes in current directory
git add -p暫存與基礎Interactively stage hunks of changes
git commit -m "msg"暫存與基礎Commit staged changes with a message
例如 git commit -m "feat: add login page"
git commit -am "msg"暫存與基礎Stage tracked files and commit in one step
git commit --amend破壞性暫存與基礎Modify the most recent commit (message or content)
git diff暫存與基礎Show unstaged changes
git diff --staged暫存與基礎Show staged changes (vs last commit)
git diff <branch1>..<branch2>暫存與基礎Compare two branches
例如 git diff main..feature
git rm <file>暫存與基礎Remove a file from working tree and index
例如 git rm old-file.txt
git rm --cached <file>暫存與基礎Untrack a file without deleting it
例如 git rm --cached secret.txt
git mv <old> <new>暫存與基礎Move or rename a tracked file
例如 git mv old.js new.js
git branch分支與合並List local branches
git branch -a分支與合並List all branches (local + remote)
git branch <name>分支與合並Create a new branch
例如 git branch feature/auth
git checkout <branch>分支與合並Switch to an existing branch
例如 git checkout main
git checkout -b <branch>分支與合並Create and switch to a new branch
例如 git checkout -b feature/auth
git switch <branch>分支與合並Switch branches (modern syntax)
例如 git switch main
git switch -c <branch>分支與合並Create and switch (modern syntax)
例如 git switch -c feature/auth
git branch -d <branch>分支與合並Delete a merged branch
例如 git branch -d feature/auth
git branch -D <branch>破壞性分支與合並Force delete a branch (unmerged too)
例如 git branch -D feature/old
git branch -m <old> <new>分支與合並Rename a branch
例如 git branch -m old-name new-name
git merge <branch>分支與合並Merge a branch into current branch
例如 git merge feature/auth
git merge --no-ff <branch>分支與合並Merge with a merge commit (no fast-forward)
git merge --squash <branch>分支與合並Squash all commits from branch into one staged change
git merge --abort分支與合並Abort an in-progress merge
git remote -v遠端操作List remote connections with URLs
git remote add <name> <url>遠端操作Add a new remote
例如 git remote add origin https://github.com/user/repo.git
git remote remove <name>遠端操作Remove a remote connection
例如 git remote remove origin
git remote rename <old> <new>遠端操作Rename a remote
例如 git remote rename origin upstream
git fetch遠端操作Download all changes from remote (no merge)
git fetch <remote>遠端操作Fetch from a specific remote
例如 git fetch origin
git pull遠端操作Fetch and merge changes from remote
git pull --rebase遠端操作Fetch and rebase onto remote branch
git push <remote> <branch>遠端操作Push branch to remote
例如 git push origin main
git push -u origin <branch>遠端操作Push and set upstream tracking
例如 git push -u origin feature/auth
git push --force-with-lease破壞性遠端操作Safe force push (fails if others pushed)
git push --tags遠端操作Push all tags to remote
git push origin --delete <branch>破壞性遠端操作Delete a remote branch
例如 git push origin --delete feature/old
git restore <file>破壞性撤銷更改Discard changes in working directory
例如 git restore index.html
git restore --staged <file>撤銷更改Unstage a file (keep changes)
例如 git restore --staged index.html
git reset HEAD <file>撤銷更改Unstage a file (older syntax)
例如 git reset HEAD index.html
git reset --soft HEAD~1撤銷更改Undo last commit, keep changes staged
git reset --mixed HEAD~1撤銷更改Undo last commit, unstage changes (default)
git reset --hard HEAD~1破壞性撤銷更改Undo last commit, discard all changes
git reset --hard <commit>破壞性撤銷更改Reset to a specific commit, discard everything after
git revert <commit>撤銷更改Create a new commit that undoes a specific commit
例如 git revert a1b2c3d
git revert HEAD撤銷更改Revert the most recent commit
git clean -fd破壞性撤銷更改Remove untracked files and directories
git clean -n撤銷更改Dry run: show what would be cleaned
git stash儲存Stash current working directory changes
git stash push -m "msg"儲存Stash with a descriptive message
例如 git stash push -m "WIP: auth feature"
git stash list儲存List all stashes
git stash pop儲存Apply most recent stash and remove it
git stash apply stash@{n}儲存Apply a specific stash without removing
例如 git stash apply stash@{0}
git stash drop stash@{n}儲存Delete a specific stash
例如 git stash drop stash@{0}
git stash clear破壞性儲存Remove all stashes
git stash branch <branch>儲存Create a branch from a stash
例如 git stash branch feature/from-stash
git rebase <branch>變基與進階Rebase current branch onto another
例如 git rebase main
git rebase -i HEAD~n變基與進階Interactive rebase for last n commits
例如 git rebase -i HEAD~3
git rebase --continue變基與進階Continue rebase after resolving conflicts
git rebase --abort變基與進階Abort an in-progress rebase
git cherry-pick <commit>變基與進階Apply a specific commit to current branch
例如 git cherry-pick a1b2c3d
git cherry-pick <c1>..<c2>變基與進階Apply a range of commits
例如 git cherry-pick a1b2c3d..f6e5d4c
git bisect start變基與進階Start binary search for a bug
git bisect good <commit>變基與進階Mark a commit as good in bisect
git bisect bad <commit>變基與進階Mark a commit as bad in bisect
git bisect reset變基與進階End bisect session
git worktree add <path> <branch>變基與進階Check out a branch into a separate directory
git log檢查與日誌Show commit history
git log --oneline檢查與日誌Compact one-line log
git log --oneline --graph --all檢查與日誌Visual branch graph in terminal
git log -n <count>檢查與日誌Show last N commits
例如 git log -n 5
git log --author="name"檢查與日誌Filter commits by author
例如 git log --author="Jane Doe"
git log --since="2 weeks ago"檢查與日誌Show commits since a date
git log -p <file>檢查與日誌Show changes to a specific file over time
例如 git log -p README.md
git log --follow <file>檢查與日誌Follow renames of a file in history
git show <commit>檢查與日誌Show details of a specific commit
例如 git show a1b2c3d
git blame <file>檢查與日誌Show who last modified each line
例如 git blame src/app.tsx
git shortlog -sn檢查與日誌Summarize commits by author
git reflog檢查與日誌Show history of HEAD movements (recovery tool)
git grep "pattern"檢查與日誌Search working directory for a pattern
例如 git grep "TODO"
git tag標籤List all tags
git tag <name>標籤Create a lightweight tag
例如 git tag v1.0.0
git tag -a <name> -m "msg"標籤Create an annotated tag
例如 git tag -a v1.0.0 -m "Release v1.0.0"
git tag -a <name> <commit>標籤Tag a past commit
例如 git tag -a v0.9.0 a1b2c3d
git show <tag>標籤Show tag metadata and commit
例如 git show v1.0.0
git push origin <tag>標籤Push a specific tag to remote
例如 git push origin v1.0.0
git push origin --tags標籤Push all tags to remote
git tag -d <tag>標籤Delete a local tag
例如 git tag -d v1.0.0
git push origin --delete <tag>標籤Delete a remote tag
例如 git push origin --delete v1.0.0
git merge通過建立合併提交來合併兩個分支,保留兩個分支的完整歷史。git rebase將提交從一個分支移動到另一個分支,重寫歷史以建立線性、更整潔的提交歷史。對公共分支和共用工作使用merge;對合併前清理本地功能分支使用rebase。
使用git reset --soft HEAD~1可以撤銷最後一次提交,同時保持更改處於暫存狀態。使用git reset HEAD~1(或--mixed)取消暫存更改但將其保留在工作目錄中。使用git reset --hard HEAD~1永久丟棄提交和所有更改(破壞性操作)。
git fetch從遠端下載更改,但不會將它們合併到當前分支。它會更新遠端追蹤分支(origin/main)。git pull是git fetch + git merge(或帶--rebase標誌的git rebase)。當您想在整合之前檢查更改時,使用fetch。
在衝突的合併/變基之後,Git用'<<<<<<'、=======、>>>>>>標記來標記衝突檔案。開啟每個衝突檔案,手動選擇保留哪些更改,刪除標記,然後用git add <檔案>暫存已解決的檔案,並用git merge --continue或git commit完成。