Git distributed version control system and github

The fourth stage of improvement

Time: August 29, 2023

Participants: All members of the class

Contents:

Git distributed version control system and github

Table of contents

1. Case Overview

2. Version control system

(1) Local version control

(2) Centralized version control system

(3) Distributed version control system

3. Introduction to Git

(I. Overview

(2) Git development history

(3) Git functional characteristics

4. GIT installation and deployment

(1) Case environment

(2) Install git

(3) Git command routine operations

(4) Branch structure

(5) Use of Git on Windows

5. GitHub hosting service

(1) Register GitHub

(2) Obtain the key on the Linux host

(3) Methods to obtain Windows host key

(4) Create a warehouse

(5) Push testing on personal host

(6) Create new files and perform pull tests

(7) Pull file test


1. Case Overview

        When developing a software project, it can still be maintained when there are only dozens or hundreds of lines of code locally. However, when the code reaches a certain amount or when two or three people jointly develop a project, code confusion and conflicts can easily occur. , troubleshooting and other issues.

        Once the development is completed, it is discovered that the entire project cannot run, and the person responsible for the submitted code cannot be confirmed, resulting in the maintenance of the project taking several times the time. In order to solve the above problems, the version control system came into being.

        Version control refers to the management of changes in various program codes, configuration files, documentation and other files during the software development process. It is one of the core ideas of software configuration management.

2. Version control system

(1) Local version control

        Local version control system Many people are used to copying the entire project directory to save different versions, and perhaps changing the name and adding the backup time to show the difference.

        The only advantage of doing this is that it is simple, but it is also very easy to make mistakes. Sometimes the working directory is confused, the wrong file is accidentally written or an unexpected file is overwritten, and the modifications in the file cannot be recognized.

(2) Centralized version control system

        How to let developers on different systems work together? As a result, Centralized Version Control Systems (CVCS) came into being.

        Such systems, such as CVS, Subversion (SVN) and Perforce, etc., have a single centrally managed server that saves revisions of all files, and people who work together connect to this server through clients and retrieve the latest revisions. File or submit an update. This has become standard practice for version control systems over the years.

(3) Distributed version control system

        In such systems, such as Git, Mercurial, Bazaar, and Darcs, the client does not only extract the latest version of the file snapshot, but completely mirrors (clone) the code repository.

        In this way, if any server used for collaborative work fails, any mirrored local warehouse can be used to recover afterwards. Because every cloning operation is actually a complete backup of the code repository.

3. Introduction to Git

Official website: https://git-scm.com

(I. Overview

        Git is a distributed version control software originally created by Linus Torvalds and released under the GPL in 2005. It was originally designed to better manage Linux kernel development.

Git official Chinese manual https://git-scm.com/book/zh/v2

(2) Git development history

        Starting in 2002, Linus Torvalds decided to use BitKeeper as the main version control system for the Linux kernel to maintain code. Because BitKeeper is proprietary software, this decision has long been met with skepticism in the community. In the Linux community, especially Richard Stallman and members of the Free Software Foundation, they advocate the use of open source software as the version control system for the Linux core. Linus Torvalds had considered using off-the-shelf software as a version control system (such as Monotone), but these software had some problems, especially poor performance. Off-the-shelf solutions, such as CVS's architecture, were criticized by Linus Torvalds.

        In 2005, Andrew Chuijiu wrote a simple program that could connect to BitKeeper's repository. BitKeeper copyright owner Larry McEvoy believed that Andrew Chuijiu had reverse-engineered the protocol used internally by BitKeeper and decided to withdraw the free use of BitKeeper. authorization. The Linux kernel development team held discussions with BitMover, but could not resolve their differences. Linus Torvalds decided to develop his own version control system to replace BitKeeper, and wrote the first git version in ten days.

        Git is an open source distributed version control system used to

        An original Git version warehouse allows other hosts to clone this original version warehouse, so that a Git version warehouse can be distributed to different hosts at the same time, and the version library of each host is the same, and there is no primary or secondary distinction. It greatly ensures the security of data and enables users to choose the Git server to push files independently. In fact, deploying a Git server is a very simple matter.

        As mentioned above, as a distributed version control system, there is no such concept as a main library in Git. Each copied library can be used independently, and any inconsistencies between the two libraries can be merged.

        GitHub is a hosting platform for open source and private software projects. Because it only supports Git as the only version library format for hosting, it is named GitHub. GitHub can provide user space to create Git repositories and save some of the user's data documents or codes.

        As an open source code repository and version control system, GitHub currently has more than 1.4 million developer users. As more applications move to the cloud, GitHub has become the go-to method for managing software development and discovering existing code.

        GitHub can host various Git repositories and provide a web interface, but unlike other services like SourceForge or Google Code, GitHub's unique selling point is the ease of branching from another project. Contributing code to a project is very simple: first click the "fork" button on the project site, then check out the code and add the modifications to the code base you just forked, and finally take responsibility for the project through the built-in "pull request" mechanism People apply for code merging.

        Gitlab is a Git-based project management software for open source projects of warehouse management systems. Use Git as a code management tool and build a web service based on this.

        Git, Gitlab, and Github are all based on Git and can be said to be derivatives of Git.

(3) Git functional characteristics

Clone database version: Clone the database (including code and version information) from the server to the local machine;

Submit the code: Submit the code on the branch you created on this machine;

Merge branches: merge branches on this machine;

Pull merge branch: Create a new branch, fetch the latest version of the code on the server, and then merge it with your main branch;

Code conflict resolution: A general method for resolving conflicts between developers. Developers can use the pull command to resolve conflicts. After the conflicts are resolved, patches can be submitted to the main developer.

4. GIT installation and deployment

(1) Case environment

IP address

CPU name

Role

192.168.100.131

git server

Git server

192.168.100.132

git

Git client

(2) Install git

1. Initial environment

[root@gitclient ~]# systemctl stop firewalld

[root@gitclient ~]# iptables -F

[root@gitclient ~]# setenforce 0

Gitserver server:

Git client:

2. Install Git with yum

# centos comes with git

[root@gitclient ~]# rpm -q git

 # installation method

[root@gitclient ~]# yum -y install git

3. Compile and install

Compile and install to install a newer version of git

Git download address:  https://github.com/git/git/releases

# Install dependencies

[root@gitclient ~]# yum -y install curl-devel expat-devel gettext-devel  openssl-devel zlib-devel

# Compile and install—server and client

[root@gitclient ~]# wget https://github.com/git/git/archive/v2.22.0.tar.gz

[root@gitclient ~]# tar xf git-2.22.0.tar.gz -C /usr/src/

[root@gitclient ~]# cd /usr/src/git-2.22.0/

[root@git git-2.22.0]# make configure

[root@git git-2.22.0]# ./configure --prefix=/usr/local/git && make && make install

[root@git git-2.22.0]# ln -sf /usr/local/git/bin/git /usr/bin/

[root@git git-2.22.0]# git --version

4. Configuration before running Git for the first time

command set

git config --global user.name "huyang" #Configure git user

git config --global user.email "[email protected]" #Configure git to use email

git config --global color.ui true #Syntax highlighting

git config --list #View global configuration

Configuration process

[root@gitclient ~]# git config --global user.name "crushlinux" #Configure git user

[root@gitclient ~]# git config --global user.email "[email protected]" #Configure git to use email

[root@gitclient ~]# git config --global color.ui true #Syntax highlighting

[root@gitclient ~]# git config --list #View global configuration

Generated configuration file

[root@gitclient ~]# cat .gitconfig

Get help

If you need help when using Git, there are three ways to find the manual for Git commands:

For example, to get a manual for a configuration command, execute

[root@gitclient ~]# git help config

[root@gitclient ~]# git config --help

[root@gitclient ~]# man git config

5. Initialize and obtain the Git repository

Git server operations

[root@gitserver ~]# mkdir git_data.git

[root@gitserver ~]# cd git_data.git/

[root@gitserver git_data.git]# git --bare init

 Git client operations

[root@gitclient ~]# ssh-keygen

[root@gitclient ~]# ssh-copy-id [email protected]

[root@gitclient ~]# git clone [email protected]:/root/git_data.git

[root@gitclient ~]# cd git_data/

[root@git git_data]# git status

(3) Git command routine operations

1. Description of common commands

Order

Command description

add

Add file content to the staging area (index)

bisect

Locate changes that introduce bugs through binary search

branch

List, create or delete branches

checkout

Checkout a branch or path into the workspace

clone

Clone a repository to a new directory

commit

Record changes to the repository (local)

diff

Show differences between commits, between commits and workspaces, etc.

fetch

Download objects and references from another repository

grep

Output lines matching pattern

init

Create an empty

Git

Repository or reinitialize an existing repository

log

Show commit log

merge

Merge two or more development histories

mv

Move or rename a file, directory or symbolic link

pull

Get and merge another repository or a local branch

push

Update remote references and related objects

fox

Local commits are moved to the updated upstream branch

reset

Reset the current HEAD to the specified state

rm

Remove files from workspace and index

show

Display various types of objects

status

Show workspace status

tag

Create, list, delete, or verify a GPG-signed tag object

Common operation diagrams

File status change cycle

2. Create files

[root@git git_data]# touch README

[root@git git_data]# git status

[root@git git_data]# git add ./*

[root@git git_data]# git status

[root@git git_data]# tree .git/

Commit from workspace to local repository

[root@git git_data]# git commit -m 'first commit'

Check the status of git

[root@git git_data]# git status

[root@git git_data]# git branch --unset-upstream

[root@git git_data]# git status

[root@git git_data]# tree  .git/

Submit to remote warehouse

[root@git git_data]# git push [email protected]:/root/git_data.git

3. Add new files

git add   #Add to the staging area

git commit   #Submit git warehouse -m followed by comment information, the content of which is the description of this submission, so that you or others can view it easily.

Modify or delete original files

normal method

git add

git commit

Easy way

git commit -a -m "Comment information"

-a means submit directly

4. Delete files in git

Command description:

Data that has not been added to the temporary storage area can be deleted directly by rm.

Data has been added to the staging area:

git rm --cached database 

#Remove files from the tracking list of the git staging area (the data files in the current working directory will not be deleted)

git rm -f database

#Delete file data from the git staging area and working directory together

Operation command:

#Create new file

[root@git git_data]# touch 123

[root@git git_data]# git status

[root@git git_data]# git add 123

[root@git git_data]# git status

# Delete Files

[root@git git_data]# rm -f 123

[root@git git_data]# ls

[root@git git_data]# git status

[root@git git_data]# git reset HEAD ./*

[root@git git_data]# git status

[root@git git_data]# git rm 123

[root@git git_data]# git status

5. Rename the temporary storage area data

Data that has not been added to the temporary storage area can be renamed directly with mv/rename.

Data has been added to the staging area:

Operation command

[root@git git_data]# git mv README NOTICE

[root@git git_data]# git status

[root@git git_data]# ls

6. View history

• git log #View commit history

• git log -2 #View recent records

• git log -p -1 #-p displays the content differences of each commit, for example, only view the latest difference

• git log --stat -2 #--stat briefly displays the number of lines added and changed in the data, so that you can see the modified content in the submission, the number of lines added or moved to the file, and at the end list a summary of all added and deleted lines information

• git log --pretty=oneline #--pretty displays commit history information in different formats

• git log --pretty=fuller -2 #Output the history of commits in more detailed mode

• git log --pretty=fomat:"%h %cn" #View the short SHA-1 hash string of all current submission records and the name of the submitter.

Use the format parameter to specify the specific output format

Format

illustrate

%s

Submit instructions.

%cd

Submission date.

%an

Author's name.

%cn

Submitter's name.

%ce

Submitter's email.

%H

Submit the complete SHA-1 hash string of the object.

%h

A short SHA-1 hash string of the submitted object.

%T

The complete SHA-1 hash string of the tree object.

%t

The short SHA-1 hash string of the tree object.

%P

The complete SHA-1 hash string of the parent object.

%p

The short SHA-1 hash string of the parent object.

%ad

Author's revision time.

Operation command

[root@git git_data]# git log

7. Restore historical data

        There is a version pointer called HEAD in the Git service program. When the user applies to restore data, it actually points the HEAD pointer to a specific submitted version. However, because Git is a distributed version control system, in order to avoid historical record conflicts, SHA-1 is used to calculate a hexadecimal hash string to distinguish each submission version. In addition, the default HEAD version pointer will point to the most recent submission version record, and the previous submission version will be called HEAD^, as above The previous version will be called HEAD^^. Of course, HEAD~5 is generally used to represent the fifth submitted version from the top.

git reset --hard   hash

git reset --hard HEAD^ #Restore the previous historical submission version

git reset --hard 3de15d4 #After finding the SHA-1 value of the historical restore point, you can restore it (if the value is not written in full, the system will automatically match it)

Operation command

[root@git git_data]# vim NOTICE

[root@git git_data]# cat NOTICE

[root@git git_data]# git commit -a -m 'test'

[root@git git_data]# git log

[root@git git_data]# git reset --hard 6a71d

[root@git git_data]# ls

[root@git git_data]# cat README

8. Restore future data

        What is future data? That is, you restored the historical data, but you regretted it and wanted to undo the changes, but git log can no longer find this version.

git reflog #View future historical update points

Operation command

[root@git git_data]# git reflog

[root@git git_data]# git reset --hard 94d46

[root@git git_data]# cat NOTICE

9、标签使用

前面回滚使用的是一串字符串,又长又难记。

git tag v1.0   #当前提交内容打一个标签(方便快速回滚),每次提交都可以打个tag。

git tag          #查看当前所有的标签

git show v1.0   #查看当前1.0版本的详细信息

git tag v1.2 -m "version 1.2 release is test"  #创建带有说明的标签,-a指定标签名字,-m指定说明文字

git tag -d v1.0   #我们为同一个提交版本设置了两次标签,删除之前的v1.0

操作命令

[root@git git_data]# git reset --hard 6a71d

[root@git git_data]# git tag

[root@git git_data]# git tag v20230829

[root@git git_data]# git tag

[root@git git_data]# git reset --hard v20230829

10、对比数据

git diff可以对比当前文件与仓库已保存文件的区别,知道了对NOTICE作了什么修改

后,再把它提交到仓库就放⼼多了。

[root@git git_data]# echo "hello,gitserver" >> README

[root@git git_data]# git diff README

(四) 分支结构

        在实际的项目开发中,尽量保证master分支稳定,仅用于发布新版本,平时不要随便直接修改master分支里面的数据文件。

        那在哪干活呢?干活都在dev分支上。每个人从dev分支创建自己个人分支,开发完合并到dev分支,最后dev分支合并到master分支。所以团队的合作分支看起来会像下图那样。

[root@git git_data]# git branch linux

[root@git git_data]# git branch

[root@git git_data]# git checkout linux

[root@git git_data]# git branch

在linux分支进行修改

[root@git git_data]# cat README

[root@git git_data]# echo "hello,github" >> README

[root@git git_data]# git add .

[root@git git_data]# git commit -m "github"

[root@git git_data]# git status

回到master分支

[root@git git_data]# git checkout master

[root@git git_data]# cat README

[root@git git_data]# git log -1

合并代码

[root@git git_data]# git merge linux

[root@git git_data]# git status

[root@git git_data]# cat README

2、合并失败解决

模拟冲突,在文件的同一行做不同修改

在master 分支进行修改

[root@git git_data]# cat README

[root@git git_data]# echo "hello.error" >> README

[root@git git_data]# git commit -a -m "master error"

切换到linux分支

[root@git git_data]# git checkout linux

[root@git git_data]# cat README

[root@git git_data]# echo "hello,linux" >> README

[root@git git_data]# git commit -a -m "linux"

回到master分区,进行合并,出现冲突

[root@git git_data]# git checkout master

[root@git git_data]# git merge linux

解决冲突

[root@git git_data]# vim README

# 手工解决冲突

[root@git git_data]# git commit -a -m "master merge"

3、删除分支

        因为之前已经合并了linux分支,所以现在看到它在列表中。 在这个列表中分支名字前没有 * 号的分支通常可以使用 git branch -d 删除掉;你已经将它们的工作整合到了另一个分支,所以并不会失去任何东西。

查看所有包含未合并工作的分支,可以运行 git branch --no-merged:

[root@git git_data]# git branch --no-merged

[root@git git_data]# git branch -d linux

[root@git git_data]# git branch

        如果真的想要删除分支并丢掉那些工作,如同帮助信息里所指出的,可以使用 -D 选项强制删除它。

(五)windwos上Git的使用

windows 上git软件网站 https://git-for-windows.github.io

软件下载地址:

https://github.com/git-for-windows/git/releases/download/v2.22.0.windows.1/Git-2.22.0-64-bit.exe

创建新的仓库

 定义仓库的路径

添加用户信息(在 git bash中)

Crushlinux@DESKTOP-5JDO87L MINGW64 ~

$ git config  --global user.email "[email protected]"

Crushlinux@DESKTOP-5JDO87L MINGW64 ~

$ git config  --global user.name "crushlinux"

在git Gui 中添加用户信息,添加一次就可

 在页面中将数据配置好即可使用

五、GitHub托管服务

        Github顾名思义是一个Git版本库的托管服务,是目前全球最大的软件仓库,拥有上百万的开发者用户,也是软件开发和寻找资源的最佳途径,Github不仅可以托管各种Git版本仓库,还拥有了更美观的Web界面,您的代码文件可以被任何人克隆,使得开发者为开源项贡献代码变得更加容易,当然也可以付费购买私有库,这样高性价比的私有库真的是帮助到了很多团队和企业。

(一)注册GitHub

浏览器访问github官网 : https://github.com/ ,点击Sign up 进行注册

填写个人信息,进行注册

输入验证码

输入信息

选择如下:

选择仓库类型,默认免费,点击底下Continue注册

用户创建完成,可以创建新的项目

添加密钥

在github上添加一个新的ssh密钥

 

(二)获取Linux主机上的密钥

[root@gitserver ~]# ssh-keygen

[root@gitserver ~]# cat .ssh/id_rsa.pub

(三)获得Windows主机密钥的方法

Crushlinux@DESKTOP-5JDO87L MINGW64 ~

$ ssh-keygen.exe

Generating public/private rsa key pair.

Enter file in which to save the key (/c/Users/Crushlinux/.ssh/id_rsa):

Created directory '/c/Users/Crushlinux/.ssh'.

Enter passphrase (empty for no passphrase):

Enter same passphrase again:

Your identification has been saved in /c/Users/Crushlinux/.ssh/id_rsa.

Your public key has been saved in /c/Users/Crushlinux/.ssh/id_rsa.pub.

The key fingerprint is:

SHA256:IHhPxTKtHlNZx0kqvob90V5tN8IM0VQT4hZ7yWBkznc Crushlinux@DESKTOP-5JDO87L

The key's randomart image is:

+---[RSA 3072]----+

|       o.o.o+X.+.|

|   .  o.=  o@ * o|

|  . o o=. .. B =E|

|   . ++o .  o o .|

|     ..oS  .     |

|      .o . .+  . |

|      . + . .+..+|

|       . . o ...o|

|          . .    |

+----[SHA256]-----+

Crushlinux@DESKTOP-5JDO87L MINGW64 ~

$ cat .ssh/id_rsa.pub

ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAABgQDHSP3L5h8irgkZe3snixWIDa4Q7JBQk0PzAoBQZPB1kgHuK5iZ4hY/mEqr/vegbWHdI0G5tYHRNi1PTnFrDSnjaIaFAoiuZt36aOUPmHpgmMHEV6hE3jeuoanK2LrIepVNvscwu15O8t2/dKc0An+x83miS5PRAojXj3BSQ1EsJ+LWmnl3W/UM6waVujmB7TihLYzPt8x2Mgi8jqvA6pgLwK/wdK+3pldSbTM3D3j8XLt2AXAPnkPa9C7HeUv7Tm905lbalRWmuhC/K2jc+WBXjbwc4xgjlLYtP5JjhGoecZ72CKCVVp1DN7Yz9Nb98rEzEsqTsvfvjVCrssmYDxSP2NRDWB8mkZw5XrKgnFRLuvpql2MPDAthWNYlZXH0RJ9yVHiqAN3E5PRuJhtFbSLJYRMNMmjkmptVwbjUYvpm3W9ZbAeP6z1ueJ9RGc1dwtBnYNN/PZlD/lqvJll+5t2c+V8xkStEEobQFg7aQSc+BU9ZAsdCL79veOHUtfiDh+k= Crushlinux@DESKTOP-5JDO87L

密钥创建完成后进行添加

密钥添加成功

(四)创建仓库

准备工作已经完毕,右上角点击创建一个新的仓库

创建仓库,输入个人信息

(五)在个人主机上进行推送测试

[root@gitserver ~]# git config --global user.email "crushlinux13363571949root@gitserver ~]# git config --global user.name "huyang"

[root@gitserver ~]# git config --list

[root@gitserver ~]# mkdir hy

[root@gitserver ~]# cd hy

[root@gitserver hy]# echo "# test" >> README.md

[root@gitserver hy]# git init

[root@gitserver hy]# git add README.md

[root@gitserver hy]# git commit -m "first

[root@gitserver hy]# git remote add origin [email protected]:huyang-Ada/test1.git

[root@gitserver hy]# git push -u origin master

Web access test:

(6) Create new files and perform pull tests

Edit content:

Click OK

After creating it, click commit below.

Github was added successfully and the pull test was performed.

(7) Pull file test

View directory contents

[root@gitserver hy]# pwd

[root@gitserver crushlinux]# ls

Make a pull

[root@gitserver hy]# git pull

[root@gitserver hy]# ls

[root@gitserver hy]# cat abc.test

Guess you like

Origin blog.csdn.net/2302_77582029/article/details/132568711