git 常用操作

📅 发布时间:2026/7/4 16:24:39 👁️ 浏览次数:
git 常用操作
1.首先判断项目结构情况1单一仓库多模块Monorepoproject/ ├── app/ # 主模块 ├── library1/ # 子模块 ├── library2/ # 子模块 └── build.gradle # 统一构建操作在根目录操作即可Git 会自动管理所有子文件夹。情况2多个独立仓库project/ ├── app/ # 独立 Git 仓库 ├── sdk/ # 独立 Git 仓库 └── docs/ # 独立 Git 仓库操作需要分别进入每个文件夹操作。2.快速判断方法# 在项目根目录查看ls-la# 如果有 .git 文件夹是单一仓库# 如果每个子文件夹都有 .git是多仓库# 或者使用 git 命令cdprojectgitstatus# 如果成功是单一仓库cdproject/appgitstatus# 如果成功说明 app 是独立仓库3.切换分支的完整流程对于单一仓库# 1. 进入项目根目录cd/path/to/project# 2. 查看当前分支gitbranch# 查看本地分支gitbranch -r# 查看远程分支gitbranch -a# 查看所有分支# 3. 切换分支gitcheckout develop# 切换到已存在的分支gitcheckout -b feature/new-branch# 创建并切换到新分支# 4. 拉取最新代码gitpull origin develop# 5. 查看状态确认gitstatus对于多仓库# 方法1手动逐个切换cdappgitcheckout developgitpullcd../sdkgitcheckout developgitpullcd../docsgitcheckout developgitpull# 方法2使用脚本推荐#!/bin/bash# switch_all_branches.shfordirinapp sdk docs;doecho 切换到$dircd$dirgitcheckout developgitpull origin developcd..echodone4.实用的 Git 操作命令查看状态和日志# 查看状态最重要gitstatus# 查看提交历史gitlog --oneline -10# 最近10条gitlog --graph --all# 图形化查看# 查看文件变化gitdiff# 未暂存的修改gitdiff--staged# 已暂存的修改分支操作# 查看分支gitbranch# 本地分支gitbranch -a# 所有分支包括远程# 创建分支gitbranch feature/xxx# 创建分支gitcheckout feature/xxx# 切换到分支# 或一步完成gitcheckout -b feature/xxx# 删除分支gitbranch -d feature/xxx# 删除本地分支gitpush origin --delete feature/xxx# 删除远程分支提交代码# 三步提交法gitadd.# 添加所有修改gitaddfile1 file2# 添加指定文件gitcommit -m描述# 提交到本地gitpush origin branch-name# 推送到远程5.多仓库管理工具使用 Git Submodule子模块# 添加子模块gitsubmoduleaddhttps://github.com/user/repo.git path/to/folder# 克隆包含子模块的项目gitclone --recursive https://github.com/user/main-repo.git# 或克隆后初始化gitclone https://github.com/user/main-repo.gitcdmain-repogitsubmodule initgitsubmodule update# 更新所有子模块gitsubmodule foreachgitpull使用脚本批量操作# 批量切换分支脚本switch_all.sh#!/bin/bashTARGET_BRANCH$1# 传参要切换的分支名echo开始批量切换分支到:$TARGET_BRANCHechoREPOS(appsdkdocsmodule1module2)forrepoin${REPOS[]};doif[-d$repo];thenechoecho处理仓库:$repoecho----------------cd$repo||continue# 检查分支是否存在ifgitrev-parse --verify$TARGET_BRANCH/dev/null21;thenecho切换到分支:$TARGET_BRANCHgitcheckout$TARGET_BRANCHgitpull origin$TARGET_BRANCHelseecho分支$TARGET_BRANCH不存在检查远程...ifgitls-remote --exit-code --heads origin$TARGET_BRANCH/dev/null21;thenecho从远程拉取分支:$TARGET_BRANCHgitfetch origin$TARGET_BRANCHgitcheckout -b$TARGET_BRANCHorigin/$TARGET_BRANCHelseecho警告分支$TARGET_BRANCH在远程也不存在CURRENT_BRANCH$(gitbranch --show-current)echo当前分支:$CURRENT_BRANCHfificd..elseecho警告目录$repo不存在fidoneechoechoecho批量切换完成使用方式chmodx switch_all.sh ./switch_all.sh develop# 切换到 develop 分支./switch_all.sh main# 切换到 main 分支./switch_all.sh feature/xxx# 切换到 feature 分支6.常见问题解决方案问题1切换分支时有未提交的修改# 方法1暂存修改gitstash# 保存修改gitcheckout other-branch# 切换分支gitstash pop# 恢复修改# 方法2强制切换会丢失未提交的修改gitcheckout -f other-branch# 方法3提交当前修改gitadd.gitcommit -m临时提交gitcheckout other-branch问题2远程分支已删除本地还在# 查看所有分支包括远程已删除的gitbranch -a# 清理本地缓存的远程分支gitfetch --prune# 或gitremote prune origin问题3冲突解决# 切换分支前先更新当前分支gitpull origin current-branch# 如果出现冲突gitstatus# 查看冲突文件# 手动解决冲突后gitadd.gitcommit -m解决冲突7.工作流建议每日工作流程# 1. 拉取最新代码gitpull origin develop# 2. 创建功能分支gitcheckout -b feature/your-feature# 3. 开发、提交gitadd.gitcommit -mfeat: 添加功能# 4. 推送到远程gitpush origin feature/your-feature# 5. 创建 Pull Request/Merge Request多仓库同步流程# 创建同步脚本sync_all.sh#!/bin/bashecho开始同步所有仓库...fordirin*/;doif[-d$dir/.git];thenecho同步:$dircd$dir# 获取当前分支CURRENT_BRANCH$(gitbranch --show-current)echo 当前分支:$CURRENT_BRANCH# 拉取最新gitpull origin$CURRENT_BRANCH# 如果有未提交的修改暂存if!gitdiff--quiet;thenecho 有未提交修改正在暂存...gitstashgitpull origin$CURRENT_BRANCHgitstash popficd..echofidoneecho同步完成8.实用别名设置.gitconfig在~/.gitconfig中添加[alias] # 查看状态 st status ls log --oneline -20 graph log --graph --all --oneline # 分支操作 br branch co checkout cob checkout -b # 清理 cleanup !git fetch --prune git branch --merged | grep -v \\*\\|main\\|develop | xargs -n 1 git branch -d # 多仓库操作 all !f() { for dir in */; do if [ -d \$dir/.git\ ]; then echo \ $dir \; cd \$dir\ git $; cd ..; fi; done; }; f使用别名gitst# 代替 git statusgitco develop# 代替 git checkout developgitall status# 查看所有仓库状态gitall pull# 所有仓库拉取更新9.可视化工具推荐如果命令行不熟悉可以使用GitHub Desktop- 简单易用SourceTree- 功能强大VS Code Git 插件- 集成在编辑器中GitKraken- 专业版功能多10.紧急情况处理如果操作混乱了# 1. 先保存当前状态gitstash save紧急备份# 2. 回到安全点gitcheckout developgitpull origin develop# 3. 重新开始gitcheckout -b feature/new-start# 4. 如果需要之前的修改gitstash list# 查看暂存列表gitstash apply stash{0}# 应用最新的暂存如果需要重置# 软重置保留修改gitreset --soft HEAD~1# 硬重置彻底删除gitreset --hard HEAD~1gitreset --hard origin/develop# 与远程一致记住多仓库操作的关键是保持结构清晰使用脚本自动化遇到问题先备份再操作。