git

Posted on By ᵇᵒ

一些不太常用的git指令

# ignore 已被记录的文件
git rm --cached <file>

# 清除远端已不存在的分支
git remote prune origin

# 导出源码
git archive -o latest.zip HEAD

# unstage 已删除的文件
git checkout -- <file>
# or
git restore --staged -- <file>

# 删除远端tag https://stackoverflow.com/a/5480292/7368406
git tag -d <tagname> && git push -d origin <tagname>

# add 除 file1、file2 以外的所有文件
git add . ':!file1' ':!file2'

git commit with editor vscode

  1. 首先设置 vscode 环境变量,从而可以被命令行启动 https://code.visualstudio.com/docs/setup/mac#_launching-from-the-command-line
  2. 配置 git config
    • 全局配置 git config –global core.editor “code –wait”
    • 单个仓库 git config core.editor “code –wait”
    • 临时配置 git -c “core.editor=code -w” commit
      –global 是否全局配置;-w 也即 –wait 选项的缩写;code 即前一步骤 vscode 设置的环境变量,也可以直接用对应的路径代替。

gitkeep

git 不会追踪空文件夹,公认的一种处理方式就是在空文件夹下新建一个 .gitkeep 的空文件,曲线救国 利用追踪目录下文件的方式来追踪文件夹。

为了确保 .gitkeep 文件被追踪,我们可以在 .gitignore 文件的最后加上 !.gitkeep 。 该规则的意思是不要忽略 .gitkeep 文件,前缀感叹号类似于编程语言里的取反。

一个比较有意思的现象是,如果 .gitignore 文件里同时有 .gitkeep!.gitkeep,则以最后出现的那条为准。简单地说就是当 ignore 规则出现矛盾的时候以最后加载的规则为准,后加载的会覆盖先加载的,越是配置在文件的末尾则越晚被读取加载。

# 不忽略 .gitkeep 文件(会追踪记录 .gitkeep)
.gitkeep
!.gitkeep
# 忽略 .gitkeep 文件(不会追踪记录 .gitkeep)
!.gitkeep
.gitkeep

.gitkeep 文件一般用于多人合作的项目初始阶段,需要先将工程目录结构搭建好,其他开发人员便可以按照各自分工填补文件。如果不事先建好目录结构,而是各自开发过程中增添,则一方面容易造成冲突混乱、另一方面命名不统一规范。搭建目录结构就免不了使用 .gitkeep 完成空文件夹占坑

学习 git 命令的几种方式

  • 官方文档 https://git-scm.com/docs/git-{cmd}

    根据指令寻找对应官方文档,比如(其他命令以此类推):
    git prune: https://git-scm.com/docs/git-prune
    git archive: https://git-scm.com/docs/git-archive

  • git {cmd} –help

      git prune --help
      git archive --help
    
  • man git-{cmd}

      man git-prune
      man git-archive
    
  • info git-{cmd}

      info git-prune
      info git-archive