一、倉庫初始化與克隆
# 初始化本地倉庫
git init
# 克隆遠程倉庫
git clone https://github.com/user/repo.git
# 克隆并指定本地目錄名
git clone https://github.com/user/repo.git myproject
二、基礎(chǔ)工作流
# 查看工作區(qū)狀態(tài)(高頻)
git status
# 添加文件到暫存區(qū)
git add filename # 指定文件
git add . # 當前目錄所有變更
git add -A # 整個倉庫所有變更(包括刪除)
# 提交變更
git commit -m "feat: 新增用戶登錄模塊"
git commit -am "fix: 修復(fù)并直接提交已跟蹤文件" # 跳過add,僅對已跟蹤文件有效
# 查看提交歷史
git log --oneline --graph --decorate # 簡潔圖形化
git reflog # 查看所有分支操作記錄(救命命令)
三、分支操作(協(xié)作核心)
# 查看分支
git branch # 本地分支
git branch -r # 遠程分支
git branch -a # 所有分支
# 創(chuàng)建并切換分支
git checkout -b feature/user-auth
# 等價于
git branch feature/user-auth
git checkout feature/user-auth
# 切換分支
git checkout main
# 合并分支
git merge feature/user-auth # 合并到當前分支
git merge --no-ff feature/user-auth # 保留分支歷史
# 刪除分支
git branch -d feature/user-auth # 安全刪除(已合并)
git branch -D feature/user-auth # 強制刪除(未合并)
# 重命名分支
git branch -m old-name new-name
四、遠程同步
# 查看遠程倉庫
git remote -v
# 添加遠程倉庫
git remote add origin https://github.com/user/repo.git
# 推送
git push -u origin main # 首次推送需加 -u 建立關(guān)聯(lián)
git push # 后續(xù)直接push
git push origin feature/user-auth # 推送指定分支
# 拉取
git pull # 拉取并自動合并(相當于 fetch + merge)
git fetch origin # 僅拉取不合并(安全)
git fetch --prune # 同步遠程已刪除的分支
# 追蹤遠程分支
git checkout -b local-branch origin/remote-branch
五、撤銷與回退
# 工作區(qū)撤銷(未add)
git checkout -- filename # 丟棄工作區(qū)修改
git checkout -- . # 丟棄所有修改
# 暫存區(qū)撤銷(已add未commit)
git reset HEAD filename # 撤銷add
git reset HEAD # 撤銷所有add
# 回退版本(已commit)
git reset --soft HEAD~1 # 回退到上一個版本,保留工作區(qū)和暫存區(qū)
git reset --mixed HEAD~1 # 默認,回退并清空暫存區(qū)
git reset --hard HEAD~1 # 徹底回退(危險,會丟失修改)
# 回退到指定版本
git reset --hard commit-id # 通過git log查看id(前7位即可)
# 遠程回退(已push)
git revert commit-id # 生成新提交抵消舊提交(安全)
六、標簽管理
# 創(chuàng)建標簽
git tag v1.0.0 # 當前提交打標簽
git tag v1.0.0 commit-id # 指定提交打標簽
git tag -a v1.0.0 -m "發(fā)布1.0.0正式版" # 帶說明的標簽
# 查看標簽
git tag # 列表
git show v1.0.0 # 查看標簽信息
# 推送標簽
git push origin v1.0.0 # 推送單個標簽
git push origin --tags # 推送所有標簽
# 刪除標簽
git tag -d v1.0.0 # 刪除本地
git push origin :refs/tags/v1.0.0 # 刪除遠程
七、高級技巧
# 暫存當前工作
git stash # 保存當前工作區(qū)
git stash list # 查看stash列表
git stash apply # 恢復(fù)最近stash(不刪除)
git stash pop # 恢復(fù)并刪除最近stash
git stash drop stash@{0} # 刪除指定stash
# 交互式添加(分塊提交)
git add -p # 按塊選擇要添加的內(nèi)容
# 查看某行代碼最后修改者
git blame filename # 逐行顯示作者和提交
# 查找提交
git log --grep="關(guān)鍵詞" # 按提交信息搜索
git log -S "代碼內(nèi)容" # 按代碼變動搜索
# 清理無效文件
git clean -fd # 刪除未跟蹤的文件和目錄(危險)
八、常見報錯速查
| 報錯信息 | 快速解決 |
|---|---|
| refusing to merge unrelated histories | git pull origin main --allow-unrelated-histories |
| error: failed to push some refs | 先 git pull 解決沖突再push |
| Your branch is behind 'origin/main' by X commits | git pull 或 git fetch + git merge |
| fatal: not a git repository | 當前目錄未初始化,先 git init 或 git clone |
九、配置與別名
# 全局配置
git config --global user.name "Your Name"
git config --global user.email "you@example.com"
git config --global core.editor "code --wait" # 設(shè)置VS Code為編輯器
# 常用別名
git config --global alias.st status
git config --global alias.co checkout
git config --global alias.br branch
git config --global alias.ci commit
git config --global alias.unstage 'reset HEAD --'
git config --global alias.last 'log -1 HEAD'
git config --global alias.visual '!gitk'
# 使用后:
git st # 等價于 git status
git co main # 等價于 git checkout main
十、總結(jié)
工作區(qū) → git add → 暫存區(qū) → git commit → 本地倉庫 → git push → 遠程倉庫
↑ ↓
git checkout -- git pull / git fetch
最后提醒:
任何撤銷操作前先用 git status 確認狀態(tài)
重要操作前創(chuàng)建臨時分支備份:git branch backup
團隊協(xié)作前先 git pull 再 git push
commit 信息遵循規(guī)范(如Angular規(guī)范):type: subject