git-learn(1)

2019-04-30  本文已影响0人  琉璃_xin

原作者文章地址:https://git-scm.com/book/zh/v2

git三种状态:

  1. 已提交(committed)
    数据已经安全的保存在本地数据库中;如果 Git 目录中保存着特定版本的文件,就属于已提交状态
  2. 已修改(modified)
    修改了文件,但还没保存到数据库中;如果自上次取出后,作了修改但还没有放到暂存区域,就是已修改状态
  3. 已暂存(staged)
    对一个已修改文件的当前版本做了标记,使之包含在下次提交的快照中; 如果作了修改并已放入暂存区域,就属于已暂存状态

工作目录、暂存区域以及 Git 仓库

引入3个工作区域的概念:git仓库,工作目录,暂存区域 3个工作区域
  1. git仓库
    Git 用来保存项目的元数据和对象数据库的地方,其他计算机git clone 的就是这里的数据
  2. 工作目录
    对项目的某个版本独立提取出来的内容。 这些从 Git 仓库的压缩数据库中提取出来的文件,放在磁盘上供你使用或修改
  3. 暂存区域
    一个保存了将提交的文件列表信息的文件。一般存放在git仓库目录

基本的工作流程:

  1. 工作目录修改文件
  2. 暂存文件,将文件的快照放入暂存区域
  3. 提交更新,找到暂存区域的文件,将快照永久存储到git仓库目录

git仓库创建

  1. git init
$ git init
Initialized empty Git repository in E:/WORK_NEW/vue2.x/gittest/.git/

当前目录下多了一个.git的目录,这个目录是Git来跟踪管理版本库的

  1. git clone [url]
git clone https://github.com/dongwudi/vue-study.git [自定义本地仓库名]

检查当前文件状态

使用git status命令检查文件状态

$ git status
On branch master

No commits yet

nothing to commit (create/copy files and use "git add" to track)

创建文件readme.md,写入内容git test;
vim: i插入模式, ESC键跳到命令模式,:q(不保存)或者:wq(保存)

$ touch readme.md
$ vim readme.md

此时再次检查当前文件状态:

$ git status
On branch master

No commits yet

Untracked files:
  (use "git add <file>..." to include in what will be committed)

        readme.md

nothing added to commit but untracked files present (use "git add" to track)

从提示中看出新的未跟踪文件。

添加新文件到仓库,跟踪

$ git add readme.md
warning: LF will be replaced by CRLF in readme.md.
The file will have its original line endings in your working directory

换行符的问题警告,git 的 Windows 客户端基本都会默认设置 core.autocrlf=true

$ git config --global core.autocrlf false

再次提交没有报错就说明已经添加成功,可以查看文件状态:

$ git status
On branch master

No commits yet

Changes to be committed:
  (use "git rm --cached <file>..." to unstage)

        new file:   readme.md

只要在 Changes to be committed 这行下面的,就说明是已暂存状态。 如果此时提交,那么该文件此时此刻的版本将被留存在历史记录中

如果此时修改已跟踪文件:

$ git status
On branch master

No commits yet

Changes to be committed:
  (use "git rm --cached <file>..." to unstage)

        new file:   readme.md

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:   readme.md

git只是暂存了运行git add 命令时的版本,修订后的文件需要再次git add把最新版本暂存起来;可以使用git add .将目录下非忽略文件全部进行提交跟踪

状态简览

git status -s | --short

$ git status -s
A  a.txt
AM b.txt
A  readme.md
?? add.txt
  1. 新添加的未跟踪文件前面有 ?? 标记,
  2. 新添加到暂存区中的文件前面有 A 标记,
  3. 修改过的文件前面有 M 标记

忽略文件

可以创建一个名为 .gitignore 的文件,列出要忽略的文件模式
.gitignore的格式规范

#忽略ign.txt
/ign.txt
ignore
$ git status -s
AM a.txt
AM b.txt
A  readme.md
?? .gitignore
?? add.txt

查看已暂存和未暂存文件的不同

git diff比较的是工作目录中当前文件和暂存区域快照之间的差异, 也就是修改之后还没有暂存起来的变化内容

$ git diff
diff --git a/a.txt b/a.txt
index bfb8f68..5133de9 100644
--- a/a.txt
+++ b/a.txt
@@ -1 +1 @@
-this is a mod
+this is a mod?
diff --git a/b.txt b/b.txt
index 5be7e15..5b1017d 100644
--- a/b.txt
+++ b/b.txt
@@ -1 +1 @@
-this is b
+this is b modefied

提交更新

$ git commit -m 'first commit'
[master (root-commit) c70394a] first commit
 5 files changed, 6 insertions(+)
 create mode 100644 .gitignore
 create mode 100644 a.txt
 create mode 100644 add.txt
 create mode 100644 b.txt
 create mode 100644 readme.md

提交后它会告诉你,当前是在哪个分支(master)提交的,本次提交的完整 SHA-1 校验和是什么(c70394a),以及在本次提交中,有多少文件修订过,多少行添加和删改过。

移除文件

$ git rm add.txt
rm 'add.txt'

$ git status
On branch master
Changes to be committed:
  (use "git reset HEAD <file>..." to unstage)

        deleted:    add.txt

下一次提交时,该文件就不再纳入版本管理了。如果删除之前修改过,并且放入暂存区的文件,需要添加 -f 强制选项

$ git rm a.txt
error: the following file has changes staged in the index:
    a.txt
(use --cached to keep the file, or -f to force removal)

$ git rm a.txt -f
rm 'a.txt'

提交历史查看

默认不用任何参数的话,git log 会按提交时间列出所有的更新,最近的更新排在最上面;
git log [<options>] [<since>..<until>] [[--] <path>...]

$ git log
commit 85365222e37207ceffe1e75be725bbe8f3f193c9 (HEAD -> master)
Author: dongwudi <550293874@qq.com>
Date:   Tue Apr 30 16:38:19 2019 +0800

    rm txt

commit c70394a33401ecc6459c6c26c512b0e6245d22b0
Author: dongwudi <550293874@qq.com>
Date:   Tue Apr 30 16:26:54 2019 +0800

    first commit

常用显示参数:

常用筛选参数:

  1. -n:显示前n条log
  2. --after= 比如 git log --after="2014-7-1”
  3. --before=
  4. --author=

撤销操作

  1. 取消暂存的文件
    git reset HEAD <file>...
$ git status
On branch 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:   b.txt
        modified:   readme.md

no changes added to commit (use "git add" and/or "git commit -a")

$ git add .

$ git status
On branch master
Changes to be committed:
  (use "git reset HEAD <file>..." to unstage)

        modified:   b.txt
        modified:   readme.md


$ git reset HEAD b.txt
Unstaged changes after reset:
M       b.txt

$ git status
On branch master
Changes to be committed:
  (use "git reset HEAD <file>..." to unstage)

        modified:   readme.md

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:   b.txt
  1. 取消对文件的修改
    git checkout -- <file>...
$ git status
On branch master
Changes to be committed:
  (use "git reset HEAD <file>..." to unstage)

        modified:   readme.md

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:   b.txt

$ git checkout -- b.txt

$ git status
On branch master
Changes to be committed:
  (use "git reset HEAD <file>..." to unstage)

        modified:   readme.md
上一篇 下一篇

猜你喜欢

热点阅读