Skip to content

提交新版本

在本地创建git仓库后,就可以对文件进行版本控制.文件分为2种状态:已追踪(tracked)和未追踪(untracked)

  • 未追踪(untracked)文件就是未添加到版本控制的文件

  • 已追踪的文件可分为已修改(modified),已暂存(staged)和未修改(unmodified)文件

基本的git工作流程如下:

  1. 在工作目录下添加文件,修改文件或删除文件
  2. 保存文件状态到暂存区域
  3. 提交修改记录到仓库

查看文件状态

使用命令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,上面包含了很多不同开发环境下的.gitignore文件

如果git add添加了忽略文件会报错,可以使用-f强制添加

git add -f name1 name2 ...

提交暂存文件

git commit -m '提交信息'

或者git commit然后回车,会打开设置的编辑器进行操作

撤销暂存文件

对添加到暂存区域的文件使用命令

git reset HEAD <file>...

撤销已修改文件

对已修改文件使用命令

git checkout -- <file>...