1. 查找某个关键字(比如函数名):
$ git grep xmmap config.c: contents = xmmap(NULL, contents_sz, PROT_READ, diff.c: s->data = xmmap(NULL, s->size, PROT_READ, MAP_PRIVATE, fd, 0); git-compat-util.h: extern void *xmmap(void *start, size_t length, int prot, int fla read-cache.c: mmap = xmmap(NULL, mmap_size, PROT_READ | PROT_WRITE, MAP_PRIVATE, refs.c: log_mapped = xmmap(NULL, mapsz, PROT_READ, MAP_PRIVATE, logfd, 0); sha1_file.c: map = xmmap(NULL, mapsz, PROT_READ, MAP_PRIVATE, fd, 0); sha1_file.c: idx_map = xmmap(NULL, idx_size, PROT_READ, MAP_PRIVATE, fd, 0); sha1_file.c: win->base = xmmap(NULL, win->len, sha1_file.c: map = xmmap(NULL, *size, PROT_READ, MAP_PRIVATE, f sha1_file.c: buf = xmmap(NULL, size, PROT_READ, MAP_PRIVATE, fd, 0); wrapper.c: void *xmmap(void *start, size_t length,
1.1 如果要显示行号,添加‘-n’选项:
$ git grep -n xmmap config.c:1016: contents = xmmap(NULL, contents_sz, PROT_READ, diff.c:1833: s->data = xmmap(NULL, s->size, PROT_READ, MAP_PRIVATE, fd, git-compat-util.h:291: extern void *xmmap(void *start, size_t length, int prot, int read-cache.c:1178: mmap = xmmap(NULL, mmap_size, PROT_READ | PROT_WRITE, MAP_ refs.c:1345: log_mapped = xmmap(NULL, mapsz, PROT_READ, MAP_PRIVATE, logfd, 0); sha1_file.c:377: map = xmmap(NULL, mapsz, PROT_READ, MAP_PRIVATE, fd, 0); sha1_file.c:479: idx_map = xmmap(NULL, idx_size, PROT_READ, MAP_PRIVATE, fd sha1_file.c:780: win->base = xmmap(NULL, win->len, sha1_file.c:1076: map = xmmap(NULL, *size, PROT_READ, MAP_PR sha1_file.c:2393: buf = xmmap(NULL, size, PROT_READ, MAP_PRIVATE, fd wrapper.c:89: void *xmmap(void *start, size_t length,
1.2 如果只显示文件名,添加‘--name-only’选项:
$ git grep --name-only xmmap config.c diff.c git-compat-util.h read-cache.c refs.c sha1_file.c wrapper.c
1.3 统计每个文件里有多少行匹配内容,添加‘-c’选项:
$ git grep -c xmmap config.c:1 diff.c:1 git-compat-util.h:1 read-cache.c:1 refs.c:1 sha1_file.c:5 wrapper.c:1
1.4 查找git仓库里某个特定版本里的内容,添加tag名或sha号:
$ git grep xmmap v1.5.0 v1.5.0:config.c: contents = xmmap(NULL, st.st_size, PROT_READ, v1.5.0:diff.c: s->data = xmmap(NULL, s->size, PROT_READ, MAP_PRIVATE, fd, v1.5.0:git-compat-util.h:static inline void *xmmap(void *start, size_t length, v1.5.0:read-cache.c: cache_mmap = xmmap(NULL, cache_mmap_size, v1.5.0:refs.c: log_mapped = xmmap(NULL, st.st_size, PROT_READ, MAP_PRIVATE, logfd v1.5.0:sha1_file.c: map = xmmap(NULL, st.st_size, PROT_READ, MAP_PRIVATE, fd, v1.5.0:sha1_file.c: idx_map = xmmap(NULL, idx_size, PROT_READ, MAP_PRIVATE, fd v1.5.0:sha1_file.c: win->base = xmmap(NULL, win->len, v1.5.0:sha1_file.c: map = xmmap(NULL, st.st_size, PROT_READ, MAP_PRIVATE, fd, v1.5.0:sha1_file.c: buf = xmmap(NULL, size, PROT_READ, MAP_PRIVATE, fd
1.5 查找git仓库里某个特定版本、特定文件里的内容(省略版本,默认当前版本),添加tag名或sha号、文件名:
$ git grep xmmap v1.5.0 diff.c v1.5.0:diff.c: s->data = xmmap(NULL, s->size, PROT_READ, MAP_PRIVATE, fd,
2. 某些关键字的逻辑组合
2.1 与逻辑:某一行既包括...又包括...
------------------->$ git grep -e 'pu' --and -e 'lic' a.txt:public c.txt:public c.txt:public
2.2 或逻辑:某一行包括...或者包括...
------------------->$ git grep -e 'public' --or -e 'void' a.txt:public a.txt:void b.txt:void c.txt:public c.txt:public
--or 是默认行为,如果省略:
------------------->$ git grep -e 'public' --or -e 'void' a.txt:public a.txt:void b.txt:void c.txt:public c.txt:public ------------------->$ git grep -e 'public' -e 'void' a.txt:public a.txt:void b.txt:void c.txt:public c.txt:public
2.3 非逻辑:某一行不包括...
------------------->$ git grep --not -e 'public' a.txt:ab a.txt:bc a.txt:void b.txt:void b.txt:aaa b.txt:bbb c.txt:kkkk c.txt:sfs c.txt:l
2.4 组合逻辑:与或非的组合(为测试组合逻辑,又在库中新添加了d.txt文件)
------------------->$ git grep -e 'public' --and ( -e 'void' -e 'return' ) d.txt:public void int save d.txt:public void string save d.txt:public return a ------------------->$ git grep -e 'public' --and ( -e 'void' --or -e 'return' ) d.txt:public void int save d.txt:public void string save d.txt:public return a ------------------->$ git grep -e 'public' --and ( -e 'void' --and -e 'int' ) d.txt:public void int save ------------------->$ git grep -e 'public' --and ( --not -e 'int' ) a.txt:public c.txt:public c.txt:public d.txt:public void string save d.txt:public return a
2.5 全匹配:某一文件既包含...又包含...
------------------->$ git grep --all-match -e 'public' -e 'void' a.txt:public a.txt:void d.txt:public void int save d.txt:public void string save d.txt:public return a
2.6 以上各个命令还可以加‘-n'、’-c‘、’--name-only‘、版本、文件等限制
2.7 查找某个字符串在何时添加
git log -S"string wanted to search" --oneline 81fbf7e a commit