Windows XP Windows 7 Windows 2003 Windows Vista Windows 綜合教學 Linux 系統教學
Windows 10 Windows 8 Windows 2008 Windows NT Windows Server 電腦軟體教學
您现在的位置: Windows教程網 >> Windows教程 >  >> Linux 系統教學 >> Linux 教學

Git 常用指令速查表 (完整分類版)

編輯:Linux 教學

整理 Git 日常操作高頻指令,按功能分類,適合做為桌邊速查手冊。涵蓋設定、倉庫建立、修改提交、歷史查看、撤銷、分支標籤、合併衍合、遠端操作、封存打包、儲藏、檔案屬性與忽略規則。

⚙ 設定

git config --global user.name "Your Name"
git config --global user.email you@example.com

📁 建立/取得倉庫


git clone [url]                    # 複製遠端倉庫
git clone git://github.com/user/repo.git
git clone git://github.com/user/repo.git mydir
git clone --bare my_project my_project.git  # 建立裸倉庫(無工作區)
git init                           # 初始化本地倉庫
git init --bare                    # 建立裸倉庫

✏ 修改與提交


git status                         # 查看工作區狀態
git diff                           # 查看未暫存變更
git diff --cached                  # 查看已暫存變更
git diff master...feature          # 特性分支與 master 共同祖先的差異
git add .                          # 暫存所有變更
git add [file]                     # 暫存指定檔案
git mv [old] [new]                 # 重命名檔案
git rm [file]                      # 刪除檔案並暫存
git rm --cached [file]             # 停止追蹤但保留檔案
git commit -m "message"            # 提交所有暫存變更
git commit --amend                 # 修正上一次提交
git commit -a -m "message"         # 跳過暫存區直接提交(僅限已追蹤檔案)

📜 查看提交歷史


git log                            # 完整歷史
git log --pretty=oneline           # 單行顯示
git log -p [file]                  # 指定檔案歷史
git log master..experiment         # experiment 有而 master 無的提交
git blame [file]                   # 檔案逐行顯示最後修改提交
git log origin/featureA ^featureA  # 比較遠端與本地分支差異

↩ 撤銷操作


git reset --hard HEAD              # 捨棄所有未提交修改
git reset --hard [commit]          # 重置至指定提交
git reset HEAD [file]              # 取消暫存(保留修改)
git checkout HEAD [file]           # 捨棄已暫存檔案的修改
git checkout -- [file]             # 捨棄未暫存檔案的修改
git revert [commit]                # 建立新提交撤銷指定提交

🌿 分支與標籤


git branch                         # 列出本地分支
git checkout [branch/tag]          # 切換分支/標籤
git checkout -b featureB origin/master  # 從遠端分支建立並切換
git branch [new-branch]            # 建立新分支
git branch sc/ruby_client master   # 從 master 建立分支
git branch -d [branch]             # 刪除本地分支
git branch --merged                # 已合併進當前分支的分支
git branch --no-merged             # 未合併進當前分支的分支
git tag                            # 列出標籤
git tag [tagname]                  # 以最新提交建標籤
git tag -d [tagname]               # 刪除標籤

🔀 合併與衍合


git merge [branch]                 # 合併指定分支到當前
git rebase [branch]                # 衍合指定分支到當前

☁ 遠端操作


git remote -v                      # 查看遠端倉庫資訊
git remote show [remote]           # 查看指定遠端詳情
git remote add [name] [url]        # 新增遠端
git remote rename [old] [new]      # 重命名遠端
git remote rm [name]               # 移除遠端
git fetch [remote]                 # 取得遠端更新(不合併)
git pull [remote] [branch]         # 取得並快速合併
git push [remote] [branch]         # 推送並快速合併
git push origin local:remote       # 推送本地分支至指定遠端分支
git push [remote] :[branch/tag]    # 刪除遠端分支/標籤
git push --tags                    # 推送所有標籤

🛠 其他實用指令


git describe master                # 產生內部版本號
git archive master --prefix='project/' | gzip > `git describe master`.tar.gz  # 打包 tar.gz
git archive master --prefix='project/' --format=zip > `git describe master`.zip  # 打包 zip
git stash                          # 儲藏當前修改
git stash list                     # 查看儲藏列表
git stash apply stash@{2}          # 套用指定儲藏
git stash drop stash@{0}           # 刪除指定儲藏
git blame -L 12,22 file.rb         # 顯示指定行範圍的 blame

📄 Git 專屬設定檔

.gitattributes (屬性設定)


*.doc diff=word
database.xml merge=ours

.gitignore (忽略規則)


# 註解
*.[oa]        # 忽略 .o .a 結尾檔案
!lib.a        # 但 lib.a 除外
/TODO         # 只忽略根目錄 TODO
build/        # 忽略 build/ 目錄所有檔案
doc/*.txt     # 忽略 doc/ 下 .txt,不含子目錄
Copyright © Windows教程網 All Rights Reserved