G&GH05 删除文件和.gitignore
注意事项与声明
平台: Windows 10
作者: JamesNULLiu
邮箱: jamesnulliu@outlook.com
博客: https://www.cnblogs.com/jamesnulliu
学习笔记 转载请注明出处 欢迎留言
0. 前言
本系列文章是 git & github 的入门教程.
本系列文章优势:
- 零基础
- 深入浅出
- 知识点涵盖面广
尽管如此, 想要真正学会 git & github 建议不要看任何教程, 直接看 git 的 官方文档.
1. [rm] 删除文件
- 当 远程储存库, git储存库 和 本地文件夹 内都存在相同的文件 "a.bbb", 并且 想彻底从储存库中删除这个文件.
先用git rm删除文件 (直接在文件夹内右键删除也行):$ git rm a.bbb
查看状态:
$ git status
On branch master
Your branch is up to date with 'origin/main'.
Changes to be committed:
(use "git restore --staged <file>..." to unstage)
deleted: a.bbb
这里注意, 根据他的提示, 如果错误地 add 了某文件 (例如 "c.ddd") 到 staged area, 就用命令
git restore --staged c.ddd取消添加.
接着不用add, 只需要commit和push.$ git commit -m "<message>"
$ git push origin main
这样这个文件就被同时从本地和远程删除了.
- [不建议] 当 git储存库 和 本地文件夹 内存在文件 "a.bbb" 但不相同 (就是先前 commit 了这个文件到 git repository 之后本地又进行了修改), 并且想彻底从 远程 和 git储存库 还有本地 工作文件夹 中删除这个文件.
这种情况需要强制删除 (force):$ git rm -f "a.bbb"
$ git commit -m "<message>"
$ git push origin main
- 当 远程储存库, git储存库 和 本地文件夹 内都存在相同的文件 "a.bbb", 而 只想删除 远程 和 git储存库 中的文件, 并将文件保留在本地 工作文件夹.
使用git rm --cached <file>:$ git rm --cached a.bbb
$ git commit -m "<message>"
$ git push origin main
2. .gitignore 的使用
实际开发过程中, 我们经常会希望不要将项目运行过程中生成的一些中间文件, 日志文件, 生成文件等同步到储存库里.
比如用 visual stutdio 编译并生成一段代码, 工作文件夹内会出现一些 .log 呀, .obj 呀这样的文件. 这些文件对版本管理来说可能没有很大的用处.
尽管我们可以用 git status 配合 git add <file> 一个个将自己想要推送到储存库的文件打到命令行里, 但如果面对一个很大的项目, 要添加非常多文件, 这样效率会十分低.
我们还需要知道, 下面的命令可以将 git status 输出显示的所有改动/新增的文件添加的 staged area 中:
git add .
这就是为什么我们应该写一个 ".gitignore" 文档, 用来让 git 知道哪些文件或文件夹应该被 untracked, 然后选择性地用 git add . 快速添加文件.
.gitignore 文件应该在 working tree 的最高层创建. 打开项目文件夹后, 右键新建文本文档, 吧文件名和 ".txt" 全删了只保留 ".gitignore".
以下是完整的书写规则:
A blank line matches no files, so it can serve as a separator for readability.
A line starting with # serves as a comment. Put a backslash (" \ ") in front of the first hash for patterns that begin with a hash.
Trailing spaces are ignored unless they are quoted with backslash (" \ ").
An optional prefix " ! " which negates the pattern; any matching file excluded by a previous pattern will become included again. It is not possible to re-include a file if a parent directory of that file is excluded. Git doesn’t list excluded directories for performance reasons, so any patterns on contained files have no effect, no matter where they are defined. Put a backslash (" \ ") in front of the first " ! " for patterns that begin with a literal " ! ", for example, "!important!.txt".
The slash / is used as the directory separator. separators may occur at the beginning, middle or end of the .gitignore search pattern.
If there is a separator at the beginning or middle (or both) of the pattern, then the pattern is relative to the directory level of the particular .gitignore file itself. Otherwise the pattern may also match at any level below the .gitignore level.
If there is a separator at the end of the pattern then the pattern will only match directories, otherwise the pattern can match both files and directories.
For example, a pattern doc/frotz/ matches doc/frotz directory, but not a/doc/frotz directory; however frotz/ matches frotz and a/frotz that is a directory (all paths are relative from the .gitignore file).
An asterisk " * " matches anything except a slash. The character " ? " matches any one character except " / ". The range notation, e.g. [a-zA-Z], can be used to match one of the characters in a range.
Two consecutive asterisks (" ** ") in patterns matched against full pathname may have special meaning:
A leading " ** " followed by a slash means match in all directories. For example, " **/foo " matches file or directory "foo" anywhere, the same as pattern "foo". "**/foo/bar" matches file or directory "bar" anywhere that is directly under directory "foo".
A trailing " /** " matches everything inside. For example, " abc/** " matches all files inside directory "abc", relative to the location of the .gitignore file, with infinite depth.
A slash followed by two consecutive asterisks then a slash matches zero or more directories. For example, "a/**/b" matches "a/b", "a/x/b", "a/x/y/b" and so on.
Other consecutive asterisks are considered regular asterisks and will match according to the previous rules.
参考:
Pro Git, 2nd Edition(2014), Scott Chacon
https://git-scm.com/docs/gitignore
G&GH05 删除文件和.gitignore的更多相关文章
- 删除文件夹工具【fuckwinfsdel】,如 node_modules
强力删除文件夹. 安装 npm install fuckwinfsdel -g 使用 fuckwinfsdel youdir 例 fuckwinfsdel node_modules 项目地址 http ...
- linux删除文件后没有释放空间
转载 http://blog.csdn.net/wyzxg/article/details/4971843 今天发现一台服务器的home空间满了,于是要清空无用的文件,当我删除文件后,发现可用空间没有 ...
- 【改造Linux命令之rm - 删除文件或目录-】
用途说明 rm命令是常用的命令,用来删除文件或目录(remove files or directories).它也是一个危险的命令,使用的时候要特别当心,尤其对于新手,否则整个系统就会毁在这个命令(比 ...
- Java NIO FileVisitor 高效删除文件
在公司项目中,由于做个二维码扫码平台项目,预计每天产生的二维码图片达到十几G,所以要做个定时清理任务来定时清理图片,根据不同场景保留图片,规则是:1.二维码统一登录图片几个小时有效 2.电子名片二 ...
- 解决linux删除文件后空间没有释放问题
linux删除文件后沒有释放空间 今天发现一台服务器的home空间满了,于是要清空没用的文件,当我删除文件后,发现可用空间沒有变化 os:centos4.7 现象: 发现当前磁盘空间使用情况: [ro ...
- linux文件权限总结(创建root不可以删除文件、只可追加的日志文件等)
文件类型 对于文件和目录的访问权力是根据读访问,写访问,和执行访问来定义的. 我们来看一下 ls 命令的输出结果 [root@iZ28dr6w0qvZ test]# ls -l 总用量 72 -rw- ...
- rimraf 跨平台删除文件
利用npm script 来删除文件, "scripts": { "clear": "rm -rf dist" } 但存在一个问题,remo ...
- Grunt: 拼接代码,js丑化(压缩),css压缩,html压缩,观察文件,拷贝文件,删除文件,压缩文件
准备工作 grunt 基于nodeJs所以 nodeJs需要的基础配置都需要安装 1.Grunt 安装 npm install -g grunt-cli 这是全局安装 2.在当前文件下npm init ...
- Git永久删除文件和历史记录
目录 Git永久删除文件和历史记录 使用filter-branch 添加到.gitignore文件里并push修改后的repo 清理和回收空间 Git永久删除文件和历史记录 造成你想从git存储库中永 ...
随机推荐
- 干货 |《2022B2B新增长系列之企服行业橙皮书》重磅发布
企服行业面临的宏观环境和微观环境已然发生了明显的变化.一方面,消费级互联网成为过去式,爆发式增长的时代结束.资本.媒体的目光已经悄然聚焦到以企服行业所代表的产品互联网身上,B2B企业正稳步走向C位. ...
- 字符输入流_Reader类&FileReader类介绍和字符输入读取字符数据
java.io.Reader:字符输入流的最顶层父类,其中定义了一些共性的成员方法,是一个抽象类共性的成员方法 int read() 读取单个字符并返回 int read(char[] cbuf) 将 ...
- HashSet集合存储数据的结构和HashSet集合存储元素不重复的原理
HashSet集合存储数据的结构 HashSet集合存储元素不重复的原理 //创建HashSet集合对象 Hashset<String> set = new HashSet<> ...
- Josephus问题(Ⅰ)
题目描述 n个人排成一圈,按顺时针方向依次编号1,2,3-n.从编号为1的人开始顺时针"一二"报数,报到2的人退出圈子.这样不断循环下去,圈子里的人将不断减少.最终一定会剩下一个人 ...
- CentOS删除桌面环境
公司有几台虚拟机安装的是CentOS7的桌面环境,平时也是用终端访问,于是在服务器卡住需要重启时,顺便就把桌面环境给卸载了:测试了好多方法均不成功,最终找到了可行的方式,以此记录: [root@yun ...
- 前端-关于CORS跨域的解决方案,面向服务端
最近自己在写后台管理系统的时候,并没有采用jsp.freemaker.叶子等模板技术,而是由后端提供数据api,前端通过AJAX和JQuery来动态操作页面上的一些div.table元素,从而实现报表 ...
- LeetCode题解-20.有效的括号
题目 给定一个只包括 '(',')','{','}','[',']' 的字符串 s ,判断字符串是否有效. 有效字符串需满足: 左括号必须用相同类型的右括号闭合. 左括号必须以正确的顺序闭合. 示例 ...
- Java版的防抖(debounce)和节流(throttle)
概念 防抖(debounce) 当持续触发事件时,一定时间段内没有再触发事件,事件处理函数才会执行一次,如果设定时间到来之前,又触发了事件,就重新开始延时. 防抖,即如果短时间内大量触发同一事件,都会 ...
- Dubbo源码(七) - 集群
前言 本文基于Dubbo2.6.x版本,中文注释版源码已上传github:xiaoguyu/dubbo 集群(cluster)就是一组计算机,它们作为一个总体向用户提供一组网络资源.这些单个的计算机系 ...
- 【Java面试】Java有几种文件拷贝方式,哪一种效率最高?
"Java有几种文件拷贝方式,哪一种效率最高?" 这个问题是京东一面的时候,针对4年经验的同学的一个面试题. 大家好,我是Mic,一个工作了14年的Java程序员. 关于这个问题的 ...