[{"data":1,"prerenderedAt":658},["ShallowReactive",2],{"$fn4yensbuhjtv":3,"content:\u002Fposts\u002F0d5f9c3":44,"surround:\u002Fposts\u002F0d5f9c3":647},{"total":4,"annual":7,"categories":16,"tags":28},{"posts":5,"words":6},12,7234,{"2024":8,"2025":11,"2026":13},{"posts":9,"words":10},5,1227,{"posts":9,"words":12},3158,{"posts":14,"words":15},2,2849,[17,20,23,26],{"name":18,"posts":19},"未分类",1,{"name":21,"posts":22},"笔记",7,{"name":24,"posts":25},"杂谈",3,{"name":27,"posts":19},"生活",[29,30,31,32,33,34,35,36,37,38,39,40,41,42,43],"hexo","软件","壁纸","图床","音乐","刷机","twikoo","npm","cdn","秒传","近期小记","旁路由","软路由","OpenClash","Git",{"id":45,"title":46,"body":47,"categories":625,"date":626,"description":627,"draft":628,"extension":629,"image":630,"meta":631,"navigation":633,"path":634,"permalink":634,"published":635,"readingTime":636,"recommend":576,"references":635,"seo":641,"sitemap":642,"stem":643,"tags":644,"type":645,"updated":626,"__hash__":646},"content\u002Fposts\u002F2026\u002F使用Git管理你的项目代码.md","使用 Git 管理项目代码：基础入门与多远程仓库工作流",{"type":48,"value":49,"toc":575},"minimark",[50,54,58,65,82,86,91,94,105,109,112,118,122,126,132,135,141,144,150,153,159,163,166,172,175,181,184,190,194,197,203,206,212,215,221,225,228,234,237,243,247,254,260,263,266,283,286,296,299,302,305,312,318,321,325,360,364,375,379,387,391,394,400,403,409,417,427,433,437,443,446,452,456,462,465,471,474,482,486,489,494,497,501,507,514,518,524,529,535,540,546,560,563,566,569],[51,52,53],"h2",{"id":53},"前言",[55,56,57],"p",{},"新的一年，祝大家新春快乐。",[55,59,60],{},[61,62],"img",{"alt":63,"src":64},"表情包","https:\u002F\u002Fbu.dusays.com\u002F2026\u002F02\u002F17\u002F69946d313230b.webp",[55,66,67,68,71,72,75,76,78,79,81],{},"最近给博客做了一次彻底大升级，抛弃了",[69,70,29],"strong",{},"框架转向了",[69,73,74],{},"astro","框架。在拉取官方仓库到本地进行适配与迁移的过程中，我越来越能感受到",[69,77,43],{},"的重要性。于是乎，这篇就来记录一下我用",[69,80,43],{},"管理本地和远程仓库以及从上游仓库获取更新的方法经验。",[51,83,85],{"id":84},"git基本操作","Git基本操作",[87,88,90],"h3",{"id":89},"_1-配置用户信息","1. 配置用户信息",[55,92,93],{},"首次使用 Git 前，需要配置全局用户名和邮箱：",[95,96,101],"pre",{"className":97,"code":99,"language":100},[98],"language-text","git config --global user.name \"你的用户名\"\ngit config --global user.email \"你的邮箱\"\n","text",[102,103,99],"code",{"__ignoreMap":104},"",[87,106,108],{"id":107},"_2-初始化仓库","2. 初始化仓库",[55,110,111],{},"在项目目录下执行：",[95,113,116],{"className":114,"code":115,"language":100},[98],"git init\n",[102,117,115],{"__ignoreMap":104},[87,119,121],{"id":120},"_3-常用操作命令","3. 常用操作命令",[123,124,125],"h4",{"id":125},"添加文件到暂存区",[95,127,130],{"className":128,"code":129,"language":100},[98],"git add \u003C文件名>        # 添加指定文件\ngit add .              # 添加所有文件\n",[102,131,129],{"__ignoreMap":104},[123,133,134],{"id":134},"提交更改",[95,136,139],{"className":137,"code":138,"language":100},[98],"git commit -m \"提交说明\"\n",[102,140,138],{"__ignoreMap":104},[123,142,143],{"id":143},"查看状态",[95,145,148],{"className":146,"code":147,"language":100},[98],"git status\n",[102,149,147],{"__ignoreMap":104},[123,151,152],{"id":152},"查看提交历史",[95,154,157],{"className":155,"code":156,"language":100},[98],"git log\n",[102,158,156],{"__ignoreMap":104},[87,160,162],{"id":161},"_4-远程仓库操作","4. 远程仓库操作",[123,164,165],{"id":165},"关联远程仓库",[95,167,170],{"className":168,"code":169,"language":100},[98],"git remote add origin \u003C远程仓库地址>\n",[102,171,169],{"__ignoreMap":104},[123,173,174],{"id":174},"推送到远程仓库",[95,176,179],{"className":177,"code":178,"language":100},[98],"git push -u origin main\n",[102,180,178],{"__ignoreMap":104},[123,182,183],{"id":183},"从远程仓库拉取",[95,185,188],{"className":186,"code":187,"language":100},[98],"git pull origin main\n",[102,189,187],{"__ignoreMap":104},[87,191,193],{"id":192},"_5-分支管理","5. 分支管理",[123,195,196],{"id":196},"创建新分支",[95,198,201],{"className":199,"code":200,"language":100},[98],"git branch \u003C分支名>\n",[102,202,200],{"__ignoreMap":104},[123,204,205],{"id":205},"切换分支",[95,207,210],{"className":208,"code":209,"language":100},[98],"git checkout \u003C分支名>\n# 或\ngit switch \u003C分支名>\n",[102,211,209],{"__ignoreMap":104},[123,213,214],{"id":214},"合并分支",[95,216,219],{"className":217,"code":218,"language":100},[98],"git merge \u003C要合并的分支名>\n",[102,220,218],{"__ignoreMap":104},[87,222,224],{"id":223},"_6-撤销操作","6. 撤销操作",[123,226,227],{"id":227},"撤销工作区修改",[95,229,232],{"className":230,"code":231,"language":100},[98],"git checkout -- \u003C文件名>\n",[102,233,231],{"__ignoreMap":104},[123,235,236],{"id":236},"撤销暂存区文件",[95,238,241],{"className":239,"code":240,"language":100},[98],"git reset HEAD \u003C文件名>\n",[102,242,240],{"__ignoreMap":104},[87,244,246],{"id":245},"_7-忽略文件","7. 忽略文件",[55,248,249,250,253],{},"创建 ",[102,251,252],{"code":252},".gitignore","文件，列出需要忽略的文件和文件夹：",[95,255,258],{"className":256,"code":257,"language":100},[98],"node_modules\u002F\n*.log\n.env\n",[102,259,257],{"__ignoreMap":104},[55,261,262],{},"以上是 Git 常用的基本操作",[51,264,265],{"id":265},"实践与优化",[55,267,268,269,276,277,282],{},"我的博客使用的是",[270,271,275],"a",{"href":272,"rel":273},"https:\u002F\u002Fastro.build\u002F",[274],"nofollow","Astro框架","＋",[270,278,281],{"href":279,"rel":280},"https:\u002F\u002Fgithub.com\u002FCuteLeaf\u002FFirefly",[274],"Firefly主题","。",[55,284,285],{},"我最开始的管理方案很简单，就是先fork官方仓库，然后clone下来到本地进行修改，之后建一个新的分支用来放修改后的代码，之后全部推送到github就完了。用这种方案管理代码流程简单，也方便给官方仓库提pr。",[55,287,288,289,292,293,282],{},"如果你只clone代码到本地，不推送到github上的话，那么我认为这种方法很不错。如果你想将代码提交到github进行备份并使用github的工作流的话，那么有一点我不太能接受——你fork下来的仓库是公开权限，这意味着",[69,290,291],{},"任何人","都能获取你的所有代码，包括可能填写在代码中的token、key之类的",[69,294,295],{},"隐私内容",[87,297,298],{"id":298},"方案优化",[55,300,301],{},"有没有什么便于管理与更新代码，且没有安全风险，又能白嫖github算力，同时日后还能提pr贡献代码的方案呢？",[55,303,304],{},"有的兄弟有的",[55,306,307,308,311],{},"我们只需要给本地的git仓库增加多个",[69,309,310],{},"远程仓库","即可。一个是你的私有仓库origin，另一个是你的项目的上游官方仓库upstream，还有你的fork仓库（提PR，可选）",[95,313,316],{"className":314,"code":315,"language":100},[98],"origin   → https:\u002F\u002Fgithub.com\u002Flmb666666\u002Ffirefly-blog.git （私有仓库）\nupstream → https:\u002F\u002Fgithub.com\u002FCuteLeaf\u002FFirefly.git （原始上游仓库）\nmyfork   → https:\u002F\u002Fgithub.com\u002Flmb666666\u002FFirefly.git （fork仓库）\n",[102,317,315],{"__ignoreMap":104},[87,319,320],{"id":320},"具体步骤",[123,322,324],{"id":323},"第一步在-github-上创建私有仓库","第一步：在 GitHub 上创建私有仓库",[326,327,328,336,343,348,354],"ol",{},[329,330,331,332],"li",{},"打开 ",[270,333,334],{"href":334,"rel":335},"https:\u002F\u002Fgithub.com\u002Fnew",[274],[329,337,338,339,342],{},"仓库名填写 ",[102,340,341],{"code":341},"firefly-blog","（或你喜欢的名字）",[329,344,345],{},[69,346,347],{},"选择 Private",[329,349,350,353],{},[69,351,352],{},"不要勾选"," \"Add a README file\" 等任何初始化选项",[329,355,356,357],{},"点击 ",[69,358,359],{},"Create repository",[123,361,363],{"id":362},"第二步查看原始上游仓库地址","第二步：查看原始上游仓库地址",[55,365,366,367,370,371,374],{},"到GitHub上查看你的",[69,368,369],{},"原始上游仓库","地址。假设原始仓库是 ",[102,372,373],{"code":373},"https:\u002F\u002Fgithub.com\u002FCuteLeaf\u002FFirefly.git","（请根据实际情况替换）。",[123,376,378],{"id":377},"第三步拉取原始上游仓库代码","第三步：拉取原始上游仓库代码",[95,380,385],{"className":381,"code":383,"language":384,"meta":104},[382],"language-bash","git clone https:\u002F\u002Fgithub.com\u002FCuteLeaf\u002FFirefly.git\ncd Firefly\n","bash",[102,386,383],{"__ignoreMap":104},[123,388,390],{"id":389},"第四步重新配置本地remote","第四步：重新配置本地remote",[55,392,393],{},"在你的项目目录下依次执行：",[95,395,398],{"className":396,"code":397,"language":384,"meta":104},[382],"# 1. 将当前的 origin（原始上游仓库）改名为 upstream\ngit remote rename origin upstream\n\n# 2. 添加你的私有仓库作为新的 origin\ngit remote add origin https:\u002F\u002Fgithub.com\u002Flmb666666\u002Ffirefly-blog.git\n\n# 3. 验证配置\ngit remote -v\n",[102,399,397],{"__ignoreMap":104},[55,401,402],{},"预期输出：",[95,404,407],{"className":405,"code":406,"language":100},[98],"origin    https:\u002F\u002Fgithub.com\u002Flmb666666\u002Ffirefly-blog.git (fetch)\norigin    https:\u002F\u002Fgithub.com\u002Flmb666666\u002Ffirefly-blog.git (push)\nupstream  https:\u002F\u002Fgithub.com\u002FCuteLeaf\u002FFirefly.git (fetch)\nupstream  https:\u002F\u002Fgithub.com\u002FCuteLeaf\u002FFirefly.git (push)\n",[102,408,406],{"__ignoreMap":104},[123,410,412,413,416],{"id":411},"第五步创建并切换到custom开发分支","第五步：创建并切换到",[102,414,415],{"code":415},"custom","开发分支",[55,418,419,420,423,424,426],{},"为了不影响主分支，我们创建一个专门用于",[69,421,422],{},"开发","的 ",[102,425,415],{"code":415}," 分支。",[95,428,431],{"className":429,"code":430,"language":384,"meta":104},[382],"# 从当前分支（通常是 master\u002Fmain）创建 custom 分支\ngit checkout -b custom\n# 或者先创建再切换\n# git branch custom\n# git checkout custom\n",[102,432,430],{"__ignoreMap":104},[123,434,436],{"id":435},"第六步日常开发","第六步：日常开发",[55,438,439,440,442],{},"在 ",[102,441,415],{"code":415}," 分支上进行日常的功能开发、bug修复等工作。",[55,444,445],{},"将修改的文件添加到暂存区，并提交到本地仓库。",[95,447,450],{"className":448,"code":449,"language":384,"meta":104},[382],"# 添加所有修改过的文件\ngit add .\n# 或者只添加特定文件\n# git add \u003Cfile_name>\n\n# 提交更改，并附上清晰的提交信息\ngit commit -m \"Your descriptive commit message here\"\n",[102,451,449],{"__ignoreMap":104},[123,453,455],{"id":454},"第七步推送所有分支到私有仓库","第七步：推送所有分支到私有仓库",[95,457,460],{"className":458,"code":459,"language":384,"meta":104},[382],"# 推送 master 分支\ngit push -u origin master\n\n# 推送 custom 分支（你的个人修改）\ngit push -u origin custom\n",[102,461,459],{"__ignoreMap":104},[123,463,464],{"id":464},"日后同步上游更新",[95,466,469],{"className":467,"code":468,"language":384,"meta":104},[382],"# 获取上游最新代码\ngit fetch upstream\n\n# 切到 master 分支，合并上游更新\ngit checkout master\ngit merge upstream\u002Fmaster\ngit push origin master\n\n# 切到 custom 分支，合并 master 的更新\ngit checkout custom\ngit merge master\ngit push origin custom\n",[102,470,468],{"__ignoreMap":104},[123,472,473],{"id":473},"流程图",[95,475,480],{"className":476,"code":478,"language":479,"meta":104},[477],"language-mermaid","sequenceDiagram\n    participant U as 上游仓库 (upstream)\n    participant L as 本地仓库\n    participant P as 私有仓库 (origin)\n\n    Note over L: 日常在 custom 分支开发\n    L->>L: git add & commit\n    L->>P: git push origin custom\n\n    Note over L: 同步上游更新\n    U->>L: git fetch upstream\n    L->>L: git checkout master\n    L->>L: git merge upstream\u002Fmaster\n    L->>P: git push origin master\n    L->>L: git checkout custom\n    L->>L: git merge master\n    L->>P: git push origin custom\n","mermaid",[102,481,478],{"__ignoreMap":104},[123,483,485],{"id":484},"提交pr贡献代码","提交PR？贡献代码?",[55,487,488],{},"如果想要给上游官方仓库提交代码的话可以再添加一个你的fork仓库",[490,491,493],"h5",{"id":492},"第一步在-github-上-fork-原始仓库","第一步：在 GitHub 上 Fork 原始仓库",[55,495,496],{},"打开原始上游仓库的GitHub页面，点击右上角 \"Fork\" 按钮，创建你自己的 fork。",[490,498,500],{"id":499},"第二步添加-fork-为新的-remote","第二步：添加 fork 为新的 remote",[95,502,505],{"className":503,"code":504,"language":384,"meta":104},[382],"git remote add myfork https:\u002F\u002Fgithub.com\u002Flmb666666\u002FFirefly.git\n",[102,506,504],{"__ignoreMap":104},[55,508,509,510,513],{},"（fork 后的仓库地址通常是 ",[102,511,512],{"code":512},"https:\u002F\u002Fgithub.com\u002F你的用户名\u002FFirefly.git","）",[490,515,517],{"id":516},"第三步基于-upstreammaster-创建功能分支","第三步：基于 upstream\u002Fmaster 创建功能分支",[95,519,522],{"className":520,"code":521,"language":384,"meta":104},[382],"git fetch upstream\ngit checkout -b feature\u002F你的功能名称 upstream\u002Fmaster\n",[102,523,521],{"__ignoreMap":104},[55,525,526],{},[69,527,528],{},"第四步：把你想提交的改动 cherry-pick 或手动应用到功能分支上",[95,530,533],{"className":531,"code":532,"language":384,"meta":104},[382],"git cherry-pick \u003C你的commit-hash>\n",[102,534,532],{"__ignoreMap":104},[55,536,537],{},[69,538,539],{},"第五步：推送到 fork",[95,541,544],{"className":542,"code":543,"language":384,"meta":104},[382],"git push myfork feature\u002F你的功能名称\n",[102,545,543],{"__ignoreMap":104},[55,547,548,551,552,555,556,559],{},[69,549,550],{},"第六步：在 GitHub 上创建 PR","\n打开你的 fork 页面，点击 \"Contribute\" → \"Open pull request\"，选择向 ",[102,553,554],{"code":554},"CuteLeaf\u002FFirefly"," 的 ",[102,557,558],{"code":558},"master"," 分支提交。",[561,562],"hr",{},[51,564,565],{"id":565},"碎碎念",[55,567,568],{},"写完这篇博文已经2月18日了，寒假过了一大半了，好想让时间倒流。",[55,570,571],{},[61,572],{"alt":573,"src":574},"悲伤","https:\u002F\u002Fbu.dusays.com\u002F2026\u002F02\u002F18\u002F69958464a0213.webp",{"title":104,"searchDepth":576,"depth":576,"links":577},4,[578,579,604,624],{"id":53,"depth":14,"text":53},{"id":84,"depth":14,"text":85,"children":580},[581,582,583,589,594,599,603],{"id":89,"depth":25,"text":90},{"id":107,"depth":25,"text":108},{"id":120,"depth":25,"text":121,"children":584},[585,586,587,588],{"id":125,"depth":576,"text":125},{"id":134,"depth":576,"text":134},{"id":143,"depth":576,"text":143},{"id":152,"depth":576,"text":152},{"id":161,"depth":25,"text":162,"children":590},[591,592,593],{"id":165,"depth":576,"text":165},{"id":174,"depth":576,"text":174},{"id":183,"depth":576,"text":183},{"id":192,"depth":25,"text":193,"children":595},[596,597,598],{"id":196,"depth":576,"text":196},{"id":205,"depth":576,"text":205},{"id":214,"depth":576,"text":214},{"id":223,"depth":25,"text":224,"children":600},[601,602],{"id":227,"depth":576,"text":227},{"id":236,"depth":576,"text":236},{"id":245,"depth":25,"text":246},{"id":265,"depth":14,"text":265,"children":605},[606,607],{"id":298,"depth":25,"text":298},{"id":320,"depth":25,"text":320,"children":608},[609,610,611,612,613,615,616,617,618,619],{"id":323,"depth":576,"text":324},{"id":362,"depth":576,"text":363},{"id":377,"depth":576,"text":378},{"id":389,"depth":576,"text":390},{"id":411,"depth":576,"text":614},"第五步：创建并切换到custom开发分支",{"id":435,"depth":576,"text":436},{"id":454,"depth":576,"text":455},{"id":464,"depth":576,"text":464},{"id":473,"depth":576,"text":473},{"id":484,"depth":576,"text":485,"children":620},[621,622,623],{"id":492,"depth":9,"text":493},{"id":499,"depth":9,"text":500},{"id":516,"depth":9,"text":517},{"id":565,"depth":14,"text":565},[21],"2026-02-18 22:48:30","从零掌握 Git 版本管理，涵盖用户配置、提交、分支与远程操作，并分享私有仓库 + 上游仓库多 remote 的协同工作流与提 PR 的方法。",false,"md","https:\u002F\u002Fbu.dusays.com\u002F2026\u002F02\u002F18\u002F699585a55c910.webp",{"slots":632},{},true,"\u002Fposts\u002F0d5f9c3",null,{"text":637,"minutes":638,"time":639,"words":640},"8 min read",7.725,463500,1545,{"title":46,"description":627},{"loc":634},"posts\u002F2026\u002F使用Git管理你的项目代码",[43],"tech","B7xvfs4imocfTE0DtFnBtsDhCb3JCj3gNTEWAg_hXBs",[648,653],{"title":649,"path":650,"stem":651,"date":652,"type":645,"children":-1},"近期小记（1）","\u002Fposts\u002F6b4e8a1","posts\u002F2025\u002F近况小记-1","2025-11-22 21:36:25",{"title":654,"path":655,"stem":656,"date":657,"type":645,"children":-1},"R3S旁路由网络环境配置","\u002Fposts\u002Fdde5ac1","posts\u002F2026\u002FR3S旁路由网络环境配置","2026-08-01 21:52:09",1785905120539]