Files
GitGuide/docs/git/base/提交新版本.md
2021-03-18 17:06:57 +08:00

104 lines
3.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.
# 提交新版本
在本地创建`git`仓库后,就可以对文件进行版本控制.文件分为`2`种状态:已追踪(`tracked`)和未追踪(`untracked`)
* 未追踪(`untracked`)文件就是未添加到版本控制的文件
* 已追踪的文件可分为已修改(`modified`),已暂存(`staged`)和未修改(`unmodified`)文件
基本的`git`工作流程如下:
1. 在工作目录下添加文件,修改文件或删除文件
2. 保存文件状态到暂存区域
3. 提交修改记录到仓库
![](https://git-scm.com/book/en/v2/images/lifecycle.png)
## 查看文件状态
使用命令`git status`查看文件状态,同时会给出相应文件状态下的命令
$ git status
On branch master
Your branch is up-to-date with 'origin/master'.
nothing to commit, working directory clean
$ git status
On branch master
Your branch is up-to-date with 'origin/master'.
Changes not staged for commit:
(use "git add <file>..." to update what will be committed)
(use "git checkout -- <file>..." to discard changes in working directory)
modified: docs/source/index.rst
Untracked files:
(use "git add <file>..." to include in what will be committed)
.vscode/
docs/source/git/
no changes added to commit (use "git add" and/or "git commit -a")
仅需查看文件状态
$ git status -s
A .vscode/settings.json
M docs/source/index.rst
?? docs/source/git/
符号`A`表示已暂存,符号`M`表示已修改,符号`??`表示未追踪
其中已修改文件有如下`3`种状态
$ git status -s
M README
MM Rakefile
M lib/simplegit.rb
* 符号`_M`表示已修改还未加入暂存区域
* 符号`MM`表示已加入暂存区域的已修改文件又被修改了
* 符号`M_`表示添加到暂存区域的已修改文件
## 添加修改文件
使用命令`git add`将未追踪文件或已修改文件添加到暂存区域
git add name1 name2 ...
# 添加所有文件
git add *
### 忽略文件
编辑`.gitignore`文件,将整个工程文件中不想添加版本控制的文件加入进去
所有空行或者以 开头的行都会被 Git 忽略。
可以使用标准的 glob 模式匹配。
匹配模式可以以(/)开头防止递归。
匹配模式可以以(/)结尾指定目录。
要忽略指定模式以外的文件或目录,可以在模式前加上惊叹号(!)取反。
可参考[github/gitignore](https://github.com/github/gitignore),上面包含了很多不同开发环境下的.gitignore文件
如果`git add`添加了忽略文件会报错,可以使用`-f`强制添加
git add -f name1 name2 ...
## 提交暂存文件
git commit -m '提交信息'
或者`git commit`然后回车,会打开设置的编辑器进行操作
## 撤销暂存文件
对添加到暂存区域的文件使用命令
git reset HEAD <file>...
## 撤销已修改文件
对已修改文件使用命令
git checkout -- <file>...