学会用Git——使用gitignore管理文件同步与否

使用gitignore管理文件同步与否

在官方的Git手册中找到了gitignore的相关介绍,这里是连接。文中内容介绍的非常详细,这里摘录一部分作为参考:

EXAMPLES

  • The pattern hello.* matches any file or folder whose name begins with hello. If one wants to restrict this only to the directory and not in its subdirectories, one can prepend the pattern with a slash, i.e. /hello.*; the pattern now matches hello.txt, hello.c but not a/hello.java.
  • The pattern foo/ will match a directory foo and paths underneath it, but will not match a regular file or a symbolic link foo (this is consistent with the way how pathspec works in general in Git)
  • The pattern doc/frotz and /doc/frotz have the same effect in any .gitignore file. In other words, a leading slash is not relevant if there is already a middle slash in the pattern.
  • The pattern “foo/*”, matches “foo/test.json” (a regular file), “foo/bar” (a directory), but it does not match “foo/bar/hello.c” (a regular file), as the asterisk in the pattern does not match “bar/hello.c” which has a slash in it.

以下内容为谷歌翻译:

  • 该模式hello.*匹配名称以开头的任何文件或文件夹hello。如果只想将此限制于目录而不是其子目录,则可以在模式前面加上斜杠,即/hello.*;模式现在匹配hello.txthello.c但是不 匹配a/hello.java
  • 该模式foo/将匹配目录foo及其下的路径,但不匹配常规文件或符号链接foo(这与pathspec通常在Git中的工作方式一致)
  • 模式doc/frotz/doc/frotz在任何.gitignore文件中都具有相同的效果。换句话说,如果模式中已经存在中间斜杠,那么前导斜杠就无关紧要。
  • 模式“ foo / *”匹配“ foo / test.json”(常规文件),“ foo / bar”(目录),但不匹配“ foo / bar / hello.c”(常规文件) ),因为该模式中的星号与其中带有斜杠的“ bar / hello.c”不匹配。

我的翻译

我的翻译就是我觉得比较重要的:

  • 一行为一个pattern
  • 加#为注释,建议注释不要再pattern后面,单独为一行;
  • 如果pattern前面加“!”,表明不对此目录或文件做忽略操作;
  • “/Work”为整个Work目录,包含其下子目录与文件;
  • 如果pattern中已经有了中间“/”,那么第一个“/”就无关紧要了;
  • gitignore操作只对untracked的文件有效,如果有stage的操作,需要先进行unstaged

下面针对之前的Vivado工程,有个例子,其内容为:

# Vivado Project
# 制定忽略名称为Work和Mcs的文件夹;
/Work
/Mcs

# Simulation Filse
# !表明忽略除了这个文件或文件夹以外的文件或文件夹
# !*/Work/project_1.sim/sim_1/behav/modelsim/*.do


# 保留bat文件
# !*/Work/auto_prj.bat
# !*/Work/auto_prj.tcl

其含义为忽略Work以及Mcs目录下所有内容。

删除远程仓库上传的ignore文件

创建仓库时,没有提交gitignore文件,commitpush之后,远端就存在了这些文件;

而后如果再提交包含这些目录的gitignore文件,依然对于远端来说是进行了修改;

那么就希望可以删除远端仓库中的相关文件,而保留本地的文件;

这时候不能直接使用git rm dircetory(想要删除的文件夹名称),会删除本地仓库中的文件;

可以使用如下指令删除缓冲;

git rm -r --cached -r dir/ (dir为想要删除的文件夹名称)

之后在进行commitpush就可以将远程仓库中的相关文件删除了,之后可以直接使用git add –all来添加修改的内容,这样上传的文件就会收到.gitignore约束了。

第三部分内容参考:https://www.cnblogs.com/rainbowk/p/10932322.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

ShareWow丶

前人栽树,分享知识,传播快乐。

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值