
# 创建 .gitignore 规则文件在 git 仓的根目录下若无则新建 .gitignore 规则文件 touch .gitignore # 编辑 .gitignore 并 commit 它使之生效。 # .gitignore 规则编辑时注意事项 # 1. 所有的注释只能是独占单行注释不能在有效代码后注释否者不生效比如错误示范 # 实例 MDK/ #忽略MDK目录下所有内容 (跟在有效代码后注释非法) # 2. 注意务必使用 UTF-8 编码格式去编辑 .gitignore 文件。 # .gitignore 使用小提示 # 1. 提前准备好 .gitignore 文件再 git init 创建仓.gitignore规则直接生效 # 2. 删除MDK下已跟踪的信息git rm --cached MDK/ -r # 3. 根据文件夹的排他性比如.o文件可以比较检测前后变化定位变动的.o文件 # 忽略所有.o文件 *.o # 忽略MDK目录下所有内容 MDK/“.gitignore” 规则只对未跟踪的文件有效对已跟踪的文件无效。# 若已存在想忽略但已经被跟踪的文件则可以 # 1. 编辑 .gitignore 文件添加新的忽略规则 # 2. 从 Git 中移除已跟踪的对象比如 debian/binary git rm -r --cached debian/binary/ # 提交更改 git commit -m 忽略 debian/binary 目录1. .gitignore 的“管辖范围”是固定的Git 官方文档对.gitignore的定义非常明确它只作用于未被跟踪的文件。当一个文件被git add并提交后它的元数据inode、路径就永久记录在了 Git 的对象数据库中。之后.gitignore里的规则在 Git 看来属于“事后提醒”但 Git 会回答“对不起这个文件我认识它已经是我的资产了我不能无视它的变化。”2.git rm --cached执行后的状态变化你提到的git rm --cached是标准的“请出去”操作执行后状态会如下状态执行前 (.arts/ 已被跟踪)执行git rm --cached .arts/后提交后Git 索引/暂存区有记录删除了无记录工作区 (本地磁盘)文件存在文件依然存在不会被物理删除文件依然存在Git 是否继续提示变更修改会显示modified不再提示因为被.gitignore命中了彻底消失于版本库Git 的优先级其实很简单tracked已跟踪 .gitignore untracked未跟踪文件是否已被 Git 跟踪 / \ 是 否 ↓ ↓ .gitignore 无效 .gitignore 是否匹配 始终显示修改 / \ 是 否 ↓ ↓ 忽略不提示 提示为未跟踪你的.arts/文件已经在第一个 commit 里被跟踪了所以.gitignore对它们就是无效的。.gitignore的职责只有一个在git add时自动跳过匹配的未跟踪文件。它不对已跟踪文件产生任何作用。打个比方已跟踪的文件就像 Git 已经认识的人 — 不管他穿什么衣服修改了什么Git 都会注意到.gitignore像门口保安只拦住还没进来的人未跟踪文件保安不会把已经坐在屋里的人赶出去所以只需要git rm --cached把他们请出去从跟踪列表删除之后再.gitignore就能拦住他们了。 总结一句话你的认知完全正确已经进门的已跟踪保安.gitignore管不着得先请出去git rm --cached保安才会拦住它。git ignore 语法规则Pattern SyntaxHere are some common patterns and how they match:PatternExplanation/MatchesExamplesBlank lines are ignored#text commentLines starting with # are ignorednameAllnamefiles,namefolders, and files and folders in anynamefolder/name.log/name/file.txt/lib/name.logname/Ending with / specifies the pattern is for a folder. Matches all files and folders in anynamefolder/name/file.txt/name/log/name.logno match:/name.logname.fileAll files with thename.file/name.file/lib/name.file/name.fileStarting with / specifies the pattern matches only files in the root folder/name.fileno match:/lib/name.filelib/name.filePatterns specifiing files in specific folders are always realative to root (even if you do not start with / )/lib/name.fileno match:name.file/test/lib/name.file**/lib/name.fileStarting with ** before / specifies that it matches any folder in the repository. Not just on root./lib/name.file/test/lib/name.file**/nameAllnamefolders, and files and folders in anynamefolder/name/log.file/lib/name/log.file/name/lib/log.file/lib/**/nameAllnamefolders, and files and folders in anynamefolder within the lib folder./lib/name/log.file/lib/test/name/log.file/lib/test/ver1/name/log.fileno match:/name/log.file*.fileAll files withe.fileextention/name.file/lib/name.file*name/All folders ending withname/lastname/log.file/firstname/log.filename?.file? matches asinglenon-specific character/names.file/name1.fileno match:/names1.filename[a-z].file[range] matches asinglecharacter in the specified range (in this case a character in the range of a-z, and also be numberic.)/names.file/nameb.fileno match:/name1.filename[abc].file[set] matches asinglecharacter in the specified set of characters (in this case either a, b, or c)/namea.file/nameb.fileno match:/names.filename[!abc].file[!set] matches asinglecharacter,exceptthe ones spesified in the set of characters (in this case a, b, or c)/names.file/namex.fileno match:/namesb.file*.fileAll files withe.fileextention/name.file/lib/name.filename/!name/secret.log! specifies a negation or exception. Matches all files and folders in anynamefolder, except name/secret.log/name/file.txt/name/log/name.logno match:/name/secret.log*.file!name.file! specifies a negation or exception. All files withe.fileextention, except name.file/log.file/lastname.fileno match:/name.file*.file!name/*.filejunk.*Adding new patterns after a negation will re-ignore a previous negated fileAll files withe.fileextention, except the ones innamefolder. Unless the file name is junk/log.file/name/log.fileno match:/name/junk.fileGit Restore 与 Reset --Hard 核心区别二者都可以丢弃修改但影响范围和操作层级不同对比项git restoregit reset --hard作用目标工作区/暂存区工作区/暂存区/提交历史安全级别相对安全⚠️ 高危操作影响范围单个文件/指定路径整个项目历史修改不改变提交历史改变提交历史HEAD位置保持不动HEAD指针移动 1.git restore(现代安全方式)# 丢弃工作区所有未暂存的修改 git restore . # 丢弃暂存区的修改保留工作区修改 git restore --staged file # 同时丢弃工作区暂存区修改 git restore --worktree --staged file定位精细控制仅作用于文件层级特点不动历史提交不改变HEAD指针适用场景撤消本地未提交的修改 2.git reset --hard(强力清除)# 丢弃所有未提交的修改工作区暂存区 git reset --hard # 重置到指定提交丢弃后续所有提交 git reset --hard commit_id定位原子级清除影响整个仓库状态特点会改变提交历史移动HEAD指针危险点永久删除未提交的代码和未推送的提交⚖️ 使用场景决策树 重要提示执行reset --hard前务必确认git status # 查看将被删除的内容已推送的提交避免使用reset --hard会破坏团队协作历史推荐使用git restore替代旧版git checkout -- 黄金法则日常修改丢弃用restore历史版本回退用reset --hard 指定commit同时将本地仓库推送到 GitHub 和 Gitee码云查看当前远程配置git remote -v添加多个远程仓库# 添加 Gitee git remote add gitee https://gitee.com/mapleay/display_-ic_-demo_-stm32-h743-iit6-ltdc_from-scratch.git # 添加 GitHub git remote add github https://github.com/your_username/your_repo.git #日常同步推送两种方式 git push gitee main git push github main