zhangli-s 发表于 2018-9-17 13:34:36

git实战(3)--提交到本地仓库

  上一节就提到了仓库的概念,其实初始化git后,git将本地空间分成两部分,一部分是工作区(Working Directory),studygit目录就是工作区存放我们自己的文件,另外一个就是版本库(Repository)也称为仓库,在工作区目录下有一个.git的隐藏目录,该目录不属于工作区,就是仓库。
houenxun@studygit$ ls -al  total 0
  drwxr-xr-x   3 houenxunstaff   10279 19:18 .
  drwxr-xr-x+ 52 houenxunstaff176879 19:18 ..
  drwxr-xr-x10 houenxunstaff   34079 19:20 .git
  通过git status 我们可以实时查看当前仓库的状态
houenxun@studygit$ git status  On branch master
  Initial commit
  nothing to commit (create/copy files and use "git add" to track)
  当前处于master分支,并且没有任何东西需要提交,这是我们通过touch命令在工作区中创建一个文件,并再次查看仓库信息
houenxun@studygit$ touch readme.txt  houenxun@studygit$ git status
  On branch master
  Initial commit
  Untracked files:
  (use "git add ..." to include in what will be committed)
  readme.txt
  nothing added to commit but untracked files present (use "git add" to track)
  Untracked file 表示readme.txt是新增的文件,在仓库中没有版本信息。下面我们将readme.tex提交到本地仓库中。
houenxun@studygit$ git add readme.txt  houenxun@studygit$ git commit -m "commit readme.txt"
   commit readme.txt
  1 file changed, 0 insertions(+), 0 deletions(-)
  create mode 100644 readme.txt
  houenxun@studygit$
  这里可能有人会问题,提交本地仓库,为什么先进行add操作然后再进行commit操作?
  其实git的仓库又进一步划分出了一块暂存区(stage),通过add命令只是将文件提交到了stage中,类似保存草稿。
  现在查看一下git的状态
houenxun@studygit$ git status  On branch master
  nothing to commit, working directory clean
  当前没有任何文件需要提交。如果想查看提交记录使用git log
houenxun@studygit$ git log  commit 32a7706df9f266522bdb223e8f4308cfccec01ba
  Author: houenxun
  Date:   Thu Jul 9 20:08:48 2015 +0800
  commit readme.txt
  其中32a7706df9f266522bdb223e8f4308cfccec01ba表示提交操作的唯一id
houenxun@studygit$ touch hello test test2  houenxun@studygit$ ls
  helloreadme.txttesttest2
  houenxun@studygit$ git add *
  houenxun@studygit$ git status
  On branch master
  Changes to be committed:
  (use "git reset HEAD ..." to unstage)
  new file:   hello
  new file:   test
  new file:   test2
  下面我们一次创建多个文件,通过git add添加到暂存区中后看一下当前仓库的状态!另外需要说明的是git add 可以一次添加多个文件,即可以通过git file1 file2的形式,也可以通过通配符匹配多个文件!
houenxun@studygit$ gitcommit -m "hello test test2"   hello test test2
  3 files changed, 0 insertions(+), 0 deletions(-)
  create mode 100644 hello
  create mode 100644 test
  create mode 100644 test2
  houenxun@studygit$ git log
  commit 75014c60c33dca4873f80a80d3892cfef7b59340
  Author: houenxun
  Date:   Thu Jul 9 20:41:33 2015 +0800
  hello test test2
  commit 32a7706df9f266522bdb223e8f4308cfccec01ba
  Author: houenxun
  Date:   Thu Jul 9 20:08:48 2015 +0800
  commit readme.txt
  houenxun@studygit$
  再次通过git commit 命令提交,查看提交记录,是不是发现有了两个版本!
  这里还有一个小功能,git log file 可以查看单个文件的提交记录
houenxun@studygit$ git log readme.txt  commit 32a7706df9f266522bdb223e8f4308cfccec01ba
  Author: houenxun
  Date:   Thu Jul 9 20:08:48 2015 +0800
  commit readme.txt
  houenxun@studygit$
  好了有关本地提交的操作就写到这里,但上面只是做了版本记录的作用,其实并未真正展示版本控制系统的真正优势,下节继续!


页: [1]
查看完整版本: git实战(3)--提交到本地仓库