• git教程


     

    目录
    • Git和SVN的区别
    • git教程:理论基础
      • git的三棵树:
        • 工作区域(工作目录):
        • 暂存区域:
        • git仓库:
      • git的工作流程:
      • git回滚快照的工作流程:
      • git管理的文件三状态:
    • git教程:入门操作
      • 初次使用Git的前的配置
        • 1、在命令行模式中输入以下命令:
        • 2、然后查看配置信息:
        • 3、进入git的项目目录:
        • 4、初始化git:
      • 查看快照:
      • 删除两个无用commit
      • Git删除版本库中的一个commit
    • git教程:git分支
      • git创建分支:
      • git删除分支:
      • git切换分支:
      • git合并分支:
      • git创建分支的方式:指针指向分支的开始点
    • git教程:几种状态
      • 正常状态
      • 有文件被删除的状态
      • 有新文件的状态
      • 文件内容被修改的状态
      • 文件被重命名时的状态
    • git教程:版本对比
      • git diff表示版本对比
      • git diff 快照ID1 快照ID2
      • git diff 快照ID
      • git diff --cached 快照ID
    • How to push your projects to github
      • create a new repository on the command line
      • push an existing repository from the command line
      • github url of https
      • github url of ssh

    Git和SVN的区别

    SVN记录的是每一次版本变动的内容
    Git是将每个版本独立保存

    git教程:理论基础

    三棵树
    工作流程
    三状态

    git的三棵树:

    在这里插入图片描述

    工作区域(工作目录):

    平时存放项目的地方。

    暂存区域:

    临时存放你的改动。是一个文件,存放即将提交到仓库的一个列表信息。

    git仓库:

    最终安全存放所有版本信息的位置。

    git的工作流程:

    • 1.在工作目录中添加、修改文件。
    • 2.将需要进行版本管理的文件放入暂存区域。
    • 3.将暂存区域的文件提交到git仓库。

    git回滚快照的工作流程:

    • 1.移动HEAD的指向(--soft)
    • 2.将快照回滚到暂存区域(--mixed,默认)
    • 3.将暂存区域还原到工作目录(--hard)

    git管理的文件三状态:

    • 已修改(modified)
    • 已暂存(staged)
    • 已提交(committed)

    git教程:入门操作

    初次使用Git的前的配置

    (已安装Git软件,并在安装时自动配置环境变量path):

    1、在命令行模式中输入以下命令:

    git config --global user.name  "Tsui"
    git config --global user.email  "tsuish@qq.com"
    

    2、然后查看配置信息:

    git config --list
    

    3、进入git的项目目录:

    C:UsersAdministrator>cd /d D:workspaceGit_Myproject
    D:workspaceGit_Myproject>
    

    4、初始化git:

    D:workspaceGit_Myproject>git init
    Reinitialized existing Git repository in D:/workspace/Git_Myproject/.git/
    

    git status:查看git的当前工作状态

    查看快照:

    git log
    git log --oneline
    //--oneline:快照的精简版本
    git log --help
    //git log的帮助文档,会在浏览器中打开
    git log --graph --all
    //--graph表示图形化显示log
    //--all表示显示所有的分支
    D:workspaceGit_Myproject>git log --oneline --graph --all
    * 5b6b147 (HEAD -> master) modify a file cshcesh.txt v0.3
    | * bef58a0 (feature) add a file testspool.txt featurev0.2
    | * 220fa33 add a file test.txt featurev0.1
    |/
    * 1346315 add a file cshcesh.txt v0.2
    * 62a8bde add two file v0.1
    

    快照ID不用全部输入,前5个就可以
    新添加的文件不在暂存区域,则为未跟踪状态。
    新添加的文件在暂存区域,则为跟踪状态。

    • 场景1:当你改乱了工作区某个文件的内容,想直接丢弃工作区的修改时,用命令 git checkout -- file 。(懂)
    • 场景2:当你不但改乱了工作区某个文件的内容,还添加到了暂存区时,想丢弃修改,分两步,第一步用命令 git reset HEAD file ,就回到了场景1,第二步按场景1操作。(懂)
    • 场景3:已经提交了不合适的修改到版本库时,想要撤销本次提交,参考 版本回退(https://www.liaoxuefeng.com/wiki/896043488029600/897013573512192)一节,不过前提是没有推送到远程库(不懂)

    删除两个无用commit

    • 1、首先修改仓库(本地和暂存区域不变),回退到前面的版本
      git reset --soft HEAD~3
      
    • 2、然后commit,中间的版本相当于删除了
      git commit -m "delete 2 commits"
      

    Git删除版本库中的一个commit

    参考链接:https://www.jianshu.com/p/ee8fb047b085

    • 在使用git的时候,本来不想提交一个文件的,但是一不小心就commit了,所以就了解了一下怎么删除该commit,当你此时还没有push的时候,只需要一条命令:
      git reset --hard HEAD~1
      
    • 直接取消上一次 commit用git reset HEAD~1; 如果加上参数--hard 则不保留当前更改
      如果你已经push了,那么使用如下命令:
      //回滚到你想回滚的commit
      git reset --hard <commit_id> 
      //重新push到你的远程仓库
      git push origin HEAD --force
      

    git教程:git分支

    git创建分支:

    D:workspaceGit_Myproject>git branch feature
    //创建一个分支,分支名是feature
    D:workspaceGit_Myproject>git log --decorate
    //--decorate标记让git log显示指向这个提交的所有引用(比如说分支、标签等)
    commit 13463151f6863d6c2b1e9a9a90bc886ad13c7383 (HEAD -> master, feature)
    //默认分支是master,即主分支
    //另一个分支是feature
    //HEAD现在指向的是master分支
    Author: Tsui <tsuish@qq.com>
    Date:   Sun Apr 28 22:10:40 2019 +0800
        add a file cshcesh.txt v0.2
    commit 62a8bdeee21268fd7d3285c18e1b8e476ece16a3
    Author: Tsui <tsuish@qq.com>
    Date:   Fri Apr 26 15:21:56 2019 +0800
        add two file v0.1
    

    git删除分支:

    git branch -d或 --d 分支名
    //--d是全称,--d是精简的表示,两个命令一样

    D:workspaceGit_Myproject>git branch -d feature2
    Deleted branch feature2 (was 678c20e).
    D:workspaceGit_Myproject>git log --all --graph --oneline
    * 678c20e (HEAD -> master) add a file feature2.txt feature2v0.1
    *   de4c5bf Merge branch 'feature'
    |
    | * bef58a0 add a file testspool.txt featurev0.2
    | * 220fa33 add a file test.txt featurev0.1
    * | 5b6b147 modify a file cshcesh.txt v0.3
    |/
    * 1346315 add a file cshcesh.txt v0.2
    * 62a8bde add two file v0.1
    

    git切换分支:

    (从分支切换到主分支,分支文件也显示了)

    D:workspaceGit_Myproject>git checkout feature
    //git checkout -b feature2:表示创建并切换分支
    Switched to branch 'feature'
    D:workspaceGit_Myproject>git log
    commit 13463151f6863d6c2b1e9a9a90bc886ad13c7383 (HEAD -> feature, master)
    Author: Tsui <tsuish@qq.com>
    Date:   Sun Apr 28 22:10:40 2019 +0800
        add a file cshcesh.txt v0.2
    commit 62a8bdeee21268fd7d3285c18e1b8e476ece16a3
    Author: Tsui <tsuish@qq.com>
    Date:   Fri Apr 26 15:21:56 2019 +0800
        add two file v0.1
    

    git合并分支:

    git merge 分支名

    D:workspaceGit_Myproject>git merge feature2
    Updating de4c5bf..678c20e
    Fast-forward
    feature2.txt | 1 +
    1 file changed, 1 insertion(+)
    create mode 100644 feature2.txt
    

    git创建分支的方式:指针指向分支的开始点

    图解如下:

    在这里插入图片描述
    在这里插入图片描述
    在这里插入图片描述

    git教程:几种状态

    正常状态

    D:workspaceGit_Myproject>git status
    On branch master
    nothing to commit, working tree clean
    

    有文件被删除的状态

    (从工作区域删除了,暂存区域不变,仓库不变):

    D:workspaceGit_Myproject>git status
    On branch master
    Changes not staged for commit:
      (use "git add/rm <file>..." to update what will be committed)
      (use "git checkout -- <file>..." to discard changes in working directory)
            deleted:    README.md
    no changes added to commit (use "git add" and/or "git commit -a")
    

    有两个选择:
    选择1:删除版本库中对应文件(分三步)
    选择2:从版本库中恢复文件

    • 选择1:(第一步:将文件从暂存区域删除)
    D:workspaceGit_Myproject>git rm README.md
    //git rm -f README.md表示暴力删除(force remove)
    //应用场景:删除工作区域和暂存区域内容不一样的文件
    //加-f会同时把工作区域和暂存区域的文件删除(这一点和不加一样)
    //git rm --cached README.md表示:
    //只删除暂存区域的文件,保留工作区域的文件
    rm 'README.md'
    
    • 选择1:(第二步【会在log中新建快照】:将暂存区域文件提交到仓库)
    D:workspaceGit_Myproject>git commit -m "remove README.md"
    //-m表示提交暂存区域文件到仓库时的注释内容
    [master 0eb2b88] remove README.md
    1 file changed, 1 deletion(-)
    delete mode 100644 README.md
    
    • 选择1:(第三步:将仓库中新建的快照删除)
    D:workspaceGit_Myproject>git reset --soft 快照ID
    
    • 选择2:
    D:workspaceGit_Myproject>git checkout -- README.md
    D:workspaceGit_Myproject>git status
    On branch master
    nothing to commit, working tree clean
    

    有新文件的状态

    (工作区域新增文件,未在暂存区域,未提交到仓库):

    D:workspaceGit_Myproject>git status
    On branch master
    Untracked files:
      (use "git add <file>..." to include in what will be committed)
            README.md
    nothing added to commit but untracked files present (use "git add" to track)
    

    将新文件存放到仓库,分两步

    • 第一步:添加文件到暂存区域(工作区域新增文件,暂存区域新增文件,未提交到仓库):
    D:workspaceGit_Myproject>git add README.md
    //也可以git add *
    D:workspaceGit_Myproject>git status
    On branch master
    Changes to be committed:
      (use "git reset HEAD <file>..." to unstage)
            new file:   README.md
    
    • 第二步:将暂存区域文件提交到仓库:
    D:workspaceGit_Myproject>git commit -m "add README.md 2019-4-25"
    //-m表示提交暂存区域文件到仓库时的注释内容
    [master 1c0e390] add README.md 2019-4-25
    1 file changed, 1 insertion(+)
    create mode 100644 README.md
    

    文件内容被修改的状态

    (工作区域文件修改,暂存区域不变,仓库不变):

    D:workspaceGit_Myproject>git status
    On branch master
    Changes not staged for commit:
      (use "git add <file>..." to update what will be committed)
      (use "git checkout -- <file>..." to discard changes in working directory)
            modified:   README.md
    no changes added to commit (use "git add" and/or "git commit -a")
    

    将修改后的文件添加到仓库,分两步

    • 第一步:将修改后的文件添加到暂存区域:
    D:workspaceGit_Myproject>git add README.md
    D:workspaceGit_Myproject>git status
    On branch master
    Changes to be committed:
      (use "git reset HEAD <file>..." to unstage)
            modified:   README.md
    
    • 第二步:将暂存区域文件提交到仓库:
    D:workspaceGit_Myproject>git commit -m "modify: README.md 2019-4-25 v0.1"
    //-m表示提交暂存区域文件到仓库时的注释内容
    [master 2b09814] modify: README.md 2019-4-25 v0.1
    1 file changed, 1 insertion(+), 1 deletion(-)
    

    文件被重命名时的状态

    (工作区域文件修改,暂存区域不变,仓库不变):
    git显示原文件被删除,添加了一个新文件。

    D:workspaceGit_Myproject>git status
    On branch master
    Changes not staged for commit:
      (use "git add/rm <file>..." to update what will be committed)
      (use "git checkout -- <file>..." to discard changes in working directory)
            deleted:    cesh.txt
    Untracked files:
      (use "git add <file>..." to include in what will be committed)
            cshcesh.txt
    no changes added to commit (use "git add" and/or "git commit -a")
    

    重命名文件名的方法,分三步:
    第一步:在工作区域把文件名改回去,git状态正常。
    第二步:用git mv命令修改工作区域和暂存区域文件名
    第三步:提交暂存区域文件到仓库中

    D:workspaceGit_Myproject>git mv cesh.txt cshcesh.txt
    //git mv 旧文件名 新文件名 等价于以下两步:
    //    git rm 旧文件名
    //    git add 新文件名
    D:workspaceGit_Myproject>git status
    On branch master
    Changes to be committed:
      (use "git reset HEAD <file>..." to unstage)
            renamed:    cesh.txt -> cshcesh.txt
    D:workspaceGit_Myproject>
    D:workspaceGit_Myproject>git commit -m "rename cesh.txt -> cshcesh.txt v0.4"
    [master 879bee4] rename cesh.txt -> cshcesh.txt v0.4
    1 file changed, 0 insertions(+), 0 deletions(-)
    rename cesh.txt => cshcesh.txt (100%)
    

    git教程:版本对比

    git diff表示版本对比

    仓库为旧-,工作区域为新+

    git diff 快照ID1 快照ID2

    表示比较两个快照
    快照顺序有影响,前面的为旧-,后面的为新+

    git diff 快照ID

    表示比较当前工作区域和仓库中的快照。
    仓库为旧-,工作区域为新+

    git diff --cached 快照ID

    表示比较暂存区域和仓库中的快照。
    暂存区域为旧-,仓库为新+

    不加快照ID表示当前HEAD指向的快照

    D:workspaceGit_Myproject>git status
    On branch master
    Changes not staged for commit:
      (use "git add <file>..." to update what will be committed)
      (use "git checkout -- <file>..." to discard changes in working directory)
            modified:   README.md
            modified:   game.py
    no changes added to commit (use "git add" and/or "git commit -a")
    D:workspaceGit_Myproject>git diff
    //git diff命令:查看文件改动的详情
    diff --git a/README.md b/README.md
    //表示对比的是工作区域和暂存区域的文件README.md
    index 197049a..e30bfa5 100644
    --- a/README.md
    //---表示是旧文件,即存放在暂存区域的文件
    +++ b/README.md
    //+++表示是新文件,即存放在工作区域的文件
    @@ -1 +1,2 @@
    //@@开头@@结尾,中间的-表示旧文件,+表示新文件,
    //-1表示旧文件从第一行开始显示
    //+1,2表示新文件从第一行开始显示,一共显示两行
    //-1后没有数字,表示旧文件已经完全包含在新文件中了
    +1233311111111111111111111111
    //+号后面的内容表示是新文件所特有的
    <E6><96><87><E5><AD><97><E6><B8><B8><E6><88><8F>
     No newline at end of file
    //这两行表示是新旧文件共有的内容
    // No newline at end of file表示:文件不是以换行符结束
    diff --git a/game.py b/game.py
    index e69de29..8ffc73b 100644
    --- a/game.py
    +++ b/game.py
    @@ -0,0 +1 @@
    +123133333333333333
     No newline at end of file
    D:workspaceGit_Myproject>
    

    How to push your projects to github

    create a new repository on the command line

    echo "# springdemo" >> README.md
    git init
    git add README.md
    git commit -m "first commit"
    git remote add origin https://github.com/BitTsui/springdemo.git
    git push -u origin master
    

    push an existing repository from the command line

    git remote add origin https://github.com/BitTsui/springdemo.git
    git push -u origin master
    

    github url of https

    https://github.com/BitTsui/springdemo.git
    

    github url of ssh

    git@github.com:BitTsui/springdemo.git
  • 相关阅读:
    java如何导入Excel文件
    C/S框架设计经验小结
    WebClient以POST方式发送Web请求
    如何自动拼接 Update语句,仅Update已修改的字段
    DataGridView如何快速导出Excel
    【转】基于STM32F103内部AD测量电池电压
    【转】stm8 rtc时钟
    【转】NiOS II从EPCQ256的自启动设置
    【转】验收代码寄存器和验收屏蔽寄存器
    【转】eclipse : Type Symbol 'xxx' could not be resolved 解决办法
  • 原文地址:https://www.cnblogs.com/onesea/p/13521757.html
Copyright © 2020-2023  润新知