Git 初接触 (二) 在项目中简单使用 Git

下面我们就尝试在项目中简单的使用一下Git

  • 建立项目

    在这里为了测试我建立了一个 非常简单的文件夹program 当作我们的项目目录 里面的文件非常的简单 就 index.php 和 index.html 两个文件

    这里写图片描述

    文件的内容如下

 index.php
 ----------------
 <?php

 echo "this is a program";

 ****************************  分割线

 index.html
 ----------------
 <!DOCTYPE html>
 <html>
 <head>
     <title>Git</title>
 </head>
 <body>
 <h1>Git test</h1>
 </body>
 </html>
  • 初始化 Git

    我们进入 项目目录 右键使用 Git Bash 输入 git init 命令即可 初始化 Git 并生成 .git 目录

    这里写图片描述

    这里写图片描述


  • Git 的区域

    在Git 中 总共有三个区域 它们分别是 我们初始进入就位于工作区

  工作区(working tree)
  暂存区(stage)
  版本库(repository)


  • Git 文件的状态

    Untracked: 未跟踪 此文件在文件夹中 但并没有加入到git库 不参与版本控制 通过git add 状态变为Staged

    Unmodify: 文件已经入库 未修改 即版本库中的文件快照内容与文件夹中完全一致 这种类型的文件有两种去处 如果它被修改 而变为Modified 如果使用git rm移出版本库 则成为Untracked文件

    Modified: 文件已修改 仅仅是修改 并没有进行其他的操作 这个文件也有两个去处 通过git add可进入暂存staged状态 使用git checkout 则丢弃修改过 返回到unmodify状态 这个git checkout即从库中取出文件 覆盖当前修改

    Staged: 暂存状态 执行git commit则将修改同步到库中 这时库中的文件和本地文件又变为一致 文件为Unmodify状态 执行git reset HEAD filename取消暂存 文件状态为Modified


  • 将文件添加到版本库

    先使用 git status 检查一下我们Git的状态

On branch master

Initial commit

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

        index.html
        index.php

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

发现 index.php 和 index.html 都未被追踪 所以我们先使用 git add 命令 追踪一下 首先追踪 index.php

执行命令 git add index.php 我们发现

On branch master

Initial commit

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

        new file:   index.php

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

        index.html

index.php 正在暂存区里等待被提交 而 index.html 还处于未追踪状态

我们尝试提交一下 index.php 使用命令 git commit 提交 使用参数 -m 可以为提交增加备注

$ git commit -m 'first add'

*** Please tell me who you are.

Run

  git config --global user.email "you@example.com"
  git config --global user.name "Your Name"

to set your account's default identity.
Omit --global to set the identity only in this repository.

fatal: empty ident name (for <my@licheng▒▒.(none)>) not allowed

提交后我们发现 我们没有 设置我们的用户名和邮箱 现在我们来设置一下 如提示 使用 git config --global user.email "you@example.com" 和 git config --global user.name "Your Name"

设置完后 可以使用 git config --list 查看 如下

$ git config --list
core.symlinks=false
core.autocrlf=true
color.diff=auto
color.status=auto
color.branch=auto
color.interactive=true
pack.packsizelimit=2g
help.format=html
http.sslcainfo=C:/Program Files (x86)/Git/mingw32/ssl/certs/ca-bundle.crt
diff.astextplain.textconv=astextplain
rebase.autosquash=true
user.email=1150400220@qq.com
user.name=lychee
core.repositoryformatversion=0
core.filemode=false
core.bare=false
core.logallrefupdates=true
core.symlinks=false
core.ignorecase=true
core.hidedotfiles=dotGitOnly

下面 我们重新提交 git commit -m 'first add'

$ git commit -m 'first add'
[master (root-commit) dcfb0a8] first add
 1 file changed, 3 insertions(+)
 create mode 100644 index.php

用 git status 检查一下Git的状态

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

        index.html

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

只有 index.html 未被追踪了

使用 git log 命令查看当前分支的提交历史


$ git log
commit dcfb0a859c548128ff836ca94c6c9531ee7ff9c5
Author: lychee <1150400220@qq.com>
Date:   Mon Dec 18 23:13:13 2017 +0800

    first add

到现在 我们已经将 index.php 提交到我们的版本库里面去了

本作品采用《CC 协议》,转载必须注明作者和本文链接
《L01 基础入门》
我们将带你从零开发一个项目并部署到线上,本课程教授 Web 开发中专业、实用的技能,如 Git 工作流、Laravel Mix 前端工作流等。
《L03 构架 API 服务器》
你将学到如 RESTFul 设计风格、PostMan 的使用、OAuth 流程,JWT 概念及使用 和 API 开发相关的进阶知识。
讨论数量: 0
(= ̄ω ̄=)··· 暂无内容!

讨论应以学习和精进为目的。请勿发布不友善或者负能量的内容,与人为善,比聪明更重要!