Git 命令大全
第一章:Git 簡介
Git 是一個開源的分布式版本控制系統,由 Linus Torvalds 于 2005 年創建,用于有效、高速地處理從小到大的項目。它是一個命令行工具,用于跟蹤和管理源代碼歷史記錄。
第二章:Git 的 100 種命令
以下是 Git 的 100 種常用命令,每種命令都附有基本用法、使用場景和示例。
基礎命令
-
git init
- 初始化一個新的 Git 倉庫。- 場景:開始一個新項目。
- 示例:
git init my-project
-
git clone [url]
- 克隆遠程倉庫到本地。- 場景:獲取遠程倉庫的副本。
- 示例:
git clone https://github.com/user/repo.git
-
git add [file]
- 將文件添加到暫存區。- 場景:準備提交。
- 示例:
git add README.md
-
git commit -m "[message]"
- 提交更改到倉庫。- 場景:保存更改。
- 示例:
git commit -m "Add README"
-
git status
- 顯示倉庫的狀態。- 場景:查看當前狀態。
- 示例:
git status
-
git log
- 顯示提交歷史。- 場景:查看歷史記錄。
- 示例:
git log
-
git diff
- 顯示更改的文件差異。- 場景:查看更改。
- 示例:
git diff
-
git branch
- 列出本地分支。- 場景:查看分支。
- 示例:
git branch
-
git checkout [branch]
- 切換到指定分支。- 場景:切換分支。
- 示例:
git checkout feature
-
git merge [branch]
- 合并指定分支到當前分支。- 場景:合并分支。
- 示例:
git merge feature
分支管理
-
git branch -a
- 列出所有分支(本地和遠程)。- 示例:
git branch -a
- 示例:
-
git branch -r
- 列出遠程分支。- 示例:
git branch -r
- 示例:
-
git branch -d [branch]
- 刪除本地分支。- 示例:
git branch -d feature
- 示例:
-
git branch -D [branch]
- 強制刪除本地分支。- 示例:
git branch -D feature
- 示例:
-
git checkout -b [branch]
- 創建并切換到新分支。- 示例:
git checkout -b feature
- 示例:
-
git push origin --delete [branch]
- 刪除遠程分支。- 示例:
git push origin --delete feature
- 示例:
-
git fetch
- 從遠程倉庫獲取所有分支。- 示例:
git fetch
- 示例:
-
git pull
- 從遠程倉庫獲取并合并到當前分支。- 示例:
git pull origin master
- 示例:
-
git push [remote] [branch]
- 推送本地分支到遠程倉庫。- 示例:
git push origin feature
- 示例:
-
git remote -v
- 顯示遠程倉庫信息。- 示例:
git remote -v
- 示例:
標簽管理
-
git tag
- 列出所有標簽。- 示例:
git tag
- 示例:
-
git tag [tagname]
- 創建標簽。- 示例:
git tag v1.0.0
- 示例:
-
git tag -d [tagname]
- 刪除標簽。- 示例:
git tag -d v1.0.0
- 示例:
-
git push origin --tags
- 推送所有標簽到遠程倉庫。- 示例:
git push origin --tags
- 示例:
-
git checkout [tagname]
- 切換到標簽的版本。- 示例:
git checkout v1.0.0
- 示例:
遠程倉庫管理
-
git remote add [name] [url]
- 添加遠程倉庫。- 示例:
git remote add origin https://github.com/user/repo.git
- 示例:
-
git remote rename [old] [new]
- 重命名遠程倉庫。- 示例:
git remote rename origin upstream
- 示例:
-
git remote remove [name]
- 刪除遠程倉庫。- 示例:
git remote remove upstream
- 示例:
-
git remote set-url [name] [url]
- 設置遠程倉庫的 URL。- 示例:
git remote set-url origin https://github.com/newuser/repo.git
- 示例:
-
git ls-remote [url]
- 顯示遠程倉庫的引用和哈希值。- 示例:
git ls-remote https://github.com/user/repo.git
- 示例:
撤銷和重置
-
git reset [file]
- 重置暫存區的文件。- 示例:
git reset README.md
- 示例:
-
git reset --hard
- 重置工作目錄和暫存區到最后一次提交。- 示例:
git reset --hard
- 示例:
-
git revert [commit]
- 撤銷指定的提交。- 示例:
git revert 1234567
- 示例:
-
git clean -f
- 清除未跟蹤的文件。- 示例:
git clean -f
- 示例:
-
git clean -fd
- 清除未跟蹤的文件和目錄。- 示例:
git clean -fd
- 示例:
-
git stash
- 暫存工作目錄的更改。- 示例:
git stash
- 示例:
-
git stash pop
- 恢復之前暫存的更改。- 示例:
git stash pop
- 示例:
-
git stash list
- 列出所有暫存的更改。- 示例:
git stash list
- 示例:
-
git stash drop [stash]
- 刪除指定的暫存更改。- 示例:
git stash drop stash@{0}
- 示例:
-
git reflog
- 顯示所有參考的日志。- 示例:
git reflog
- 示例:
子模塊管理
-
git submodule add [url]
- 添加子模塊。- 示例:
git submodule add https://github.com/user/submodule.git
- 示例:
-
git submodule update --init --recursive
- 初始化和更新所有子模塊。- 示例:
git submodule update --init --recursive
- 示例:
-
git submodule status
- 顯示子模塊的狀態。- 示例:
git submodule status
- 示例:
-
git submodule deinit [path]
- 取消初始化子模塊。- 示例:
git submodule deinit path/to/submodule
- 示例:
-
git submodule foreach [command]
- 對所有子模塊執行命令。- 示例:
git submodule foreach git pull origin master
- 示例:
忽略文件
-
git ignore
- 列出全局忽略的文件。- 示例:
git ignore
- 示例:
-
git config --global core.excludesfile ~/.gitignore_global
- 設置全局忽略文件。- 示例:
git config --global core.excludesfile ~/.gitignore_global
- 示例:
-
git add .gitignore
- 將忽略文件添加到倉庫。- 示例:
git add .gitignore
- 示例:
-
git check-ignore [file]
- 檢查文件是否被忽略。- 示例:
git check-ignore build/
- 示例:
-
git ls-files --others --ignored --exclude-standard
- 列出所有被忽略的文件。- 示例:
git ls-files --others --ignored --exclude-standard
- 示例:
交互式命令
-
git add -i
- 進入交互式添加模式。- 示例:
git add -i
- 示例:
-
git commit -a
- 自動暫存所有已跟蹤的更改并提交。- 示例:
git commit -a
- 示例:
-
git rebase -i [commit]
- 交互式變基。- 示例:
git rebase -i HEAD~3
- 示例:
-
git cherry-pick [commit]
- 選擇性地應用提交。- 示例:
git cherry-pick 1234567
- 示例:
-
git bisect
- 二分查找錯誤的提交。- 示例:
git bisect start
- 示例:
-
git blame [file]
- 顯示文件每一行的最后提交者。- 示例:
git blame README.md
- 示例:
-
git grep [pattern]
- 在工作目錄中搜索文本。- 示例:
git grep "Hello World"
- 示例:
-
git gui
- 打開 Git 的圖形用戶界面。- 示例:
git gui
- 示例:
-
gitk
- 打開 Git 的圖形歷史查看工具。- 示例:
gitk
- 示例:
-
git mergetool
- 使用圖形工具解決合并沖突。- 示例:
git mergetool
- 示例:
配置和幫助
-
git config --list
- 列出所有 Git 配置。- 示例:
git config --list
- 示例:
-
git config --global user.name "[name]"
- 設置全局用戶名。- 示例:
git config --global user.name "John Doe"
- 示例:
-
git config --global user.email "[email]"
- 設置全局用戶郵箱。- 示例:
git config --global user.email "john.doe@example.com"
- 示例:
-
git config --global color.ui auto
- 啟用顏色顯示。- 示例:
git config --global color.ui auto
- 示例:
-
git help [command]
- 顯示指定命令的幫助信息。- 示例:
git help commit
- 示例:
-
git --version
- 顯示 Git 的版本。- 示例:
git --version
- 示例:
-
git help config
- 顯示關于配置選項的幫助信息。- 示例:
git help config
- 示例:
-
git help每一天
- 顯示關于每一天命令的幫助信息。- 示例:
git help everyday
- 示例:
-
git help glossary
- 顯示 Git 術語表。- 示例:
git help glossary
- 示例:
-
git help tutorial
- 顯示 Git 教程。- 示例:
git help tutorial
- 示例:
工作流命令
-
git flow init
- 初始化一個 Git Flow 工作流。- 示例:
git flow init
- 示例:
-
git flow feature start [feature]
- 開始一個新的功能分支。- 示例:
git flow feature start login
- 示例:
-
git flow feature finish [feature]
- 完成一個功能分支。- 示例:
git flow feature finish login
- 示例:
-
git flow release start [version]
- 開始一個新的發布分支。- 示例:
git flow release start 1.0.0
- 示例:
-
git flow release finish [version]
- 完成一個發布分支。- 示例:
git flow release finish 1.0.0
- 示例:
-
git flow hotfix start [hotfix]
- 開始一個新的熱修復分支。- 示例:
git flow hotfix start 1.0.1
- 示例:
-
git flow hotfix finish [hotfix]
- 完成一個熱修復分支。- 示例:
git flow hotfix finish 1.0.1
- 示例:
-
git flow support start [support]
- 開始一個新的支持分支。- 示例:
git flow support start 1.0-stable
- 示例:
-
git flow support finish [support]
- 完成一個支持分支。- 示例:
git flow support finish 1.0-stable
- 示例:
-
git flow version
- 顯示當前 Git Flow 版本。- 示例:
git flow version
- 示例:
子命令
-
git clean
- 清除工作目錄中的未跟蹤文件。- 示例:
git clean
- 示例:
-
git describe
- 顯示最近的標簽和提交的哈希值。- 示例:
git describe
- 示例:
-
git fsck
- 檢查 Git 倉庫的完整性。- 示例:
git fsck
- 示例:
-
git gc
- 清理無用的文件和優化倉庫。- 示例:
git gc
- 示例:
-
git prune
- 移除引用的遠程跟蹤分支。- 示例:
git prune
- 示例:
-
git repack
- 重新打包倉庫中的包文件。- 示例:
git repack
- 示例:
-
git whatchanged
- 顯示倉庫的變更日志。- 示例:
git whatchanged
- 示例:
-
git worktree add
- 添加一個新的工作樹。- 示例:
git worktree add
- 示例:
-
git worktree list
- 列出所有工作樹。- 示例:
git worktree list
- 示例:
-
git worktree remove
- 刪除一個工作樹。- 示例:
git worktree remove
- 示例:
腳本和鉤子
-
git init-db
- 創建一個新的 Git 倉庫。- 示例:
git init-db
- 示例:
-
git get-tar-commit-id [file]
- 獲取 tar 文件的提交 ID。- 示例:
git get-tar-commit-id archive.tar
- 示例:
-
git instaweb
- 啟動一個簡單的 web 服務器。- 示例:
git instaweb
- 示例:
-
git archive
- 創建一個歸檔文件。- 示例:
git archive
- 示例:
-
git bisect run [command]
- 自動執行 bisect 測試。- 示例:
git bisect run make
- 示例:
-
git cat-file
- 顯示 Git 對象的信息。- 示例:
git cat-file
- 示例:
-
git check-attr
- 檢查文件屬性。- 示例:
git check-attr
- 示例:
-
git checkout-index
- 從索引中檢出文件。- 示例:
git checkout-index
- 示例:
-
git commit-tree
- 創建一個新的提交。- 示例:
git commit-tree
- 示例:
-
git diff-files
- 顯示工作目錄和索引的差異。
- 示例:`git diff-files`
請注意,這個列表并不完整,Git 有更多命令和選項,可以根據具體需求使用。這些命令覆蓋了 Git 的基本使用,分支管理,標簽管理,遠程倉庫管理,撤銷和重置,子模塊管理,忽略文件,交互式命令,配置和幫助,工作流命令,子命令,腳本和鉤子等多個方面。