git 的 .gitignore 规则文件 、git restore 、 git reset

# .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/

git ignore 语法规则

Pattern Syntax

Here are some common patterns and how they match:

PatternExplanation/MatchesExamples
Blank lines are ignored
text commentLines starting with # are ignored
nameAll name files, name folders, and files and folders in any name folder/name.log
/name/file.txt
/lib/name.log
name/Ending with / specifies the pattern is for a folder. Matches all files and folders in any name folder/name/file.txt
/name/log/name.log

no match:
/name.log
name.fileAll files with the name.file/name.file
/lib/name.file
/name.fileStarting with / specifies the pattern matches only files in the root folder/name.file

no match:
/lib/name.file
lib/name.filePatterns specifiing files in specific folders are always realative to root (even if you do not start with / )/lib/name.file

no 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
**/nameAll name folders, and files and folders in any name folder/name/log.file
/lib/name/log.file
/name/lib/log.file
/lib/**/nameAll name folders, and files and folders in any name folder within the lib folder./lib/name/log.file
/lib/test/name/log.file
/lib/test/ver1/name/log.file

no match:
/name/log.file
*.fileAll files withe .file extention/name.file
/lib/name.file
*name/All folders ending with name/lastname/log.file
/firstname/log.file
name?.file? matches a single non-specific character/names.file
/name1.file

no match:
/names1.file
name[a-z].file[range] matches a single character in the specified range (in this case a character in the range of a-z, and also be numberic.)/names.file
/nameb.file

no match:
/name1.file
name[abc].file[set] matches a single character in the specified set of characters (in this case either a, b, or c)/namea.file
/nameb.file

no match:
/names.file
name[!abc].file[!set] matches a single character, except the ones spesified in the set of characters (in this case a, b, or c)/names.file
/namex.file

no match:
/namesb.file
*.fileAll files withe .file extention/name.file
/lib/name.file
name/
!name/secret.log
! specifies a negation or exception. Matches all files and folders in any name folder, except name/secret.log/name/file.txt
/name/log/name.log

no match:
/name/secret.log
*.file
!name.file
! specifies a negation or exception. All files withe .file extention, except name.file/log.file
/lastname.file

no match:
/name.file
*.file
!name/*.file
junk.*
Adding new patterns after a negation will re-ignore a previous negated file
All files withe .file extention, except the ones in name folder. Unless the file name is junk
/log.file
/name/log.file

no match:
/name/junk.file

Git 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指针
  • 危险点​:永久删除未提交的代码和未推送的提交

⚖️ 使用场景决策树

🚨 重要提示

  1. 执行 reset --hard ​前务必确认​:
    git status # 查看将被删除的内容
  2. 已推送的提交避免使用 reset --hard,会破坏团队协作历史
  3. 推荐使用 git restore 替代旧版 git checkout --

📌 ​黄金法则​:日常修改丢弃用 restore,历史版本回退用 reset --hard + 指定commit

同时将本地仓库推送到 GitHub 和 Gitee(码云)

  1. 查看当前远程配置

    git remote -v
  2. 添加多个远程仓库

    # 添加 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

 

 

<think>好的,用户的问题是询问如何查看Git中待提交的更改详情以及如何取消暂存文件。首先,我需要回忆Git的相关命令。记得使用`git status`可以查看当前仓库的状态,包括已暂存和未暂存的更改。用户可能已经运行过`git add`,现在想撤销暂存的文件。 接下来,关于取消暂存,Git提供了不同的方法。在较新的版本中,推荐使用`git restore --staged <file>`,而旧版本可能用`git reset HEAD <file>`。需要确认用户使用的Git版本,但通常建议使用`git restore`,因为它是更直观的命令。 然后,用户提到的引用内容中有几个解决方案。引用[2]和[3]都提到了`git restore --staged`,这可能就是正确答案。需要确保在回答中明确说明这个命令的使用方法,并举例说明,比如撤销单个文件或所有文件。 另外,用户可能还想知道如何查看详细的更改内容,这时候可以建议使用`git diff --staged`来查看已暂存的更改与最后一次提交的差异。这部分信息对用户了解具体修改内容很有帮助。 还需要考虑用户可能的误操作,比如错误地暂存了文件,如何快速撤销。这时候分情况讨论:撤销单个文件用`git restore --staged <file>`,撤销所有文件则用`git restore --staged .`,但需要谨慎使用后者,避免不必要的撤销。 最后,总结步骤,确保用户能够按照清晰的步骤操作。同时,根据用户提供的引用,特别是引用[3]中提到的提交信息格式问题,提醒用户在提交时注意语法,比如空格的使用,虽然这可能不是当前问题的重点,但可以作为补充建议。 检查是否有遗漏的细节,比如是否要提醒用户提交前检查状态,或者如何避免常见错误。确保回答结构清晰,分步骤说明,并引用提供的参考资料来增强可信度。</think>### 查看Git待提交的更改详情 1. **查看当前状态**: 运行`git status`,会显示两类信息: - `Changes to be committed`:已暂存但未提交的更改(绿色显示) - `Changes not staged for commit`:未暂存的修改(红色显示) 2. **查看具体修改内容**: 使用`git diff --staged`查看已暂存文件与最后一次提交的差异[^2]。 --- ### 取消暂存文件的方法 #### 1. 撤销单个文件的暂存 ```bash git restore --staged <文件名> ``` 例如撤销`example.sql`的暂存: ```bash git restore --staged example.sql ``` #### 2. 撤销所有文件的暂存 ```bash git restore --staged . ``` (谨慎使用,避免意外撤销必要文件)[^3] --- ### 操作示例 假设误将`test.sql`添加到暂存区: 1. 查看当前状态: ```bash git status ``` 输出示例: ``` On branch master Changes to be committed: (use "git restore --staged <file>..." to unstage) modified: test.sql ``` 2. 取消`test.sql`的暂存: ```bash git restore --staged test.sql ``` 3. 再次检查状态: ```bash git status ``` 此时`test.sql`会回到未暂存状态。 --- ### 注意事项 - 若提交信息格式错误(如缺少空格),可能导致提交失败,需检查命令格式[^3]。 - 若文件已提交,需通过`git reset`回退版本。 ---
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值