Git初级(一)

    科技2022-07-12  146

    1.分布式版本控制系统

    01版本02版本03版本,各个版本之间可以复原之前的版本。每个版本记录之前版本的索引,空间占用很小。

    2.Git历史

    终止了与Bitkeeper项目管理工具合作后,林纳斯-托瓦兹一周时间用C写出了Git,开源免费。

    3.安装Git并使用

    网络上安装教程资源很多,安装完成后,桌面右键选择Git Bash Here 就打开了Linux终端命令框。

    4.Git本地结构

    本地结构不涉及远程的东西,包含三个区域 工作区 :写代码的地方,各种文件 暂存区 :打算提交的代码,不想提交了可以撤回 本地库 :每一个历史版本的信息

    #mermaid-svg-YBSc8Df8NGQULWbq .label{font-family:'trebuchet ms', verdana, arial;font-family:var(--mermaid-font-family);fill:#333;color:#333}#mermaid-svg-YBSc8Df8NGQULWbq .label text{fill:#333}#mermaid-svg-YBSc8Df8NGQULWbq .node rect,#mermaid-svg-YBSc8Df8NGQULWbq .node circle,#mermaid-svg-YBSc8Df8NGQULWbq .node ellipse,#mermaid-svg-YBSc8Df8NGQULWbq .node polygon,#mermaid-svg-YBSc8Df8NGQULWbq .node path{fill:#ECECFF;stroke:#9370db;stroke-width:1px}#mermaid-svg-YBSc8Df8NGQULWbq .node .label{text-align:center;fill:#333}#mermaid-svg-YBSc8Df8NGQULWbq .node.clickable{cursor:pointer}#mermaid-svg-YBSc8Df8NGQULWbq .arrowheadPath{fill:#333}#mermaid-svg-YBSc8Df8NGQULWbq .edgePath .path{stroke:#333;stroke-width:1.5px}#mermaid-svg-YBSc8Df8NGQULWbq .flowchart-link{stroke:#333;fill:none}#mermaid-svg-YBSc8Df8NGQULWbq .edgeLabel{background-color:#e8e8e8;text-align:center}#mermaid-svg-YBSc8Df8NGQULWbq .edgeLabel rect{opacity:0.9}#mermaid-svg-YBSc8Df8NGQULWbq .edgeLabel span{color:#333}#mermaid-svg-YBSc8Df8NGQULWbq .cluster rect{fill:#ffffde;stroke:#aa3;stroke-width:1px}#mermaid-svg-YBSc8Df8NGQULWbq .cluster text{fill:#333}#mermaid-svg-YBSc8Df8NGQULWbq div.mermaidTooltip{position:absolute;text-align:center;max-width:200px;padding:2px;font-family:'trebuchet ms', verdana, arial;font-family:var(--mermaid-font-family);font-size:12px;background:#ffffde;border:1px solid #aa3;border-radius:2px;pointer-events:none;z-index:100}#mermaid-svg-YBSc8Df8NGQULWbq .actor{stroke:#ccf;fill:#ECECFF}#mermaid-svg-YBSc8Df8NGQULWbq text.actor>tspan{fill:#000;stroke:none}#mermaid-svg-YBSc8Df8NGQULWbq .actor-line{stroke:grey}#mermaid-svg-YBSc8Df8NGQULWbq .messageLine0{stroke-width:1.5;stroke-dasharray:none;stroke:#333}#mermaid-svg-YBSc8Df8NGQULWbq .messageLine1{stroke-width:1.5;stroke-dasharray:2, 2;stroke:#333}#mermaid-svg-YBSc8Df8NGQULWbq #arrowhead path{fill:#333;stroke:#333}#mermaid-svg-YBSc8Df8NGQULWbq .sequenceNumber{fill:#fff}#mermaid-svg-YBSc8Df8NGQULWbq #sequencenumber{fill:#333}#mermaid-svg-YBSc8Df8NGQULWbq #crosshead path{fill:#333;stroke:#333}#mermaid-svg-YBSc8Df8NGQULWbq .messageText{fill:#333;stroke:#333}#mermaid-svg-YBSc8Df8NGQULWbq .labelBox{stroke:#ccf;fill:#ECECFF}#mermaid-svg-YBSc8Df8NGQULWbq .labelText,#mermaid-svg-YBSc8Df8NGQULWbq .labelText>tspan{fill:#000;stroke:none}#mermaid-svg-YBSc8Df8NGQULWbq .loopText,#mermaid-svg-YBSc8Df8NGQULWbq .loopText>tspan{fill:#000;stroke:none}#mermaid-svg-YBSc8Df8NGQULWbq .loopLine{stroke-width:2px;stroke-dasharray:2, 2;stroke:#ccf;fill:#ccf}#mermaid-svg-YBSc8Df8NGQULWbq .note{stroke:#aa3;fill:#fff5ad}#mermaid-svg-YBSc8Df8NGQULWbq .noteText,#mermaid-svg-YBSc8Df8NGQULWbq .noteText>tspan{fill:#000;stroke:none}#mermaid-svg-YBSc8Df8NGQULWbq .activation0{fill:#f4f4f4;stroke:#666}#mermaid-svg-YBSc8Df8NGQULWbq .activation1{fill:#f4f4f4;stroke:#666}#mermaid-svg-YBSc8Df8NGQULWbq .activation2{fill:#f4f4f4;stroke:#666}#mermaid-svg-YBSc8Df8NGQULWbq .mermaid-main-font{font-family:"trebuchet ms", verdana, arial;font-family:var(--mermaid-font-family)}#mermaid-svg-YBSc8Df8NGQULWbq .section{stroke:none;opacity:0.2}#mermaid-svg-YBSc8Df8NGQULWbq .section0{fill:rgba(102,102,255,0.49)}#mermaid-svg-YBSc8Df8NGQULWbq .section2{fill:#fff400}#mermaid-svg-YBSc8Df8NGQULWbq .section1,#mermaid-svg-YBSc8Df8NGQULWbq .section3{fill:#fff;opacity:0.2}#mermaid-svg-YBSc8Df8NGQULWbq .sectionTitle0{fill:#333}#mermaid-svg-YBSc8Df8NGQULWbq .sectionTitle1{fill:#333}#mermaid-svg-YBSc8Df8NGQULWbq .sectionTitle2{fill:#333}#mermaid-svg-YBSc8Df8NGQULWbq .sectionTitle3{fill:#333}#mermaid-svg-YBSc8Df8NGQULWbq .sectionTitle{text-anchor:start;font-size:11px;text-height:14px;font-family:'trebuchet ms', verdana, arial;font-family:var(--mermaid-font-family)}#mermaid-svg-YBSc8Df8NGQULWbq .grid .tick{stroke:#d3d3d3;opacity:0.8;shape-rendering:crispEdges}#mermaid-svg-YBSc8Df8NGQULWbq .grid .tick text{font-family:'trebuchet ms', verdana, arial;font-family:var(--mermaid-font-family)}#mermaid-svg-YBSc8Df8NGQULWbq .grid path{stroke-width:0}#mermaid-svg-YBSc8Df8NGQULWbq .today{fill:none;stroke:red;stroke-width:2px}#mermaid-svg-YBSc8Df8NGQULWbq .task{stroke-width:2}#mermaid-svg-YBSc8Df8NGQULWbq .taskText{text-anchor:middle;font-family:'trebuchet ms', verdana, arial;font-family:var(--mermaid-font-family)}#mermaid-svg-YBSc8Df8NGQULWbq .taskText:not([font-size]){font-size:11px}#mermaid-svg-YBSc8Df8NGQULWbq .taskTextOutsideRight{fill:#000;text-anchor:start;font-size:11px;font-family:'trebuchet ms', verdana, arial;font-family:var(--mermaid-font-family)}#mermaid-svg-YBSc8Df8NGQULWbq .taskTextOutsideLeft{fill:#000;text-anchor:end;font-size:11px}#mermaid-svg-YBSc8Df8NGQULWbq .task.clickable{cursor:pointer}#mermaid-svg-YBSc8Df8NGQULWbq .taskText.clickable{cursor:pointer;fill:#003163 !important;font-weight:bold}#mermaid-svg-YBSc8Df8NGQULWbq .taskTextOutsideLeft.clickable{cursor:pointer;fill:#003163 !important;font-weight:bold}#mermaid-svg-YBSc8Df8NGQULWbq .taskTextOutsideRight.clickable{cursor:pointer;fill:#003163 !important;font-weight:bold}#mermaid-svg-YBSc8Df8NGQULWbq .taskText0,#mermaid-svg-YBSc8Df8NGQULWbq .taskText1,#mermaid-svg-YBSc8Df8NGQULWbq .taskText2,#mermaid-svg-YBSc8Df8NGQULWbq .taskText3{fill:#fff}#mermaid-svg-YBSc8Df8NGQULWbq .task0,#mermaid-svg-YBSc8Df8NGQULWbq .task1,#mermaid-svg-YBSc8Df8NGQULWbq .task2,#mermaid-svg-YBSc8Df8NGQULWbq .task3{fill:#8a90dd;stroke:#534fbc}#mermaid-svg-YBSc8Df8NGQULWbq .taskTextOutside0,#mermaid-svg-YBSc8Df8NGQULWbq .taskTextOutside2{fill:#000}#mermaid-svg-YBSc8Df8NGQULWbq .taskTextOutside1,#mermaid-svg-YBSc8Df8NGQULWbq .taskTextOutside3{fill:#000}#mermaid-svg-YBSc8Df8NGQULWbq .active0,#mermaid-svg-YBSc8Df8NGQULWbq .active1,#mermaid-svg-YBSc8Df8NGQULWbq .active2,#mermaid-svg-YBSc8Df8NGQULWbq .active3{fill:#bfc7ff;stroke:#534fbc}#mermaid-svg-YBSc8Df8NGQULWbq .activeText0,#mermaid-svg-YBSc8Df8NGQULWbq .activeText1,#mermaid-svg-YBSc8Df8NGQULWbq .activeText2,#mermaid-svg-YBSc8Df8NGQULWbq .activeText3{fill:#000 !important}#mermaid-svg-YBSc8Df8NGQULWbq .done0,#mermaid-svg-YBSc8Df8NGQULWbq .done1,#mermaid-svg-YBSc8Df8NGQULWbq .done2,#mermaid-svg-YBSc8Df8NGQULWbq .done3{stroke:grey;fill:#d3d3d3;stroke-width:2}#mermaid-svg-YBSc8Df8NGQULWbq .doneText0,#mermaid-svg-YBSc8Df8NGQULWbq .doneText1,#mermaid-svg-YBSc8Df8NGQULWbq .doneText2,#mermaid-svg-YBSc8Df8NGQULWbq .doneText3{fill:#000 !important}#mermaid-svg-YBSc8Df8NGQULWbq .crit0,#mermaid-svg-YBSc8Df8NGQULWbq .crit1,#mermaid-svg-YBSc8Df8NGQULWbq .crit2,#mermaid-svg-YBSc8Df8NGQULWbq .crit3{stroke:#f88;fill:red;stroke-width:2}#mermaid-svg-YBSc8Df8NGQULWbq .activeCrit0,#mermaid-svg-YBSc8Df8NGQULWbq .activeCrit1,#mermaid-svg-YBSc8Df8NGQULWbq .activeCrit2,#mermaid-svg-YBSc8Df8NGQULWbq .activeCrit3{stroke:#f88;fill:#bfc7ff;stroke-width:2}#mermaid-svg-YBSc8Df8NGQULWbq .doneCrit0,#mermaid-svg-YBSc8Df8NGQULWbq .doneCrit1,#mermaid-svg-YBSc8Df8NGQULWbq .doneCrit2,#mermaid-svg-YBSc8Df8NGQULWbq .doneCrit3{stroke:#f88;fill:#d3d3d3;stroke-width:2;cursor:pointer;shape-rendering:crispEdges}#mermaid-svg-YBSc8Df8NGQULWbq .milestone{transform:rotate(45deg) scale(0.8, 0.8)}#mermaid-svg-YBSc8Df8NGQULWbq .milestoneText{font-style:italic}#mermaid-svg-YBSc8Df8NGQULWbq .doneCritText0,#mermaid-svg-YBSc8Df8NGQULWbq .doneCritText1,#mermaid-svg-YBSc8Df8NGQULWbq .doneCritText2,#mermaid-svg-YBSc8Df8NGQULWbq .doneCritText3{fill:#000 !important}#mermaid-svg-YBSc8Df8NGQULWbq .activeCritText0,#mermaid-svg-YBSc8Df8NGQULWbq .activeCritText1,#mermaid-svg-YBSc8Df8NGQULWbq .activeCritText2,#mermaid-svg-YBSc8Df8NGQULWbq .activeCritText3{fill:#000 !important}#mermaid-svg-YBSc8Df8NGQULWbq .titleText{text-anchor:middle;font-size:18px;fill:#000;font-family:'trebuchet ms', verdana, arial;font-family:var(--mermaid-font-family)}#mermaid-svg-YBSc8Df8NGQULWbq g.classGroup text{fill:#9370db;stroke:none;font-family:'trebuchet ms', verdana, arial;font-family:var(--mermaid-font-family);font-size:10px}#mermaid-svg-YBSc8Df8NGQULWbq g.classGroup text .title{font-weight:bolder}#mermaid-svg-YBSc8Df8NGQULWbq g.clickable{cursor:pointer}#mermaid-svg-YBSc8Df8NGQULWbq g.classGroup rect{fill:#ECECFF;stroke:#9370db}#mermaid-svg-YBSc8Df8NGQULWbq g.classGroup line{stroke:#9370db;stroke-width:1}#mermaid-svg-YBSc8Df8NGQULWbq .classLabel .box{stroke:none;stroke-width:0;fill:#ECECFF;opacity:0.5}#mermaid-svg-YBSc8Df8NGQULWbq .classLabel .label{fill:#9370db;font-size:10px}#mermaid-svg-YBSc8Df8NGQULWbq .relation{stroke:#9370db;stroke-width:1;fill:none}#mermaid-svg-YBSc8Df8NGQULWbq .dashed-line{stroke-dasharray:3}#mermaid-svg-YBSc8Df8NGQULWbq #compositionStart{fill:#9370db;stroke:#9370db;stroke-width:1}#mermaid-svg-YBSc8Df8NGQULWbq #compositionEnd{fill:#9370db;stroke:#9370db;stroke-width:1}#mermaid-svg-YBSc8Df8NGQULWbq #aggregationStart{fill:#ECECFF;stroke:#9370db;stroke-width:1}#mermaid-svg-YBSc8Df8NGQULWbq #aggregationEnd{fill:#ECECFF;stroke:#9370db;stroke-width:1}#mermaid-svg-YBSc8Df8NGQULWbq #dependencyStart{fill:#9370db;stroke:#9370db;stroke-width:1}#mermaid-svg-YBSc8Df8NGQULWbq #dependencyEnd{fill:#9370db;stroke:#9370db;stroke-width:1}#mermaid-svg-YBSc8Df8NGQULWbq #extensionStart{fill:#9370db;stroke:#9370db;stroke-width:1}#mermaid-svg-YBSc8Df8NGQULWbq #extensionEnd{fill:#9370db;stroke:#9370db;stroke-width:1}#mermaid-svg-YBSc8Df8NGQULWbq .commit-id,#mermaid-svg-YBSc8Df8NGQULWbq .commit-msg,#mermaid-svg-YBSc8Df8NGQULWbq .branch-label{fill:lightgrey;color:lightgrey;font-family:'trebuchet ms', verdana, arial;font-family:var(--mermaid-font-family)}#mermaid-svg-YBSc8Df8NGQULWbq .pieTitleText{text-anchor:middle;font-size:25px;fill:#000;font-family:'trebuchet ms', verdana, arial;font-family:var(--mermaid-font-family)}#mermaid-svg-YBSc8Df8NGQULWbq .slice{font-family:'trebuchet ms', verdana, arial;font-family:var(--mermaid-font-family)}#mermaid-svg-YBSc8Df8NGQULWbq g.stateGroup text{fill:#9370db;stroke:none;font-size:10px;font-family:'trebuchet ms', verdana, arial;font-family:var(--mermaid-font-family)}#mermaid-svg-YBSc8Df8NGQULWbq g.stateGroup text{fill:#9370db;fill:#333;stroke:none;font-size:10px}#mermaid-svg-YBSc8Df8NGQULWbq g.statediagram-cluster .cluster-label text{fill:#333}#mermaid-svg-YBSc8Df8NGQULWbq g.stateGroup .state-title{font-weight:bolder;fill:#000}#mermaid-svg-YBSc8Df8NGQULWbq g.stateGroup rect{fill:#ECECFF;stroke:#9370db}#mermaid-svg-YBSc8Df8NGQULWbq g.stateGroup line{stroke:#9370db;stroke-width:1}#mermaid-svg-YBSc8Df8NGQULWbq .transition{stroke:#9370db;stroke-width:1;fill:none}#mermaid-svg-YBSc8Df8NGQULWbq .stateGroup .composit{fill:white;border-bottom:1px}#mermaid-svg-YBSc8Df8NGQULWbq .stateGroup .alt-composit{fill:#e0e0e0;border-bottom:1px}#mermaid-svg-YBSc8Df8NGQULWbq .state-note{stroke:#aa3;fill:#fff5ad}#mermaid-svg-YBSc8Df8NGQULWbq .state-note text{fill:black;stroke:none;font-size:10px}#mermaid-svg-YBSc8Df8NGQULWbq .stateLabel .box{stroke:none;stroke-width:0;fill:#ECECFF;opacity:0.7}#mermaid-svg-YBSc8Df8NGQULWbq .edgeLabel text{fill:#333}#mermaid-svg-YBSc8Df8NGQULWbq .stateLabel text{fill:#000;font-size:10px;font-weight:bold;font-family:'trebuchet ms', verdana, arial;font-family:var(--mermaid-font-family)}#mermaid-svg-YBSc8Df8NGQULWbq .node circle.state-start{fill:black;stroke:black}#mermaid-svg-YBSc8Df8NGQULWbq .node circle.state-end{fill:black;stroke:white;stroke-width:1.5}#mermaid-svg-YBSc8Df8NGQULWbq #statediagram-barbEnd{fill:#9370db}#mermaid-svg-YBSc8Df8NGQULWbq .statediagram-cluster rect{fill:#ECECFF;stroke:#9370db;stroke-width:1px}#mermaid-svg-YBSc8Df8NGQULWbq .statediagram-cluster rect.outer{rx:5px;ry:5px}#mermaid-svg-YBSc8Df8NGQULWbq .statediagram-state .divider{stroke:#9370db}#mermaid-svg-YBSc8Df8NGQULWbq .statediagram-state .title-state{rx:5px;ry:5px}#mermaid-svg-YBSc8Df8NGQULWbq .statediagram-cluster.statediagram-cluster .inner{fill:white}#mermaid-svg-YBSc8Df8NGQULWbq .statediagram-cluster.statediagram-cluster-alt .inner{fill:#e0e0e0}#mermaid-svg-YBSc8Df8NGQULWbq .statediagram-cluster .inner{rx:0;ry:0}#mermaid-svg-YBSc8Df8NGQULWbq .statediagram-state rect.basic{rx:5px;ry:5px}#mermaid-svg-YBSc8Df8NGQULWbq .statediagram-state rect.divider{stroke-dasharray:10,10;fill:#efefef}#mermaid-svg-YBSc8Df8NGQULWbq .note-edge{stroke-dasharray:5}#mermaid-svg-YBSc8Df8NGQULWbq .statediagram-note rect{fill:#fff5ad;stroke:#aa3;stroke-width:1px;rx:0;ry:0}:root{--mermaid-font-family: '"trebuchet ms", verdana, arial';--mermaid-font-family: "Comic Sans MS", "Comic Sans", cursive}#mermaid-svg-YBSc8Df8NGQULWbq .error-icon{fill:#522}#mermaid-svg-YBSc8Df8NGQULWbq .error-text{fill:#522;stroke:#522}#mermaid-svg-YBSc8Df8NGQULWbq .edge-thickness-normal{stroke-width:2px}#mermaid-svg-YBSc8Df8NGQULWbq .edge-thickness-thick{stroke-width:3.5px}#mermaid-svg-YBSc8Df8NGQULWbq .edge-pattern-solid{stroke-dasharray:0}#mermaid-svg-YBSc8Df8NGQULWbq .edge-pattern-dashed{stroke-dasharray:3}#mermaid-svg-YBSc8Df8NGQULWbq .edge-pattern-dotted{stroke-dasharray:2}#mermaid-svg-YBSc8Df8NGQULWbq .marker{fill:#333}#mermaid-svg-YBSc8Df8NGQULWbq .marker.cross{stroke:#333} :root { --mermaid-font-family: "trebuchet ms", verdana, arial;} #mermaid-svg-YBSc8Df8NGQULWbq { color: rgba(232, 230, 227, 0.75); font: ; } git add git commit 工作区 暂存区 本地库

    5.代码托管中心GitHub远程库

    在代码托管中心创建远程库

    6.初始化本地库

    1.把项目文件夹作为本地库,打开文件夹,在文件夹空白区域右击选择Git Bash Here。(要点1)

    2.在Linux终端命令框中右击选择Options… ,Text,Locale选择zh_CN,Character set 选择 UTF-8 ,Select…可以设置字体大小,Save保存设置。

    3.常用的Linux命令:

    1)查看Git安装版本

    git --version

    2)清屏

    clear

    3)设置本地库的用户名和邮箱 (要点2)

    即为设置作者的信息与联系方式,俗称签名,xxx改为自己的信息
    1.全局用户名:
    git config --global user.name "xxx"
    2.全局邮箱:
    git config --global user.email "xxx@xxx.com"

    4)本地仓库的初始化

    1.初始化一个空的仓库:(要点3)
    创建完成后文件夹名为 .git ,在打开的文件夹下,是隐藏的文件夹。.git目录下的文件不能随意删除修改 git init
    2.查看隐藏的文件
    ll -la

    5)Linux命令

    1.进入文件夹
    cd .git/
    2.进入上一层文件夹
    cd ..
    3.查看显示的文件
    ll

    6)将项目文件提交到本地库(要点4)

    1.添加文件
    将工作区文件添加到暂存区add . 表示添加所有的文件 在项目文件夹下创建 Demo.txt 文件将 Demo.txt 文件添加到暂存区命令如下: git add Demo.txt
    2.提交文件
    将暂存区的文件提交到本地库-m : “Message消息注释” 提交的注释 xxx 改为自己的内容注释后写提交的文件名 git commit -m "xxx" Demo.txt

    7.本地仓库管理

    1.status(状态)命令

    1)查看暂存区有没有要提交的文件

    git status On branch maaster :在主分支nothing to commit :没有东西要提交Untrached files :没有被追踪(Git管理)的文件Changes to be committed :更新的文件可以被提交Changes not staged for commit :更改之后的文件没有提交

    2.log(日志)命令

    1)查看提交的历史记录

    git log

    2)历史记录过多产生分页

    按 空格 下一页按 b 上一页尾页显示 END按 q 退出

    3)日志展示方式

    在一行上展示日志历史记录,展示索引号和注释信息 git log --pretty=oneline 在一行上展示前7位索引号和注释信息 git log -oneline 比上一种多了HEAD{数字},数字表示指针回到当前的历史版本需要走多少步 git reflog

    3.reset(重置)命令

    1)前进或者后退历史版本

    xxxxxxx 版本索引号前7位,改为你所需回退的版本 git reset --hard xxxxxxx 回退之后 HEAD 指针索引回到对应历史的版本你的文件也会改为历史版本的文件本地库的指针移动,暂存区重置,工作区重置

    2)工作区不回退历史版本

    git reset --mixed xxxxxxx 本地库的指针移动,暂存区重置,工作区不重置

    3)只有本地库指针移动回退历史版本

    git reset --soft xxxxxxx 本地库的指针移动,暂存区不重置,工作区不重置

    4)在Linux终端中选中就是复制,右键 Paste 粘贴。

    暂存区中添加了删除操作,本地库还没有提交删除操作,也可以使用本地库来恢复工作区的文件,HEAD 指向本地库的当前指针索引 git reset --hard HEAD

    4.删除文件

    xxx 改为你要删除的文件名字

    1)删除工作区中的 xxx 文件

    rm xxx

    2)删除操作添加到暂存区

    git add xxx 添加一个删除的文件,就是在暂存区删除该文件

    3)删除操作提交到本地库

    git commit -m "" xxx

    5.diff(差异)命令

    Git按行进行管理,把有差异的那一行删除,再添加一行

    1)

    Processed: 0.011, SQL: 8