1 Git简介
Git是一个开源的分布式版本控制系统,可以有效、高速地处理从很小到非常大的项目版本管理。 也是Linus Torvalds为了帮助管理Linux内核开发而开发的一个开放源码的版本控制软件。
2 Git使用
2.1 Git安装
#在centos-stream
yum install git -y
[root@localhost ~]# yum install git -y
Last metadata expiration check: 2:06:55 ago on Sat 24 Sep 2022 09:30:36 PM CST.
Dependencies resolved.
================================================================================================================================
Package Architecture Version Repository Size
================================================================================================================================
Installing:
git x86_64 2.31.1-2.el9.2 AppStream 124 k
Installing dependencies:
git-core-doc noarch 2.31.1-2.el9.2 AppStream 2.5 M
perl-Error noarch 1:0.17029-7.el9 AppStream 42 k
perl-Git noarch 2.31.1-2.el9.2 AppStream 43 k
perl-TermReadKey x86_64 2.38-11.el9 AppStream 37 k
perl-lib x86_64 0.65-479.el9 AppStream 24 k
Transaction Summary
================================================================================================================================
Install 6 Packages
Total download size: 2.8 M
Installed size: 16 M
Downloading Packages:
(1/6): git-2.31.1-2.el9.2.x86_64.rpm 202 kB/s | 124 kB 00:00
(2/6): perl-Error-0.17029-7.el9.noarch.rpm 55 kB/s | 42 kB 00:00
(3/6): perl-Git-2.31.1-2.el9.2.noarch.rpm 136 kB/s | 43 kB 00:00
(4/6): perl-TermReadKey-2.38-11.el9.x86_64.rpm 109 kB/s | 37 kB 00:00
(5/6): git-core-doc-2.31.1-2.el9.2.noarch.rpm 2.2 MB/s | 2.5 MB 00:01
(6/6): perl-lib-0.65-479.el9.x86_64.rpm 112 kB/s | 24 kB 00:00
--------------------------------------------------------------------------------------------------------------------------------
Total 2.4 MB/s | 2.8 MB 00:01
Running transaction check
Transaction check succeeded.
Running transaction test
Transaction test succeeded.
Running transaction
Preparing : 1/1
Installing : perl-lib-0.65-479.el9.x86_64 1/6
Installing : perl-TermReadKey-2.38-11.el9.x86_64 2/6
Installing : perl-Error-1:0.17029-7.el9.noarch 3/6
Installing : git-core-doc-2.31.1-2.el9.2.noarch 4/6
Installing : perl-Git-2.31.1-2.el9.2.noarch 5/6
Installing : git-2.31.1-2.el9.2.x86_64 6/6
Running scriptlet: git-2.31.1-2.el9.2.x86_64 6/6
Verifying : git-2.31.1-2.el9.2.x86_64 1/6
Verifying : git-core-doc-2.31.1-2.el9.2.noarch 2/6
Verifying : perl-Error-1:0.17029-7.el9.noarch 3/6
Verifying : perl-Git-2.31.1-2.el9.2.noarch 4/6
Verifying : perl-TermReadKey-2.38-11.el9.x86_64 5/6
Verifying : perl-lib-0.65-479.el9.x86_64 6/6
Installed:
git-2.31.1-2.el9.2.x86_64 git-core-doc-2.31.1-2.el9.2.noarch perl-Error-1:0.17029-7.el9.noarch
perl-Git-2.31.1-2.el9.2.noarch perl-TermReadKey-2.38-11.el9.x86_64 perl-lib-0.65-479.el9.x86_64
Complete!
[root@localhost ~]# git --version
git version 2.31.1
2.2 Git设置用户信息
git config --global user.name "xiaoming"
git config --global user.email "mmx@qq.com"
# 设置用户名
[root@localhost ~]# git config --global user.name "xiaoming"
# 设置用户邮箱
[root@localhost ~]# git config --global user.email "mmx@qq.com"
[root@localhost ~]# cat .gitconfig
[user]
name = xiaoming
email = mmx@qq.com
2.3 Git基础操作
2.3.1 git创建版本库
mkdir git_test
git init
[root@localhost ~]# mkdir git_test
[root@localhost git_test]# git init
hint: Using 'master' as the name for the initial branch. This default branch name
hint: is subject to change. To configure the initial branch name to use in all
hint: of your new repositories, which will suppress this warning, call:
hint:
hint: git config --global init.defaultBranch <name>
hint:
hint: Names commonly chosen instead of 'master' are 'main', 'trunk' and
hint: 'development'. The just-created branch can be renamed via this command:
hint:
hint: git branch -m <name>
Initialized empty Git repository in /root/git_test/.git/
2.3.2 git上传版本信息
git add 文件名
git commit -m "版本说明"
[root@localhost git_test (master #)]# echo v1 > mmx_v1
[root@localhost git_test (master #)]# git add mmx_v1
[root@localhost git_test (master +)]# git status
On branch master
No commits yet
Changes to be committed:
(use "git rm --cached <file>..." to unstage)
new file: mmx_v1
[root@localhost git_test (master +)]# git commit -m "v1"
[master (root-commit) 639391e] v1
1 file changed, 1 insertion(+)
create mode 100644 mmx_v1
2.3.3 git 查看状态
git status
git log
# 未进行追踪的文件
[root@localhost git_test (master)]# git status
On branch master
Untracked files:
(use "git add <file>..." to include in what will be committed)
hello
nothing added to commit but untracked files present (use "git add" to track)
# 对文件进行追踪
[root@localhost git_test (master)]# git add hello
[root@localhost git_test (master +)]# git status
On branch master
Changes to be committed:
(use "git restore --staged <file>..." to unstage)
new file: hello
# 将追踪文件进行提交
[root@localhost git_test (master +)]# git commit -m "v2"
[master 495e74c] v2
1 file changed, 0 insertions(+), 0 deletions(-)
create mode 100644 hello
[root@localhost git_test (master)]# git status
On branch master
nothing to commit, working tree clean
# 查看版本信息
[root@localhost git_test (master)]# git log
commit 495e74c600928e109328ce6d3c9ac352f5ed9b18 (HEAD -> master)
Author: xiaoming <mmx@qq.com>
Date: Sat Sep 24 23:51:39 2022 +0800
v2
commit 639391e69e391623bc3b810867c12450439b01f5
Author: xiaoming <mmx@qq.com>
Date: Sat Sep 24 23:46:11 2022 +0800
v1
2.4 更舒服的使用git
添加全局变量更方便的查看git状态
[root@localhost ~]# cat gitrc
# 更加舒服的使用git
source /usr/share/git-core/contrib/completion/git-prompt.sh
export GIT_PS1_SHOWDIRTYSTATE=true
export GIT_PS1_SHWODWUNTRACKDFILES=true
export PS1='\[\033[01;35m\][\[\033[01;32m\]\h\[\033[01;34m\] \W$(declare -F __git_ps1 &>/dev/null && __git_ps1 " (%s)")\[\033[01;35m\]]\[\033[01;34m\]#\[\033[00m\] '
2.5 版本回退
可以通过git reset回退到之前的git
2.5.1 显示log
git log
[root@localhost git_test (master)]# git log
commit a2b6751f53ff492663810c122da66380420ea9db (HEAD -> master)
Author: xiaoming <mmx@qq.com>
Date: Sun Sep 25 00:01:11 2022 +0800
v3
commit 495e74c600928e109328ce6d3c9ac352f5ed9b18
Author: xiaoming <mmx@qq.com>
Date: Sat Sep 24 23:51:39 2022 +0800
v2
commit 639391e69e391623bc3b810867c12450439b01f5
Author: xiaoming <mmx@qq.com>
Date: Sat Sep 24 23:46:11 2022 +0800
v1
git log --pretty=oneline
[root@localhost git_test (master)]# git log --pretty=oneline
a2b6751f53ff492663810c122da66380420ea9db (HEAD -> master) v3
495e74c600928e109328ce6d3c9ac352f5ed9b18 v2
639391e69e391623bc3b810867c12450439b01f5 v1
2.5.2 查看历史修改
[root@localhost git_test (master)]# git reflog
a2b6751 (HEAD -> master) HEAD@{0}: commit: v3
495e74c HEAD@{1}: commit: v2
639391e HEAD@{2}: commit (initial): v1
2.5.2 回退指定版本
回退到指定版本
[root@localhost git_test (master)]# git reflog
a2b6751 (HEAD -> master) HEAD@{0}: commit: v3
495e74c HEAD@{1}: commit: v2
639391e HEAD@{2}: commit (initial): v1
# 回退到v1
[root@localhost git_test (master)]# git reset --hard HEAD@{2}
HEAD is now at 639391e v1
[root@localhost git_test (master)]# ls
mmx_v1
# 回退到v2
[root@localhost git_test (master)]# git reset --hard HEAD@{1}
HEAD is now at a2b6751 v3
[root@localhost git_test (master)]# ls
good hello mmx_v1
# 回退到v3
[root@localhost git_test (master)]# git reset --hard HEAD@{0}
HEAD is now at a2b6751 v3
[root@localhost git_test (master)]# ls
good hello mmx_v1
2.6 分支管理
2.6.1 查看分支
git brance
[root@localhost git_test (master)]# git branch
* master
2.6.2 新建分支
创建分支
git branch 分支名
# 创建新分支
[root@localhost git_test (master)]# git branch mmx
[root@localhost git_test (master)]# git branch
* master
mmx
创建并切换分支
git branch -M 分支名
# 创建并切换分支
[root@localhost git_test (master)]# git branch -M mmx01
[root@localhost git_test (mmx01)]# git branch
mmx
* mmx01
2.6.3 切换分支
git switch 分支名
[root@localhost git_test (mmx01)]# git branch
mmx
* mmx01
[root@localhost git_test (mmx01)]# git switch mmx
Switched to branch 'mmx'
2.6.4 分支合并
git merge 分支名
[root@localhost git_test (mmx01)]# git switch mmx
Switched to branch 'mmx'
[root@localhost git_test (mmx)]# echo hello mmx >> hello
[root@localhost git_test (mmx *)]# cat hello
hello mmx
[root@localhost git_test (mmx *)]# git add hello
[root@localhost git_test (mmx +)]# git commit -a -m "new hello"
[mmx 89735ba] new hello
1 file changed, 1 insertion(+)
[root@localhost git_test (mmx)]# git switch mmx01
Switched to branch 'mmx01'
[root@localhost git_test (mmx01)]# git merge mmx
Updating a2b6751..89735ba
Fast-forward
hello | 1 +
1 file changed, 1 insertion(+)
2.7 删除分支
git branch -d 分支名称
[root@localhost git_test (mmx01)]# git branch
mmx
* mmx01
[root@localhost git_test (mmx01)]# git branch -d mmx
Deleted branch mmx (was 89735ba).
[root@localhost git_test (mmx01)]# git branch
* mmx01
2.8 修改分支名
git branch -m 分支名 新分支名
[root@localhost git_test (mmx01)]# git branch
* mmx01
[root@localhost git_test (mmx01)]# git branch -m mmx01 master
[root@localhost git_test (master)]# git branch
* master
3 远程git仓库
3.1 github的使用
3.1.1 github简介
GitHub于2008年4月10日正式上线,除了Git代码仓库托管及基本的Web管理界面以外,还提供了订阅、讨论组、文本渲染、在线文件编辑器、协作图谱(报表)、代码片段分享(Gist)等功能。目前,其注册用户已经超过350万,托管版本数量也是非常之多,其中不乏知名开源项目Ruby on Rails、jQuery、python等。 2018年6月4日,微软宣布,通过75亿美元的股票交易收购代码托管平台GitHub。
3.1.2 进入官网
https://github.com
3.1.3 新建仓库
Repository name填写仓库名称
Description填写仓库描述信息
通过public或private将仓库设置为公有/私有
3.1.4 配置ssh密钥
生成ssh-keygen
ssh-keygen -t rsa -f github\rsa -C "mmx"
[root@localhost .ssh]# ssh-keygen -t rsa -f github01
Generating public/private rsa key pair.
Enter passphrase (empty for no passphrase):
Enter same passphrase again:
Your identification has been saved in github01
Your public key has been saved in github01.pub
The key fingerprint is:
SHA256:n5txbVbXQN/O+cAShapNDOjq8F1LQ64Jbr3efRAhWkI root@localhost.localdomain
The key's randomart image is:
+---[RSA 3072]----+
| .E. .o |
| o + . .o ..|
| . + + o. . o|
| o . = o +o|
| . oS+ .. o.*|
| . o *.o. o oo|
| = + = o+.. + .|
| = =... =.o |
| . .o.. +. |
+----[SHA256]-----+
[root@localhost .ssh]# ls
github01 github01.pub
将ssh公钥加入git
3.1.5 上传版本信息
创建/上传新版本
touch ansible.cfg
touch inventory
touch playbook.yml
git init
git add -A
git commit -m "ansible project"
git branch -M mmx
git remote add origin git@github.com:mmx-good/test.git
git push -u origin mmx
[root@localhost test]# touch ansible.cfg
[root@localhost test]# touch inventory
[root@localhost test]# touch playbook.yml
[root@localhost test]# git init
hint: Using 'master' as the name for the initial branch. This default branch name
hint: is subject to change. To configure the initial branch name to use in all
hint: of your new repositories, which will suppress this warning, call:
hint:
hint: git config --global init.defaultBranch <name>
hint:
hint: Names commonly chosen instead of 'master' are 'main', 'trunk' and
hint: 'development'. The just-created branch can be renamed via this command:
hint:
hint: git branch -m <name>
Initialized empty Git repository in /root/test/.git/
[root@localhost test (master #)]# git add -A
[root@localhost test (master +)]# git commit -m "ansible project"
[master (root-commit) 7668a2a] ansible project
3 files changed, 2 insertions(+)
create mode 100644 ansible.cfg
create mode 100644 inventory
create mode 100644 playbook.yml
[root@localhost test (master)]# git branch -M mmx
[root@localhost test (mmx)]# git remote add origin git@github.com:mmx-good/test.git
[root@localhost test (mmx)]# git push -u origin mmx
Enumerating objects: 4, done.
Counting objects: 100% (4/4), done.
Delta compression using up to 32 threads
Compressing objects: 100% (2/2), done.
Writing objects: 100% (4/4), 294 bytes | 294.00 KiB/s, done.
Total 4 (delta 0), reused 0 (delta 0), pack-reused 0
To github.com:mmx-good/test.git
* [new branch] mmx -> mmx
Branch 'mmx' set up to track remote branch 'mmx' from 'origin'.
使用现有git
git remote add origin git@github.com:mmx-good/test.git
git branch -M main
git push -u origin main
[root@localhost test (mmx)]# git add hello
[root@localhost test (mmx +)]# git commit -m "add hello file"
[mmx a52c2d7] add hello file
1 file changed, 0 insertions(+), 0 deletions(-)
create mode 100644 hello
[root@localhost test (mmx)]# git push -u origin mmx
Enumerating objects: 3, done.
Counting objects: 100% (3/3), done.
Delta compression using up to 32 threads
Compressing objects: 100% (2/2), done.
Writing objects: 100% (2/2), 225 bytes | 225.00 KiB/s, done.
Total 2 (delta 1), reused 0 (delta 0), pack-reused 0
remote: Resolving deltas: 100% (1/1), completed with 1 local object.
To github.com:mmx-good/test.git
7668a2a..a52c2d7 mmx -> mmx
Branch 'mmx' set up to track remote branch 'mmx' from 'origin'.
3.2 gitee的使用
3.2.1 gitee简介
Gitee 是开源中国社区2013年推出的基于 Git 的代码托管服务,目前已经成为国内最大的代码托管平台,致力于为国内开发者提供优质稳定的托管服务。
3.2.2 进入官网
https://gitee.com/
3.2.3 新建仓库
3.2.4 配置ssh密钥
管理 --> 公钥管理 --> 添加部署公钥
3.2.5 上传版本信息
touch A
touch B
touch C
git init
git add -A
git commit -m "ABC"
git branch -M mmx
git remote add origin git@gitee.com:mmx-good/test.git
git push -u origin mmx
[root@localhost test02]# git init
hint: Using 'master' as the name for the initial branch. This default branch name
hint: is subject to change. To configure the initial branch name to use in all
hint: of your new repositories, which will suppress this warning, call:
hint:
hint: git config --global init.defaultBranch <name>
hint:
hint: Names commonly chosen instead of 'master' are 'main', 'trunk' and
hint: 'development'. The just-created branch can be renamed via this command:
hint:
hint: git branch -m <name>
Initialized empty Git repository in /root/test02/.git/
[root@localhost test02 (master #)]# touch A B C
[root@localhost test02 (master #)]# ls
A B C
[root@localhost test02 (master #)]# git add -A
[root@localhost test02 (master +)]# git commit -m "ABC"
[master (root-commit) bd5cf6d] ABC
3 files changed, 0 insertions(+), 0 deletions(-)
create mode 100644 A
create mode 100644 B
create mode 100644 C
[root@localhost test02 (master)]# git branch -M mmx
[root@localhost test02 (mmx)]# git remote add origin git@gitee.com:mmx-good/test.git
[root@localhost test02 (mmx)]# git push -u origin mmx
Enumerating objects: 3, done.
Counting objects: 100% (3/3), done.
Delta compression using up to 32 threads
Compressing objects: 100% (2/2), done.
Writing objects: 100% (3/3), 202 bytes | 202.00 KiB/s, done.
Total 3 (delta 0), reused 0 (delta 0), pack-reused 0
remote: Powered by GITEE.COM [GNK-6.4]
remote: Create a pull request for 'mmx' on Gitee by visiting:
remote: https://gitee.com/mmx-good/test/pull/new/mmx-good:mmx...mmx-good:master
To gitee.com:mmx-good/test.git
* [new branch] mmx -> mmx
Branch 'mmx' set up to track remote branch 'mmx' from 'origin'.