git速成

1.下载

通过镜像网站下载git-for-windows Mirror (taobao.org),本教程使用版本为2.17.0.安装过程使用默认选项,在选择path选项时不改变环境变量,只通过git Bash使用即可(如下图)。

image-20211020202641662
alt

安装成功后,在任意目录(如桌面)右键可以看到git Bash。

image-20211020203520025
alt

2.git简介

git是一款版本控制工具,可以帮助我们进行代码的历史记录维护以及团队协作。

2.1 git的目录结构

工作区:写代码的地方。

暂存区:将要提交的代码暂时放在这。

本地库:存储历史提交版本

image-20211020203951978
alt

2.2 代码托管中心

用于维护远程库,比如github,码云。远程库和本地库有两种交互方式。

团队类协作。

image-20211020204259268
alt

团队间合作。

image-20211020204533894
alt

3.git命令行操作

3.1 初始化仓库

git的命令与linux系统的命令兼容,我们创建工作目录gitworkspace,从工作目录下右键git bash,即可使用linuxgit命令。

在工作目录新建gitvideo\wechat,在该目录下执行gitinstall.可以看到生成了.git目录。

image-20211020210048080
alt

3.2 设置签名

image-20211020210627044
alt

签名命令如下。--global参数设置的签名为系统级别签名。

git config [--global] user.name wz
git config [--global] user.email wz@qq.com

看下配置。

$ cat .git/config
[core]
        repositoryformatversion = 0
        filemode = false
        bare = false
        logallrefupdates = true
        symlinks = false
        ignorecase = true
[user]
        name = wz
        email = wz@qq.com

如果配置了--global选项则在~目录下查看。

3.3 添加提交
$ git status
On branch master
No commits yet
nothing to commit (create/copy files and use "git add" to track)

可以看到每页提交过的内容,说明本地库是空的,也没有需要被提交的内容,说明暂存库是空的。

来一次提交吧。


24724@LAPTOP-OCSC7S98 MINGW64 /e/gitworkspace/gitviedo/wechat (master)
$ vim hello,world.txt

24724@LAPTOP-OCSC7S98 MINGW64 /e/gitworkspace/gitviedo/wechat (master)
$ git add hello,world.txt
warning: LF will be replaced by CRLF in hello,world.txt.
The file will have its original line endings in your working directory.

24724@LAPTOP-OCSC7S98 MINGW64 /e/gitworkspace/gitviedo/wechat (master)
$ git status
On branch master

No commits yet

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

        new file:   hello,world.txt


24724@LAPTOP-OCSC7S98 MINGW64 /e/gitworkspace/gitviedo/wechat (master)
$ git commit -m "hello,world!"
[master (root-commit) 8262451] hello,world!
 1 file changed, 1 insertion(+)
 create mode 100644 hello,world.txt