• ZH奶酪:Git简明教程


    这里是原网站:https://try.github.io/levels/1/challenges/1

    这篇博文就当是笔记+翻译吧。

    几个名词相关

    changes:变更

    repository:仓库

    staging area:缓存区

    local:本地

    remote:远程

    git的工作流程

    本地文件变更>add>缓存区(staging area)>commit>本地仓库>push>远程仓库(GitHub)

    开始

    在git bash中进入工作目录,比如“octobox”

    1.初始化一个git repository

    git init

    然后“octobox”目录下就会有一个空的repository保存在/.git/,这个repository是由Git操作的隐藏目录

    2.看一下当前project的状态

    git status

    因为项目是刚刚创建的,所以提示没有需要提交的内容:

    # On branch master
    #
    # Initial commit
    #
    nothing to commit (create/copy files and use "git add" to track)

    3.在“octobox”中新建一个txt文档“octocat.txt”,再查看一下当前project的状态:

    git status

    此时会显示octocat.txt是“untracked files”,这是因为Git看到了这是一个文件:

    # On branch master
    #
    # Initial commit
    #
    # Untracked files:
    # (use "git add <file>..." to include in what will be committed)
    #
    # octocat.txt
    nothing added to commit but untracked files present (use "git add" to track)

    4.要告知Git开始追踪octocat.txt文件中的改动,我们首先要把该文件添加到staging area中:

    git add octocat.txt

    5.现在Git已经开始追踪我们的octocat.txt文件了,现在让我们看一下project的状态:

    git status

    然后我们可以看到Git如何展示“待提交的变更(changes to be committed)”,这里列举的文件都是Staging area中的文件,它们还没有保存到我们的repository中:

    # On branch master
    #
    # Initial commit
    #
    # Changes to be committed:
    #   (use "git rm --cached <file>..." to unstage)
    #
    #    new file:   octocat.txt
    #

     6.要保存我们在Staging area中的变更到repository,我们需要提交,一般会附加一条描述变更的信息:

    git commit -m "Add cute octocat story"

     接下来就会看到提交成功的信息 :

    [master (root-commit) 20b5ccd] Add cute octocat story
    1 file changed, 1 insertion(+)
    create mode 100644 octocat.txt

    7.如何提交大量的变更呢?在当前目录“octobox”下:(1)新建一些txt文件;(2)新建一个目录,在新建目录下也放一些txt文件,然后添加到staging area:

    git add '*.txt'

    8.然后提交到repository:

    git commit -m 'Add all the octocat txt files'

    此时就可以看到我们刚刚新建的那些全部的txt都被提交到repository中了:

    [master 3852b4d] Add all the octocat txt files
    4 files changed, 4 insertions(+)
    create mode 100644 blue_octocat.txt
    create mode 100644 octofamily/baby_octocat.txt
    create mode 100644 octofamily/momma_octocat.txt
    create mode 100644 red_octocat.txt

    9.我们已经做了一些提交,那么如何浏览我们曾经提交了哪些变更呢?

    git log

    此时就可以看到我们全部的历史变更了:

    commit 3852b4db1634463d0bb4d267edb7b3f9cd02ace1
    Author: Try Git <try_git@github.com>
    Date: Sat Oct 10 08:30:00 2020 -0500
    
    Add all the octocat txt files
    
    commit b652edfd888cd3d5e7fcb857d0dabc5a0fcb5e28
    Author: Try Git <try_git@github.com>
    Date: Sat Oct 10 08:30:00 2020 -0500
    
    Added cute octocat story

    10.到目前位置的变更、提交一系列操作,我们还都是在本地机器上进行的,怎么保存到网上(GitHub)呢?

    首先在GitHub上新建一个空的repository,然后记下该repository的url,比如:https://github.com/try-git/try_git.git

    要想将我们本地的repository保存到GitHub服务器,我们要添加一个远程仓库(remote repository),git remote add这个命令需要提供远程仓库的名字(自定义)和repository的url(你在GitHub上创建的那个repository):

    git remote add origin https://github.com/try-git/try_git.git

    11.把我们的变更push到远程仓库 origin (on GitHub):

    我们远程仓库名字是“origin”,本地默认分支(default branch)的名字是“master”,-u 是告诉Git记住后边的参数,这样下次我们就可以直接使用git push进行push了:

    git push -u origin master

    12.假如过了一段时间,有其他人从我们的GitHub上pull了我们的repository、并进行了提交、push,比如添加了一些文件,现在我们想把最新的repository下载(pull)到我们本地电脑中:

    git pull origin master

    然后我们可以看到pull的版本:

    Updating 3852b4d..3e70b0f
    Fast-forward
    yellow_octocat.txt | 1 +
    1 file changed, 1 insertion(+)
    create mode 100644 yellow_octocat.txt

    13.pull之后,我们可以查看现在的repository和我们最后一次提交时的状态有哪些不同,其中HEAD是一个指针,指向我们最近的一次提交commit:

    git diff HEAD

    此时可以看到详细的不同:

    diff --git a/octocat.txt b/octocat.txt
    index 7d8d808..e725ef6 100644
    --- a/octocat.txt
    +++ b/octocat.txt
    @@ -1 +1 @@
    -A Tale of Two Octocats
    +A Tale of Two Octocats and an Octodog

    14.diff命令还可以查看已经staged的文件(在staging area),staged files就是我们告诉git已经准备好提交的文件,假如我们变更了octofamily/octodog.txt,我们把它add到stage中:

    git add octofamily/octodog.txt

    15.然后看一下staged文件有哪些difference:

    git diff --staged

    然后看到下边的信息:

    diff --git a/octofamily/octodog.txt b/octofamily/octodog.txt
    new file mode 100644
    index 0000000..cfbc74a
    --- /dev/null
    +++ b/octofamily/octodog.txt
    @@ -0,0 +1 @@
    +woof

    16.我们不想提交octodog.txt了,要把octodog.txt从staging area中删掉(unstaged):

    git reset octofamily/octodog.txt

    17.git reset帮我们把octodog.txt从staging area中unstaged了,但是可以注意到它其实还是存在的,只是不在staging area中了,如果时间倒流,然octodog.txt回到octodog.txt出现之前(这个例子中也就是消失),那是不是很好。文件可以改回到我们的最后一次提交的状态,比如我们更改了octocat.txt文件,但是想让它回到最后一次提交的状态:

    git checkout -- octocat.txt

    18.当开发者开发一个新的feature(项目中的功能)或者修复bug的时候,他们通常都会创建一个repository的拷贝(分支,branch),以便让他们隔离提交,以至于不会影响真正的产品,然后当他们完成之后,就可以合并他们的branch和主要的master branch。

    我们想清除一些txt文件,先新建一个branch clean_up:

    git branch clean_up

    19.现在如果输入git branch,就会看到两个本地分支,一个主要分支master,一个新建分支clean_up:

    git branch

    显示:

      clean_up
    * master

    *表示当前工作分支,我们要切换分支到clean_up:

    git checkout clean_up

    显示切换成功:

    Switched to branch 'clean_up'

    20.现在在clean_up分支上了,可以用git rm完成刚才想做的清除任务了(git rm不仅仅从硬盘上清除文件,还会把removal放到staging area中)

    git rm '*.txt'

    清除结果:

    rm 'blue_octocat.txt'
    rm 'octocat.txt'
    rm 'octofamily/baby_octocat.txt'
    rm 'octofamily/momma_octocat.txt'
    rm 'red_octocat.txt'

    21.可以用git status查看一下当前stage中的changes:

    git status

    显示:

    # On branch clean_up
    # Changes to be committed:
    # (use "git reset HEAD <file>..." to unstage)
    #
    # deleted:    blue_octocat.txt
    # deleted:    octocat.txt
    # deleted:    octofamily/baby_octocat.txt
    # deleted:    octofamily/momma_octocat.txt
    # deleted:    red_octocat.txt
    #
    # Untracked files:
    # (use "git add <file>..." to include in what will be committed)
    #
    # octofamily/

    然后提交刚刚的变更:

    git commit -m "Remove all the cats"

    提交结果:

    [clean_up 63540fe] Remove all the cats
    5 files changed, 5 deletions(-)
    delete mode 100644 blue_octocat.txt
    delete mode 100644 octocat.txt
    delete mode 100644 octofamily/baby_octocat.txt
    delete mode 100644 octofamily/momma_octocat.txt
    delete mode 100644 red_octocat.txt

    22.回到master分支:

    git checkout master

    23.合并clean_up到master:

    git merge clean_up

    显示:

    Updating 3852b4d..ec6888b
    Fast-forward
    blue_octocat.txt | 1 -
    octocat.txt | 1 -
    octofamily/baby_octocat.txt | 1 -
    octofamily/momma_octocat.txt | 1 -
    red_octocat.txt | 1 -
    5 files changed, 5 deletions(-)
    delete mode 100644 blue_octocat.txt
    delete mode 100644 octocat.txt
    delete mode 100644 octofamily/baby_octocat.txt
    delete mode 100644 octofamily/momma_octocat.txt
    delete mode 100644 red_octocat.txt

    24.既然已经完成了清除工作,那么就不需要clean_up这个分支了,可以卸磨杀驴了:

    git branch -d clean_up

    25.前几步都是在本地玩的,现在可以把成品放到remote repository了:

    git push
  • 相关阅读:
    left join的多重串联与groupby
    转换坐标为数字型的函数
    oracle 11g 导出空表
    sql优化
    pl/sql使用技巧
    佳能mp288拆解步骤--绝对原创
    转)delphi chrome cef3 控件学习笔记 (二)
    mac, ios 模拟器
    一个人软件独立开发。
    Delphi在Android下通过WiFI进行调试
  • 原文地址:https://www.cnblogs.com/CheeseZH/p/4653810.html
Copyright © 2020-2023  润新知