1
0
forked from iicd/git-learner
git-learner/notes/2.7-git_basic_op.md
2024-08-19 09:38:33 +02:00

97 lines
2.0 KiB
Markdown
Raw Permalink Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

## 2.7 Git Basic Ops
### 2.7.1 git tag
给当前的进度打个标签
```bash
git tag -m "some message" <version-string>
git describe # 查看标签
```
### 2.7.2 Git 删除文件
git
```bash
rm *.txt
git ls-files #本地删除不是真的删除,暂存库中还在,也就是删除的这个命令没有被添加到暂存库中
```
```bash
git rm welcome.txt hack-2.txt #将文本文件从git中删除
git status
git ls-files --with-tree=HEAD^ # 父节点中的文件还在
```
```bash
git add -u #将版本库中的本地文件的变更记录到暂存区中
```
### 2.7.3 恢复删除的文件
```bash
git cat-file -p HEAD~1:filename > filename
```
### 2.7.4 移动文件
```bash
git mv welcome.txt README # git提供的git mv来移动文件
git commit -m "rename test" # 在输出的结果中可以查看改名前后两个文件的相似度
```
### 2.7.5 显示版本号
```bash
git describe # 展示版本号
```
```bash
git log --oneline --decorate -4 #在提交日志中显示提交对应的Tag
```
### 2.7.6 git add -i
```bash
git add -i #进入一个交互式的提交脚本
```
### 2.7.8 Git忽略
就是编辑.gitignore文件
可以设置全局忽略的文件位置
```bash
git config --global core.excludesfile /path/to/file
git config core.excludesfile
```
小trick,可以用cat快速写入一个文件
```bash
cat > .gitignore << EOF
hello
*.o
*.h
EOF
```
忽略语法
```text
# 这是注释行 —— 被忽略
*.a # 忽略所有以 .a 为扩展名的文件。
!lib.a # 但是 lib.a 文件或者目录不要忽略,即使前面设置了对 *.a 的忽略。
/TODO # 只忽略根目录下的 TODO 文件,子目录的 TODO 文件不忽略。
build/ # 忽略所有 build/ 目录下的文件。
doc/*.txt # 忽略文件如 doc/notes.txt但是文件如 doc/server/arch.txt 不被忽略。
```
### 2.7.9 Git Archive
```bash
git archive -o latest.zip HEAD # 根据最新的提交创建zip
git archive -o latest.zip HEAD src doc # 只将目录src doc放入zip中
git archive --format=tar --prefix=1.0/ v1.0 | gzip > foo-1.0.tar.gz
```