一、Git的入门与基本使用(1)
admin
2023-07-25 22:00:06
0

 基于苏玲老师<玩转 Git 三剑客>视频学习的一点总结。——极客时间

1、版本控制系统的演变
 VCS 出现前
  用目录拷贝区别不同版本
  公共文件容易被覆盖
  成员沟通成本很高,代码集成效率低下
一、Git的入门与基本使用(1)
 集中式 VCS
  有集中的版本管理服务器器
  具备文件版本管理理和分支管理理能力
  集成效率有明显地提高
  客户端必须时刻和服务器相连
一、Git的入门与基本使用(1)
 分布式 VCS
  服务端和客户端都有完整的版本库
  脱离服务端,客户端照样可以管理理版本
  查看历史和版本比较等多数操作,都不需要访问服务器器,比集中式 VCS 更能提高版本管理理效率
一、Git的入门与基本使用(1)
 Git的特点
  最优的存储能力、非凡的性能、开源的、很容易做备份、支持离线操作、很容易定制工作流程
2、安装Git

Git官网:https://git-scm.com/
参考文档:https://git-scm.com/book/en/v2

3、最小配置
配置user.name和user.email

$ git config --global user.name ‘your_name’
$ git config --global user.email ‘your_email@domain.com’

config 的三个作用域

$ git config --local       #local只对仓库有效,缺省等同于 local
$ git config --global     #global对登录用户所有仓库有效
$ git config --system   #system对系统的所有用户有效

显示 config 的配置,加 --list

$ git config --list --local
$ git config --list --global
$ git config --list --system

清除,--unset

$ git config --unset --local user.name
$ git config --unset --global user.name
$ git config --unset --system user.name

实例:

$ git config  --global user.name "Jone"
$ git config  --global user.email "764651475@qq.com"
$ git config  --global --list
sendpack.sideband=false
user.name=Jone
user.email=764651475@qq.com

4、创建第一个仓库
两种方式:

  1. 用 Git 之前已经有项目代码
    $ git init
    $ cd 某个⽂文件夹
  2. 用 Git 之前还没有项目代码
    $ cd 项目代码所在的文件夹
    $ git init your_project      #会在当前路径下创建和项目名称同名的文件夹
    $ cd your_project

实例

$ git init git_learning
Initialized empty Git repository in D:/git_learning/.git/
$ cd git_learning/
$ git status
On branch master

No commits yet

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

$ echo "hello world!" > first.txt            #添加第一个文件

$ git status                             #显示当前git状态
On branch master
No commits yet
Untracked files:
  (use "git add ..." to include in what will be committed)
        first.txt
nothing added to commit but untracked files present (use "git add" to track)

$ git add first.txt            #添加到暂存区
warning: LF will be replaced by CRLF in first.txt.
The file will have its original line endings in your working directory.

$ git commit -m"Add first file"    #提交
[master (root-commit) c8588e4] Add first file
 1 file changed, 1 insertion(+)
 create mode 100644 first.txt

$ git status
On branch master
nothing to commit, working tree clean

5、通过几次commit来认识工作区和暂存区
 在上面添加文件到暂存区时,出现警告信息,是由于Git的换行符检查功能。Windows使用回车和换行两个字符来结束一行,而Mac和Linux只使用换行一个字符。Git可以在提交时自动地把行结束符CRLF转换成LF,而在读取代码时把LF转换成CRLF。如果该项目仅运行在Windows上的项目,可以设置false取消此功能。

$ echo "second file" > second.txt       #创建几个文件
$ echo "third file" > third.txt
$ echo "fourth file" > fourth.txt

$ git status                  #查看git当前状态
On branch master
Untracked files:
  (use "git add ..." to include in what will be committed)

        fourth.txt
        second.txt
        third.txt

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

$ git add second.txt     #添加第二个文件到暂存区
warning: LF will be replaced by CRLF in second.txt.
The file will have its original line endings in your working directory.

$ git config --global core.autocrlf false    #关闭换行符检查功能

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

        new file:   second.txt

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

        fourth.txt
        third.txt

$ git commit -m"Add second file"#提交第二个文件
[master 0bd98cb] Add second file
 1 file changed, 1 insertion(+)
 create mode 100644 second.txt

$ git log
commit 0bd98cb5d0d969cfc35d8c5a16d33b5924cbc6b0 (HEAD -> master)
Author: Jone <764651475@qq.com>
Date:   Thu Mar 14 16:59:25 2019 +0800

    Add second file

commit c8588e43dd1053684632871fb8aec1945ee6a6ab
Author: Jone <764651475@qq.com>
Date:   Thu Mar 14 16:36:00 2019 +0800

    Add first file

$ git add third.txt
$ git commit -m"Add third file"   #提交第三个文件
[master b843c28] Add third file
 1 file changed, 1 insertion(+)
 create mode 100644 third.txt

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

        fourth.txt

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

$ git add fourth.txt
$ git commit -m"Add fouth file"   #提交第四个文件
[master 1d63ec8] Add fouth file
 1 file changed, 1 insertion(+)
 create mode 100644 fourth.txt

$ echo "Update the file" >> fourth.txt   #修改第四个文件
$ git status
On branch master
Changes not staged for commit:
  (use "git add ..." to update what will be committed)
  (use "git checkout -- ..." to discard changes in working directory)

        modified:   fourth.txt

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

$ git add fourth.txt
$ git commit -m"Update fourth file"     #提交修改后的文件
[master 7376bc5] Update fourth file
 1 file changed, 1 insertion(+)

$ git log
commit 7376bc5b2ebc3e13d4c4552ebdef348a17cd4eef (HEAD -> master)
Author: Jone <764651475@qq.com>
Date:   Thu Mar 14 17:03:07 2019 +0800

    Update fourth file

commit 1d63ec82259b237f58e7525ccf856a03fb880fcd
Author: Jone <764651475@qq.com>
Date:   Thu Mar 14 17:01:46 2019 +0800

    Add fouth file

commit b843c287804d2b5886167740f9e6c0d327540ee1
Author: Jone <764651475@qq.com>
Date:   Thu Mar 14 17:00:21 2019 +0800

    Add third file

commit 0bd98cb5d0d969cfc35d8c5a16d33b5924cbc6b0
Author: Jone <764651475@qq.com>
Date:   Thu Mar 14 16:59:25 2019 +0800

    Add second file
...

通过几次提交文件,可以总结git工作区、暂存区与历史版本之间的关系:
一、Git的入门与基本使用(1)

相关内容

热门资讯

普京称考虑全面禁止柴油出口 △当地时间28日,俄罗斯总统普京主持召开国内燃油市场保障会议。当地时间6月28日,俄罗斯总统普京在主...
委内瑞拉强震遇难人数升至145... △当地时间28日凌晨,委内瑞拉拉瓜伊拉州,震后救援现场。当地时间28日,委内瑞拉全国代表大会主席豪尔...
AI抢专家饭碗?一线社工被AI... “在AI时代,专家甚至可能不如豆包。我们现在去社区里,对方都说,豆包就能解决大部分问题。你们专家来,...
第十四届中俄(黑河)林草生态建... 科技日报记者 朱虹 通讯员 温浩 6月24日,第十四届中俄(黑河)林草生态建设学术论坛在黑龙江省黑河...
如何在沈阳筛选适配多场景的防爆... 在工业生产、现代农业、节能环保等多个领域,涉及易燃易爆介质的工况下,对测控仪表的防爆性能、测量精度都...
原创 预... 在预算三千内挑选手机,大家最看重颜值与实用性的完美结合。今天为大家盘点2000-3000元好看又好用...
原创 央... #央视曝手机测评作弊乱象 #,央视财经专项调查栏目曝光手机数码测评行业全链条作弊黑幕,完整揭露当下测...
高塔变身“电波卫士” 科技赋能... 无形的空中电波,是城市平稳运行、民生通信保障、公共安全防控的重要“生命线”,深度赋能社会生产生活的方...
手机直连卫星布局加速!中国移动... 快科技6月28日消息,据《科创板日报》报道,中国移动下一颗手机直连卫星试验星03星近期将择机发射。 ...
广东一大学禁止小米汽车入校!学... 6月28日,多家媒体报道称,广州华立学院禁止小米汽车进入校园。 该校工作人员在接受采访时说,其他外来...