Experimental work a soft Git code version management

Purpose:

1) understand the core mechanism of distributed version control system;

2) git master basic instructions and a branch management instruction;

Experiment:

1) install git

2) the initial configuration git, git init git status command

3) master git log, git add, git diff command

4) master git tag git branch, git commit command

5) master git revert command

Experimental records:

1) experimental content and results screenshot:

2) the problem occurred during the experiment and solve.

1. Installation with initial configuration git

Configure the user name, email, and ensure that content Git output color-coded. Link and code editor Sublime.

2. Create a warehouse from scratch 

   2.1 Creating the project directory

Create a directory: se2020-git-course, in this directory, create another directory: new-git-project, using the cd command to move to the next new-git-project directory.

Run git init, generates an empty Git repository in the current directory.

 

 

 

   2.2 Cloning an existing warehouse

Enter the command git clone, and then enter the path you want to clone the Git repository. The experiment path: https: //github.com/udacity/course-git-blog-project.

 

     Analyzing the prior state warehouse 2.3

git status is to understand the core of Git. It will tell us anything, and we have seen the state of Git Git repository under consideration.

 

 

Question: how to prove that there is no any library commit.

Running git log, as follows:

3.git log command

 git log command information repository for all commit to display. By default, the command displays the warehouse of each commit: SHA, author, date, news

 

   3.1git log --oneline command

 git log --oneline, the warehouse can be used to change the information displayed.

 

  3.2git log --stat command

 git log --stat, used to display files and commit changes in the number of lines added or deleted.

此命令会:1.显示被修改的文件

                  2.显示添加/删除的行数

                  3.显示一个摘要,其中包含修改/删除的总文件数和总行数

 

 3.3git log -p 命令

git log -p 命令可用来显示对文件作出实际更改的命令。

此命令会:1.显示被修改的文件

                        2.显示添加/删除的行所在的位置

                        3.显示做出的实际更改

  

 

 3.4git show 命令

仅显示一个特定的commit,方便查阅。

 

 4.git add& git commit&git diff

4.1创建HTML文件,文件名为index.html,并添加一些初始代码。

建立js和css文件夹,并在文件夹下分别建立app.js和app.css文件,文件内容可为空。

输入git status 查看状态

 

4.2git add

将index.html添加到暂存区,并利用 git status查看

 

 

 显示index.html被成功加入到暂存区。

再暂存另外两个文件。现在我们可以运行以下命令:

$ git add css/app.css js/app.js

或者使用 git add .   ( 句点指代当前目录,可以用来表示所有文件和目录(包括所有嵌套文件和目录!))。

 

 

4.3 git commit

要在 git 中提交 commit,你需要使用 git commit 命令,但是先别运行这条命令。运行这条命令将会打开你在第一节课配置的代码编辑器。

 

 4.4 使用 -m 选项绕过编辑器

在index.html 中的body标记中加入

<header>  

<h1><Expedition></h1>

</header>

运行git status

 

将index.html 文件添加到暂存区,并运行git status查看是否已经保存。

 

 使用 git commit 命令提交 commit,并添加提交说明 Add header to blog。

 

 

然后运行git log 查看刚刚提交的commit 说明。

 

 

4.5git diff

 此工具可以在进行提交之前告诉我们已对文件进行了什么样的更改。

用git diff 命令来查看已加入但是尚未提交的更改。在index.html中将标题改为Adventure,保存后在终端上运行git diff。

 

 

4.6  .gitignore

如果你想将某个文件保留在项目的目录结构中,但是确保它不会意外地提交到项目中,可以使用名称特殊的文件 .gitignore

(注意文件名开头的点,很重要!)。

在目录下新建word 文件夹project.docx.当运行git status 时会出现在文件目录上。

 

 此时在文件new-git-project文件中添加 .gitignore 文件,并在文件中添加 project.docx,再次运行git status 将不会再出现

在文件目录中。

 5. 标签与分支

5.1git tag 命令

进入,前面建立的 new-git-project 项目文件夹中。

5.1.1创建标签

使用 git tag 命令与仓库的标签进行交互,输入以下命令 :git tag -a v1.0

 

 

 

 5.1.2 验证标签

保存并退出编辑器后,命令行上什么也不会显示。只需输入 git tag,命令行会显示仓库中的所有标签。

 

 然后运行 git log 查看标签的位置

 

5.1.3删除标签

可以通过输入 -d 选项 (表示 delete 删除!)加上标签名称来删除 git 标签:git tag -d v1.0

 

 

 5.1.4向以前的 commit 添加标签

运行 git tag -a v1.0 将为最近的 commit 添加标签。

或提供要添加标签的 commit 的 SHA ,可以通过查询历史SHA git log --oneline,查找已经commit  的SHA。

 

 输入 git tag -a v1.0 4d87b86添加标签

 

 5.2git branch 分支

5.2.1 git branch 命令用来与 git 的分支进行交互:列出仓库所有的分支名称,创建新的分支,删除分支。

 

 5.2.2创建分支

要创建分支,只需使用 git branch 并提供要创建的分支对应的名称。因此,如果你想创建一个叫做"sidebar"的分支,

只需运行以下命令:gti branch sidebar

 

但是当前分支仍然是master.

使用 git checkout命令你切换分支。

 

使用 git log --oneline查看。

 

 5.2.3活跃分支

判断活跃分支的最快速方式是查看 git branch 命令的输出结果。活跃分支名称旁边会显示一个星号。

 

 5.2.4删除分支

首先切换掉想要删除的分支,确保其不是当前分支,例如sidebar。

使用git checkout master命令。

 

 

 然后在使用 git branch -d sidebar命令删除sidebar 分支。

删除提示:

1)如果某个分支上有任何其他分支上都没有包含的 commit(也就是这个 commit 是要被删除的分支独有的),git 不会删除该分支。

2)如果你创建了 sidebar 分支,向其添加了 commit,然后尝试使用 git branch -d sidebar 删除该分支,git 不会让你删除该分支,

因为你无法删除当前所在的分支。

3)如果你切换到 master 分支并尝试删除 sidebar 分支,git 也不会让你删除,因为 sidebar 分支上的新 commit 会丢失!要强制删除,

你需要使用大写的 D 选项 git branch -D sidebar

 

5.3 高效分支

前期准备

 

5.3.1分支实战 

更改 1 - 添加页面颜色,确保位于 master 分支上,并向 css/app.css 添加以下内容:

 

更改 2 - 添加侧栏,我们向页面上添加一个侧栏

 先使用命令 git log --oneline 查看commit。在输入git branch sidebar 4d87b86,向该commit添加分支。

最后切换到sidebar分支后,会发现之前在master中添加的 Set background color for page没有了 。

 

添加html文件

 

更改 3 - 更改 master 上的标题,切换到 master 分支并更新页面标题。

切换分支

 

 保存 index.html 文件并进行 commit 以将此更改添加到仓库中。

 

 同时查看所有分支:git log --oneline  --graph --all

 

 6.合并

将分支组合到一起称为合并

6.1合并指令

git merge 指令用来合并 git 分支:

 

 6.2合并冲突

首先人为制造冲突。

更改master标签,并commit保存。

 

 创建一个heading-update分支,使用git --oneline --graph --all查看,

 

保存heading-update commit。合并分支发生冲突。

 

 

 运行git status查看

 

 修改index.html解决冲突

 

 7.撤销更改

7.1更改最后一个 commit,git commit --amend

 

 7.2 还原commit :  git revert <SHA-of-commit-to-revert>

 

 7.3重置

git reset 命令

git reset 命令用来重置(清除)commit: git reset <reference-to-commit>

git reset 的选项

git 根据所使用选项来判断是清除、暂存之前 commit 的更改,还是取消暂存之前 commit 的更改。这些选项包括:

  • 使用 --hard 选项清除 commit

  • 使用 --soft 选项将 commit 的更改移至暂存区

  • 使用 --mixed 选项取消暂存已被 commit 的更改 

 

 

 

 7.4备份分支

先创建一个分支用来备份 :git branch backup

 

 

 

 

 总结与体会:

       因为以前对GIT从未有过任何了解,所以刚开始时根本无从下手,起初配置软件时,还不知道软甲的用处,在配置过程中就遇到了很多麻烦,经过网上查阅资料才对GIT有了进一步的了解,在完成配置后,根据实验你步骤的指导起初一步步的操作感觉还不是很难,因为刚开始还是很顺利的,但是在做到需要更换上一级文件目录时出现了问题,不知道需要用什么命令返回上一级目录,进故宫查阅资料,利用 cd ..\, 然后会弹出  >,这是直接回车即可返回上一级目录。在后来的操作中因为第一次使用GIT,实验过程一路磕磕绊绊,才得以完成,在解决合并冲突,人为制造合并冲突时,同样遇到了麻烦,经历了好几次的摸索,最后得以解决,其中heading-up的分支commit 为上一个的分支的,随机一个commit ,而我在实验中使用了与master更改时相同的commit ,导致无法产生冲突。

    经过这次实验对GIT有了初步的认识与了解,对GIT软件的使用,以及GIT的语法命令有了更进一步的掌握,对建立仓库,添加commit,修改commit其中git status,git log --oneline.在对GIT的使用中起到很大作用,对编译者很友好,可以方便查询状态信息。

      

思考题:

  阅读维基百科和百度百科的Git词条,总结分布式版本控制系统的核心机理

  答:Git是一个开源的分布式版本控制系统,可以有效、高速地处理从很小到非常大的项目版本管理。Git 是 Linus Torvalds 为了帮助管理 Linux 内核开发而开发的一个开放源码的版本控制软件。分布式版本控制系统是一种有效、高速地处理从很小到非常大的项目版本管理系统。与它相对的是集中式版本控制系统。在分布式版本控制系统中客户端并不像集中式版本控制系统那样提取最新版本的文件快照,而是把代码仓库完整地镜像下来。这样的核心机理就轻松的解决了集中式版本控制系统中容易出现的也是很致命的中央服务器单点故障。分布式版本控制系统没有所谓的"中央服务器",每个人的电脑上都是一个完整的版本

库。通常分布式版本控制系统有一台"伪中央服务器",但是这个服务器的作用仅仅是用来方便"交换"大家的修改,没有它大家仍然可以正常工作。

 

Guess you like

Origin www.cnblogs.com/chh1012/p/12375113.html