Git 快速入门
文章目录
- Git 快速入门
- 一、代码托管平台(远程仓库)
- 二、安装Git
- 三、Git的命令实践
- Git 的四个区域
- Git 管理代码的3个场景
- Git 工作区的理念
- Git 工作区的生命周期
- Git 版本回退
- Git 文件重命名
- Git查看版本提交日志
- Git Stash
- Git分支
- Git标签
- 四、创建码云代码仓库
- 五、配置Linux连接码云的代码仓库
- 六、实践代码仓库推送
- 七、Gitlab 安装搭建
- yum安装方式
- rpm 包安装方式
- Gitlab 汉化配置
一、代码托管平台(远程仓库)
git 是一个分布式版本控制系统,同一个git仓库可以分布在不同的机器上,但是开发团队必须保证在同一个网络中,且必须有一个项目的原始版本,通常的办法就是让一台电脑充当服务器的角色,每天24小时开机,其他每个人都可以在这台"服务器"仓库里克隆一份代码到自己的电脑上。
并且也可以把各自的代码提交到代码仓库里,也能从代码仓库拉取别人的提交。
这样的代码仓库服务器,我们可以自由的搭建,也可以选择使用免费的托管平台。
Git代码托管平台,首先推荐的是Github,世界范围内的开发者都在使用Github托管代码,可以找到大量优秀的开源项目,缺点就是访问可能会卡一点。
其次选择的就是Gitee,国内的代码托管平台,以及自建Gitlab服务器。
1.国外的代码托管平台,全球最大的程序员交友平台
GitHub官网地址:https://github.com
2.国内的代码托管平台
Gitee官网地址:https://gitee.com/
二、安装Git
安装Git的方式
Git有多种安装方式:
- 原生的纯命令实行,linux学习的操作
- 有很多的GUI图形管理Git的工具
yum install -y git
修改环境变量,定制Git的环境
git config 控制 Git的行为,来定义环境变量
它提供三个环境参数
当时用如下命令,配置Git的环境变量时,不同的参数,会写入信息到不同的文件中
git config --global xxx.xxx
–system 针对任意登录该linux系统的用户都生效,Git的配置信息,写入到/etc/gitconfig
–global 全局,只针对当前登录的用户生效,Git配置写入到 ~/.config/git/config(用的最多的)
–local 本地,(只针对一个文件夹生效),/learn/database/.git/config
用户Git信息配置
[root@Git ~]# git config --global user.name "guan12319" // 配置用户名
[root@Git ~]# git config --global user.email "guan12319@qq.com" // 配置邮箱
[root@Git ~]# git config --global color.ui true //开启Git的颜色区分
[root@Git ~]# cat ~/.gitconfig
[user]name = guan12319email = guan12319@qq.com
[color]ui = true
[root@Git ~]# git config --global --list
user.name=guan12319
user.email=guan12319@qq.com
color.ui=true
Git 配置相关命令
yum install git -y // 安装Git
git --version // 查看Git版本
git config --system --list // 查看系统所有linux用户的通用配置,此命令检查 /etcgitconfig
git config --global --list // 查看当前Linux用户的配置,检查~/.gitconfig 文件
git config --local --list // 查看git目录中的仓库配置文件,.gitconfig文件
git config --global user.name "guan12319" // 配置当前linux用户全局用户名,这台机器所有git仓库都会用这个配置
git config --global user.email "guan12319@qq.com" // 配置当前linux用户全局邮箱
git config --global color.ui true //配置Git语法高亮提示
git config --list // 列出Git能找到的所有配置,从不同文件中读取所有结果
git config user.name // 列出Git某一项配置
git help // 获取git帮助
man git // man手册
git help config // 获取config命令的手册
[root@Git ~]# git config --system user.name "guan12319"
[root@Git ~]# git config --system user.email "guan12319@qq.com"
[root@Git ~]# cat /etc/gitconfig
[user]name = guan12319email = guan12319@qq.com
[root@Git ~]# git config --system --list
user.name=guan12319
user.email=guan12319@qq.com
[root@Git ~]#
三、Git的命令实践
- 工作目录,就是一个linux文件夹
- git status 查看暂存区状态
- git本地仓库,就是一个git的版本库,说白了就是代码目录下的一个,git文件夹,这就是管理文件变动信息的目录,也就是git核心的本地仓库
- (这个本地库,作用是,记录所有对文件的修改,删除动作,git都会记录下来,以便于历史回退,追踪信息)
Git 的四个区域
Git 管理代码的3个场景
1.本地已经有一个代码,需要用Git管理
(程序员已经吧开发好的程序,发给了运维,运维要针对这个目录,进行git初始化管理)
ls /data/nginx_web/
cd/data/nginx_web/
git init #就是对git初始化,生成.git目录
2.本地没有代码,要新建一个Git版本仓库
(程序员小王要开始写代码了,并且从开始就用git进行版本管理)
mkdir /my_code/
cd /my_code/ && git init # 只要执行git init就表示git初始化开始,该目录已经被git管理了
touch hello.shxxxxx(以后代码的变动,都会被git管理,记录)
3.本地没有代码,也没有Git版本仓库,去GitHub代码托管平台下载一个Git版本代码库
git clone https://github.com/xxxx/xxx_code
Git clone命令会去github平台,下載一个已经被git管理的代码仓库了
实际操作
- 场景1:
[root@Git ~]# cd /
[root@Git /]# mkdir test_git
[root@Git /]# cd ./test_git/
[root@Git test_git]# echo "echo hello git " > hello.sh
[root@Git test_git]# cat hello.sh
echo hello git
[root@Git test_git]# bash hello.sh
hello git
[root@Git test_git]# pwd
/test_git
[root@Git test_git]# ls -a
. .. hello.sh
用Git管理目录
[root@Git test_git]# 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 /test_git/.git/
[root@Git test_git]# ls -a
. .. .git hello.sh
- 场景2:
# 直接用git生成一个本地仓库,名字叫 test_git01
[root@Git tmp]# git init test_git01
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 /tmp/test_git01/.git/
[root@Git tmp]# ls
test_git01
[root@Git tmp]# cd test_git01/
[root@Git test_git01]# ls -a
. .. .git
[root@Git test_git01]#
- 场景3
[root@Git test_git01]# git clone https://github.com/pallets/flask.git
Cloning into 'flask'...
remote: Enumerating objects: 24112, done.
remote: Counting objects: 100% (724/724), done.
remote: Compressing objects: 100% (307/307), done.
remote: Total 24112 (delta 401), reused 645 (delta 388), pack-reused 23388
Receiving objects: 100% (24112/24112), 9.99 MiB | 3.05 MiB/s, done.
Resolving deltas: 100% (16160/16160), done.
[root@Git test_git01]# ls
flask
[root@Git test_git01]# ls -a
. .. flask .git
[root@Git test_git01]# cd flask/
[root@Git flask]# ls
CHANGES.rst docs pyproject.toml src
CODE_OF_CONDUCT.md examples README.rst tests
CONTRIBUTING.rst LICENSE.rst requirements tox.ini
[root@Git flask]# ls -a
. docs .gitignore requirements
.. .editorconfig LICENSE.rst src
CHANGES.rst examples .pre-commit-config.yaml tests
CODE_OF_CONDUCT.md .flake8 pyproject.toml tox.ini
CONTRIBUTING.rst .git README.rst
.devcontainer .github .readthedocs.yaml
[root@Git flask]# git status
On branch main
Your branch is up to date with 'origin/main'.nothing to commit, working tree clean
[root@Git flask]# touch hello.txt
[root@Git flask]# git status
On branch main
Your branch is up to date with 'origin/main'.Untracked files:(use "git add <file>..." to include in what will be committed)hello.txtnothing added to commit but untracked files present (use "git add" to track)
[root@Git flask]# git add .
[root@Git flask]# git status
On branch main
Your branch is up to date with 'origin/main'.Changes to be committed:(use "git restore --staged <file>..." to unstage)new file: hello.txt[root@Git flask]#
Git 工作区的理念
1. git命令 生成一个工作区,也就是git对该文件夹进行管理
[root@Git ~]# mkdir /git_code
[root@Git ~]# cd /git_code
[root@Git git_code]# ls -a
. ..[root@Git git_code]# git init hello_git
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 /git_code/hello_git/.git/
2.查看git工作区的本地仓库
.git这个隐藏文件夹,就是git的本地仓库
[root@Git git_code]# ls -a
. .. hello_git
[root@Git git_code]# cd hello_git/
[root@Git hello_git]# ls -a
. .. .git
[root@Git hello_git]# cd .git/
[root@Git .git]# ls
branches config description HEAD hooks info objects refs
3.通过 tree 命令,查看.git工作区的信息
.git 本地仓库的内容
[root@Git .git]# tree
.
├── branches
├── config // 该Git项目独有的配置文件
├── description
├── HEAD // git的文件指针
├── hooks
│ ├── applypatch-msg.sample
│ ├── commit-msg.sample
│ ├── fsmonitor-watchman.sample
│ ├── post-update.sample
│ ├── pre-applypatch.sample
│ ├── pre-commit.sample
│ ├── pre-merge-commit.sample
│ ├── prepare-commit-msg.sample
│ ├── pre-push.sample
│ ├── pre-rebase.sample
│ ├── pre-receive.sample
│ ├── push-to-checkout.sample
│ └── update.sample
│---index // index 文件,保存暂存区的信息,只有git add 之后才会生成
├── info
│ └── exclude
├── objects
│ ├── info
│ └── pack
└── refs├── heads└── tags9 directories, 17 files
[root@Git .git]#
4.查看工作区的信息(查看文件变动状态,未跟踪,已跟踪)
[root@Git hello_git]# ls -a
. .. .git
[root@Git hello_git]# git status
On branch masterNo commits yetnothing to commit (create/copy files and use "git add" to track)
[root@Git hello_git]#
[root@Git hello_git]# git status
On branch masterNo commits yetnothing to commit (create/copy files and use "git add" to track)
5.在工作区进行文件创建,发生一些变化
[root@Git hello_git]# touch hello.sh
# 此时git会提示你,是否要 git 添加到暂存区
[root@Git hello_git]# git status
On branch masterNo commits yetUntracked files:(use "git add <file>..." to include in what will be committed)hello.shnothing added to commit but untracked files present (use "git add" to track)
6.确认要添加,跟踪这个文件
[root@Git hello_git]# git add .
# git 会询问你是否要提交到本地仓库
[root@Git hello_git]# git status
On branch masterNo commits yetChanges to be committed:(use "git rm --cached <file>..." to unstage)new file: hello.sh
7.确认提交到本地仓库
[root@Git hello_git]# git commit -m "guan12319 first commit"
[master (root-commit) 7cbd4e8] guan12319 first commit1 file changed, 0 insertions(+), 0 deletions(-)create mode 100644 hello.sh
[root@Git hello_git]# git status
On branch master
nothing to commit, working tree clean
Git 工作区的生命周期
1.未跟踪,进入暂存区
git add .
2.进行版本库提交,将暂存区的内容,写入到本地仓库
git commit -m“提交注释"
3.此时文件被修改了,从unmodified状态变更为 modified已经修改的状态
4.再次提交这个被修改的文件,进入暂存区
git add file
5.再次的提交版本
git commit -m "修改了文件"
6.从本地仓库中,删除对某个文件的跟踪
#将文件,回退到未跟踪的状态
git rm --cached 文件名
7.此时对上述的删除动作,可有3个选择
- 直接删除这个文件
rm -rf test.sh
- 撤销你刚才的 git rm 操作
git restore --staged test.sh
- 再次进入跟踪状态,然后 git commit -m 提交
git add .
[root@Git hello_git]# ls
hello_05.sh hello.sh
[root@Git hello_git]# git rm --cached hello.sh
rm 'hello.sh'
[root@Git hello_git]# git status
On branch master
Changes to be committed:(use "git restore --staged <file>..." to unstage)deleted: hello.shUntracked files:(use "git add <file>..." to include in what will be committed)hello.sh[root@Git hello_git]# git restore --staged hello.sh
[root@Git hello_git]# git status
On branch master
nothing to commit, working tree clean
[root@Git hello_git]#
Git 版本回退
版本回退这么多,那应该怎么样回到之前的版本
git reset --hard HEAD^ // 回到上一个版本
# 查看git所记的你每一次版本提交与回退记录的日志
git reflog
# 回退到指定版本
git reset --hard 7cbd4e8
[root@Git hello_git]# git reflog
6431a7b (HEAD -> master) HEAD@{0}: commit: guan12319 third commit
530916c HEAD@{1}: commit: guan12319 second commit
7cbd4e8 HEAD@{2}: commit (initial): guan12319 first commit
[root@Git hello_git]# git reset --hard HEAD^
HEAD is now at 530916c guan12319 second commit
[root@Git hello_git]# git reflog
530916c (HEAD -> master) HEAD@{0}: reset: moving to HEAD^
6431a7b HEAD@{1}: commit: guan12319 third commit
530916c (HEAD -> master) HEAD@{2}: commit: guan12319 second commit
7cbd4e8 HEAD@{3}: commit (initial): guan12319 first commit
[root@Git hello_git]# git reset --hard HEAD^^
fatal: ambiguous argument 'HEAD^^': unknown revision or path not in the working tree.
Use '--' to separate paths from revisions, like this:
'git <command> [<revision>...] -- [<file>...]'
[root@Git hello_git]# git reset --hard HEAD^
HEAD is now at 7cbd4e8 guan12319 first commit
[root@Git hello_git]# git reflog
7cbd4e8 (HEAD -> master) HEAD@{0}: reset: moving to HEAD^
530916c HEAD@{1}: reset: moving to HEAD^
6431a7b HEAD@{2}: commit: guan12319 third commit
530916c HEAD@{3}: commit: guan12319 second commit
7cbd4e8 (HEAD -> master) HEAD@{4}: commit (initial): guan12319 first commit
[root@Git hello_git]# git reset --hard 6431a7b
HEAD is now at 6431a7b guan12319 third commit
[root@Git hello_git]# git reflog
6431a7b (HEAD -> master) HEAD@{0}: reset: moving to 6431a7b
7cbd4e8 HEAD@{1}: reset: moving to HEAD^
530916c HEAD@{2}: reset: moving to HEAD^
6431a7b (HEAD -> master) HEAD@{3}: commit: guan12319 third commit
530916c HEAD@{4}: commit: guan12319 second commit
7cbd4e8 HEAD@{5}: commit (initial): guan12319 first commit
[root@Git hello_git]# ls
hello_05.sh hello.sh
[root@Git hello_git]# git reset --hard 7cbd4e8
HEAD is now at 7cbd4e8 guan12319 first commit
[root@Git hello_git]# ls
hello.sh
[root@Git hello_git]# git reflog
7cbd4e8 (HEAD -> master) HEAD@{0}: reset: moving to 7cbd4e8
6431a7b HEAD@{1}: reset: moving to 6431a7b
7cbd4e8 (HEAD -> master) HEAD@{2}: reset: moving to HEAD^
530916c HEAD@{3}: reset: moving to HEAD^
6431a7b HEAD@{4}: commit: guan12319 third commit
530916c HEAD@{5}: commit: guan12319 second commit
7cbd4e8 (HEAD -> master) HEAD@{6}: commit (initial): guan12319 first commit
Git 文件重命名
Git查看版本提交日志
程序员,写代码,写了一部分的功能,就进行一次存档,git commit
当发现某个错误约话,可以随时的回到某个存档的状态
1.查看git仓库的提交版本信息
[root@Git hello_git]# git log
commit 530916c49a6ed59cda9668dfed72b54146ccbebe (HEAD -> master)
Author: guan12319 <guan12319@qq.com>
Date: Sat Aug 5 20:51:11 2023 +0800guan12319 second commitcommit 7cbd4e8d6edb2e0b8324967c02ac3a62f1e68292
Author: guan12319 <guan12319@qq.com>
Date: Sat Aug 5 20:24:31 2023 +0800guan12319 first commit
2.一行显示,简短的实现git版本信息
[root@Git hello_git]# git log --oneline
530916c (HEAD -> master) guan12319 second commit
7cbd4e8 guan12319 first commit
3.显示,最新的1个提交记录
[root@Git hello_git]# git log -1
commit 530916c49a6ed59cda9668dfed72b54146ccbebe (HEAD -> master)
Author: guan12319 <guan12319@qq.com>
Date: Sat Aug 5 20:51:11 2023 +0800guan12319 second commit
Git Stash
Git Stash 实验
git stash就是吧暂存区还未提交的内容,临时存放到一个区域,便于日后再取回来使用
git stash save "注释" // 保存暂存区,工作进度
git stash list // 查看stash保存的列表以及id
git stash pop // 恢复最新的stash进度到工作区
git stash pop stash——id // 恢复指定的stash 进度
git stash clear // 清空所有存储的stash进度
git stash drop stash_id // 删除一个存储的stash进度
1.初始化生成一个新的git仓库
git init hello_git
# 在该目录,进行一次版本提交
cd hello_git
touch hello.sh
git add .
git commit -m "guan12319 first commit"
2.再次写入新的内容,然后提交到暂存区,并且放入stash 临时空间
[root@Git hello_git]# ls
hello.sh
[root@Git hello_git]# cat hello.sh
[root@Git hello_git]# echo "hello guan12319" > stash.git.txt
[root@Git hello_git]# ls
hello.sh stash.git.txt
[root@Git hello_git]# cat stash.git.txt
hello guan12319
[root@Git hello_git]# git status
On branch master
Untracked files:(use "git add <file>..." to include in what will be committed)stash.git.txtnothing added to commit but untracked files present (use "git add" to track)
[root@Git hello_git]# git add .
[root@Git hello_git]# git status
On branch master
Changes to be committed:(use "git restore --staged <file>..." to unstage)new file: stash.git.txt
3.此时,程序员小王,突然临时要去做其他事情,比如开发另一个功能,但是这个写好的代码文件,又不想把它给删除了,这时就可以使用 git stash
[root@Git hello_git]# ls
hello.sh stash.git.txt
[root@Git hello_git]# git stash save "save stash.git.txt ing..."
Saved working directory and index state On master: save stash.git.txt ing...
# 执行命令后,stash.git.txt 这个文件就存入stash空间了
[root@Git hello_git]# ls
hello.sh
4.此时,该文件就会放入到stash临时空间,这时,你就可以去处理其他事情了。当你把其他事情做完,又可以把存入stash空间的文件找回来,继续写代码了
[root@Git hello_git]# ls
hello.sh
[root@Git hello_git]# echo "我现在是在执行了stash之后,又做了其他事情" > hello.txt[root@Git hello_git]# ls
hello.sh hello.txt[root@Git hello_git]# git add .
[root@Git hello_git]# git commit -m "guan12319 forth commit"
[master 6b60834] guan12319 forth commit1 file changed, 1 insertion(+)create mode 100644 hello.txt
[root@Git hello_git]# git log --oneline
6b60834 (HEAD -> master) guan12319 forth commit
7cbd4e8 guan12319 first commit
[root@Git hello_git]# git status
On branch master
nothing to commit, working tree clean
[root@Git hello_git]# ls
hello.sh hello.txt
[root@Git hello_git]# git stash list
stash@{0}: On master: save stash.git.txt ing...
[root@Git hello_git]# git stash pop stash@{0}
On branch master
Changes to be committed:(use "git restore --staged <file>..." to unstage)new file: stash.git.txtDropped stash@{0} (e6cba9dd28e3af3b0e1007d13d91045998c3b6ee)
[root@Git hello_git]# ls
hello.sh hello.txt stash.git.txt
[root@Git hello_git]# git commit -m "guan12319 创建了 stash.git.txt文件,并且提交了"
[master d3efdfd] guan12319 创建了 stash.git.txt文件,并且提交了1 file changed, 1 insertion(+)create mode 100644 stash.git.txt
[root@Git hello_git]# git status
On branch master
nothing to commit, working tree clean
[root@Git hello_git]# git stash list
[root@Git hello_git]# git log --oneline
d3efdfd (HEAD -> master) guan12319 创建了 stash.git.txt文件,并且提交了
6b60834 guan12319 forth commit
7cbd4e8 guan12319 first commit
Git分支
1.查看当前的分支情况
git branch
2.创建一个guan01分支,也就表示该员工可以使用该分支,进行自己的独立空间的代码管理
git branch guan01
3.切换到分支下去写代码,查看效果
git checkout guan01
4.git 分支的管理实践
- 先创建分支
[root@Git hello_git]# git branch guan01
[root@Git hello_git]# git branchguan01
* master
- 切换到分支下
[root@Git hello_git]# git checkout guan01
Switched to branch 'guan01'
[root@Git hello_git]# git branch
* guan01master
- 在分支下创建文件,提交到暂存区,并且要进行版本提交,此时该文件,就提交到了该分支下的版本空间内
[root@Git hello_git]# git checkout guan01
Switched to branch 'guan01'
[root@Git hello_git]# echo "我是guan12319" > guan001.txt
[root@Git hello_git]# ls
guan001.txt hello.sh hello.txt stash.git.txt
[root@Git hello_git]# git status
On branch guan01
Untracked files:(use "git add <file>..." to include in what will be committed)guan001.txtnothing added to commit but untracked files present (use "git add" to track)
[root@Git hello_git]# git checkout master
Switched to branch 'master'
[root@Git hello_git]# ls
guan001.txt hello.sh hello.txt stash.git.txt
[root@Git hello_git]# git status
On branch master
Untracked files:(use "git add <file>..." to include in what will be committed)guan001.txtnothing added to commit but untracked files present (use "git add" to track)
[root@Git hello_git]# git branch guan01
* master
[root@Git hello_git]# git checkout guan01
Switched to branch 'guan01'
[root@Git hello_git]# git status
On branch guan01
Untracked files:(use "git add <file>..." to include in what will be committed)guan001.txtnothing added to commit but untracked files present (use "git add" to track)
[root@Git hello_git]# git add .
[root@Git hello_git]# git status
On branch guan01
Changes to be committed:(use "git restore --staged <file>..." to unstage)new file: guan001.txt[root@Git hello_git]# git checkout guan01
A guan001.txt
Switched to branch 'guan01'
[root@Git hello_git]# git status
On branch guan01
Changes to be committed:(use "git restore --staged <file>..." to unstage)new file: guan001.txt[root@Git hello_git]# git commit -m "guan001 第一次提交"
[guan01 c71e0e2] guan001 第一次提交1 file changed, 1 insertion(+)create mode 100644 guan001.txt
[root@Git hello_git]# git status
On branch guan01
nothing to commit, working tree clean
[root@Git hello_git]# ls
guan001.txt hello.sh hello.txt stash.git.txt
[root@Git hello_git]# git branch
* guan01master
[root@Git hello_git]# git log
commit c71e0e28d025e02ecbfa0e04fedd54d690c7d55e (HEAD -> guan01)
Author: guan12319 <guan12319@qq.com>
Date: Sat Aug 5 22:51:26 2023 +0800guan001 第一次提交commit d3efdfdb85cb14e22e2e1dccef65015fd86a3081 (master)
Author: guan12319 <guan12319@qq.com>
Date: Sat Aug 5 22:22:43 2023 +0800guan12319 创建了 stash.git.txt文件,并且提交了commit 6b60834a06caff7d614ecef7e97d3434ea33a68e
Author: guan12319 <guan12319@qq.com>
Date: Sat Aug 5 22:18:58 2023 +0800guan12319 forth commitcommit 7cbd4e8d6edb2e0b8324967c02ac3a62f1e68292
Author: guan12319 <guan12319@qq.com>
Date: Sat Aug 5 20:24:31 2023 +0800guan12319 first commit
- 这时候在切回master 查看状态
[root@Git hello_git]# git checkout master
A guan001.txt
Switched to branch 'master'
[root@Git hello_git]# git status
On branch master
Changes to be committed:(use "git restore --staged <file>..." to unstage)new file: guan001.txt[root@Git hello_git]# ls
guan001.txt hello.sh hello.txt stash.git.txt
[root@Git hello_git]# git checkout master
Switched to branch 'master'
[root@Git hello_git]# git status
On branch master
nothing to commit, working tree clean
[root@Git hello_git]# ls
hello.sh hello.txt stash.git.txt
[root@Git hello_git]# git log
commit d3efdfdb85cb14e22e2e1dccef65015fd86a3081 (HEAD -> master)
Author: guan12319 <guan12319@qq.com>
Date: Sat Aug 5 22:22:43 2023 +0800guan12319 创建了 stash.git.txt文件,并且提交了commit 6b60834a06caff7d614ecef7e97d3434ea33a68e
Author: guan12319 <guan12319@qq.com>
Date: Sat Aug 5 22:18:58 2023 +0800guan12319 forth commitcommit 7cbd4e8d6edb2e0b8324967c02ac3a62f1e68292
Author: guan12319 <guan12319@qq.com>
Date: Sat Aug 5 20:24:31 2023 +0800guan12319 first commit
[root@Git hello_git]# ls
hello.sh hello.txt stash.git.txt
这时可以看到在master里没有guan001的提交记录
5.此时可以选择删除这个分支的记录,也可以选择合并这个分支的提交记录,合并到master分支上
- 删除该分支,该分支的提交的版本信息也会随之删除
[root@Git hello_git]# git branch -D guan01
- 选择合并分支的情况如下
# 创建一个新的分支并立即切换到该分支
[root@Git hello_git]# git checkout -b guan003
Switched to a new branch 'guan003'
[root@Git hello_git]# git branch
* guan003master#合并guan01分支
[root@Git hello_git]# git checkout master
Switched to branch 'master'
[root@Git hello_git]# git branch guan003guan01
* master
[root@Git hello_git]# lshello.sh hello.txt stash.git.txt
[root@Git hello_git]# git merge guan01
Updating d3efdfd..c71e0e2
Fast-forwardguan001.txt | 1 +1 file changed, 1 insertion(+)create mode 100644 guan001.txt
[root@Git hello_git]# git status
On branch master
nothing to commit, working tree clean
[root@Git hello_git]# ls
guan001.txt hello.sh hello.txt stash.git.txt
[root@Git hello_git]# git log --oneline
c71e0e2 (HEAD -> master, guan01) guan001 第一次提交
d3efdfd (guan003) guan12319 创建了 stash.git.txt文件,并且提交了
6b60834 guan12319 forth commit
7cbd4e8 guan12319 first commit
[root@Git hello_git]# git branchguan003guan01
* master
[root@Git hello_git]#
- 当分支的提交被合并后,该分支就可以删除了,随时用分支,在创建就行
[root@Git hello_git]# git branch -d guan01
Deleted branch guan01 (was c71e0e2).
[root@Git hello_git]# git branch -d guan003
Deleted branch guan003 (was d3efdfd).
Git 分支合并冲突如何解决
Git标签
Git tag 是一个便于记忆的标签,可以是字符,也可以是数字
tag主要是和commit记录绑定在一起的
1.对于当前最新的版本记录,加上一个标签
# -a 标签名字,-m 给标签再加上一个注释
[root@Git hello_git]# git tag -a "v1.0" -m "完成了最后一个test.txt代码编写,项目.成"
2.可以直接输入 git tag 查看当前的标签版本
也可以查看 commit id 和 tag 的关系
[root@Git hello_git]# git tag
v1.0
[root@Git hello_git]# git log --oneline --decorate
c71e0e2 (HEAD -> master, tag: v1.0) guan001 第一次提交
d3efdfd guan12319 创建了 stash.git.txt文件,并且提交了
6b60834 guan12319 forth commit
7cbd4e8 guan12319 first commit
[root@Git hello_git]# git log --oneline --decorate --graph
* c71e0e2 (HEAD -> master, tag: v1.0) guan001 第一次提交
* d3efdfd guan12319 创建了 stash.git.txt文件,并且提交了
* 6b60834 guan12319 forth commit
* 7cbd4e8 guan12319 first commit
给指定 commit id 打标签
[root@Git hello_git]# git tag -a "v0.1" 7cbd4e8 -m "这是7cbd4e8的tag"
[root@Git hello_git]# git log --oneline --decorate --graph
* c71e0e2 (HEAD -> master, tag: v1.0) guan001 第一次提交
* d3efdfd guan12319 创建了 stash.git.txt文件,并且提交了
* 6b60834 guan12319 forth commit
* 7cbd4e8 (tag: v0.1) guan12319 first commit
查看某个标签的详细信息
[root@Git hello_git]# git show v1.0
tag v1.0
Tagger: guan12319 <guan12319@qq.com>
Date: Sat Aug 5 23:25:11 2023 +0800完成了最后一个test.txt代码编写,项目完成commit c71e0e28d025e02ecbfa0e04fedd54d690c7d55e (HEAD -> master, tag: v1.0)
Author: guan12319 <guan12319@qq.com>
Date: Sat Aug 5 22:51:26 2023 +0800guan001 第一次提交diff --git a/guan001.txt b/guan001.txt
new file mode 100644
index 0000000..74fc915
--- /dev/null
+++ b/guan001.txt
@@ -0,0 +1 @@
+我是guan12319
删除标签
[root@Git hello_git]# git tag
v0.1
v1.0
[root@Git hello_git]# git tag -d v0.1
Deleted tag 'v0.1' (was 6f5abec)
[root@Git hello_git]# git tag
v1.0
四、创建码云代码仓库
对于码云,或者github来说,已经创建号代码仓库的,后续的用法分为2个场景
- 在linux机器上已经存在一个Git本地仓库,
/opt/my_crm/.git
- 在Linux机器上没有Git代码仓库
- Git init初始化
- Git clone 下载Git仓库
五、配置Linux连接码云的代码仓库
简易的命令行入门教程:
Git 全局设置:
git config --global user.name "guan12319"
git config --global user.email "guan12319@qq.com"
创建 git 仓库:
mkdir test_git
cd test_git
git init
touch README.md
git add README.md
git commit -m "first commit"
# git和码云的运程绑定操作
# git 命令,给运程代码仓库地址,加上一个别名叫做origin
git remote add origin https://gitee.com/sound-of-birds-chirpingg/test_git.git
# 推送代码命令
git push -u origin "master"
已有仓库?
cd existing_git_repo
# 如果该代码仓库,还未知和远程仓库绑定,执行如下命令
git remote add origin https://gitee.com/sound-of-birds-chirpingg/test_git.git
git push -u origin "master"
六、实践代码仓库推送
注意,linux和码云的仓库,绑定的时候,一定要注意是什么协议
- 选择ssh协议,就得配置ssh-key免密推送
- 选择https协议,就输入账户密码
mkdir test_git
cd test_git
git init
touch README.md
git add README.md
git commit -m "first commit"
# git和码云的运程绑定操作
# git 命令,给运程代码仓库地址,加上一个别名叫做origin
git remote add origin https://gitee.com/sound-of-birds-chirpingg/test_git.git
# 推送代码命令
git push -u origin "master"
[root@Git my_crm]# git remote add origin git@gitee.com:sound-of-birds-chirpingg/test_git.git
[root@Git my_crm]# git push -u origin master
The authenticity of host 'gitee.com (182.255.33.134)' can't be established.
ECDSA key fingerprint is SHA256:FQGC9Kn/eye1W8icdBgrQp+KkGYoFgbVr17bmjey0Wc.
Are you sure you want to continue connecting (yes/no/[fingerprint])? y
Please type 'yes', 'no' or the fingerprint: yes
Warning: Permanently added 'gitee.com,182.255.33.134' (ECDSA) to the list of known hosts.
git@gitee.com: Permission denied (publickey).
fatal: Could not read from remote repository.Please make sure you have the correct access rights
and the repository exists.
[root@Git my_crm]# git remote add origin https://gitee.com/sound-of-birds-chirpingg/test_git.git
error: remote origin already exists.
# 删除 remote origin
[root@Git my_crm]# git remote remove origin
# 使用 HTTP 协议进行推送,需要使用码云的账号和密码登录
[root@Git my_crm]# git remote add origin https://gitee.com/sound-of-birds-chirpingg/test_git.git
推送
[root@Git my_crm]# git push -u origin master
Username for 'https://gitee.com': guan12319@qq.com
Password for 'https://guan12319@qq.com@gitee.com':
Enumerating objects: 3, done.
Counting objects: 100% (3/3), done.
Writing objects: 100% (3/3), 233 bytes | 233.00 KiB/s, done.
Total 3 (delta 0), reused 0 (delta 0), pack-reused 0
remote: Powered by GITEE.COM [GNK-6.4]
To https://gitee.com/sound-of-birds-chirpingg/test_git.git* [new branch] master -> master
branch 'master' set up to track 'origin/master'.
使用 ssh-key 免密推送
1.在Linux客户端生成密钥对
[root@Git my_crm]# ssh-keygen -t rsa
Generating public/private rsa key pair.
Enter file in which to save the key (/root/.ssh/id_rsa):
Enter passphrase (empty for no passphrase):
Enter same passphrase again:
Your identification has been saved in /root/.ssh/id_rsa.
Your public key has been saved in /root/.ssh/id_rsa.pub.
The key fingerprint is:
SHA256:ZwhRsJtahCDL4cicyMvtKqeHcKeJZDqwLGpSnL7mp+k root@Git
The key's randomart image is:
+---[RSA 3072]----+
|... oo. |
|Ooo. . o |
|+B . + |
|. o . + . |
| + o + S o |
|oo* .o o |
|BB =. |
|XoO.. |
|OXE+ |
+----[SHA256]-----+
2.复制公钥上传到码云
[root@Git my_crm]# cat ~/.ssh/id_rsa.pub // 公钥位置
3.可以使用 ssh 协议直接推送
[root@Git my_crm]# ls
git.txt
[root@Git my_crm]# echo "hello git " > hello.txt
[root@Git my_crm]# git status
On branch master
Your branch is up to date with 'origin/master'.Untracked files:(use "git add <file>..." to include in what will be committed)hello.txtnothing added to commit but untracked files present (use "git add" to track)
[root@Git my_crm]# git add .
[root@Git my_crm]# git commit -m "第二次推送"
[master 35be056] 第二次推送1 file changed, 1 insertion(+)create mode 100644 hello.txt
[root@Git my_crm]# git push -u origin master
Username for 'https://gitee.com': ^C
[root@Git my_crm]# git remote remove origin
[root@Git my_crm]# git remote add origin git@gitee.com:sound-of-birds-chirpingg/test_git.git
[root@Git my_crm]# git push -u origin master
Enumerating objects: 4, done.
Counting objects: 100% (4/4), done.
Delta compression using up to 2 threads
Compressing objects: 100% (2/2), done.
Writing objects: 100% (3/3), 290 bytes | 290.00 KiB/s, done.
Total 3 (delta 0), reused 0 (delta 0), pack-reused 0
remote: Powered by GITEE.COM [GNK-6.4]
To gitee.com:sound-of-birds-chirpingg/test_git.gitb9bcb2d..35be056 master -> master
branch 'master' set up to track 'origin/master'.
远程获取仓库代码
1.执行代码克隆命令
# 如果是https协议,就需要账户密码,验证
git clone
因为提前做了免密所以就使用ssh协议下载
[root@jenkins ~]# mkdir ./git_code
[root@jenkins ~]# cd ./git_code
[root@jenkins git_code]# ls
[root@jenkins git_code]# git clone git@gitee.com:sound-of-birds-chirpingg/test_git.git
Cloning into 'test_git'...
The authenticity of host 'gitee.com (182.255.33.134)' can't be established.
ECDSA key fingerprint is SHA256:FQGC9Kn/eye1W8icdBgrQp+KkGYoFgbVr17bmjey0Wc.
Are you sure you want to continue connecting (yes/no/[fingerprint])? yes
Warning: Permanently added 'gitee.com,182.255.33.134' (ECDSA) to the list of known hosts.
remote: Enumerating objects: 6, done.
remote: Counting objects: 100% (6/6), done.
remote: Compressing objects: 100% (3/3), done.
remote: Total 6 (delta 0), reused 0 (delta 0), pack-reused 0
Receiving objects: 100% (6/6), done.
[root@jenkins git_code]# ls
test_git
[root@jenkins git_code]# cd test_git/
[root@jenkins test_git]# ls
git.txt hello.txt
[root@jenkins test_git]# ls -a
. .. .git git.txt hello.txt
从仓库拉取更新后的代码
分别在两台linux服务器上,都clone下载号码云的代码
1.在服务器A上,往代码仓库推送内容,这时仓库更新了一些内容
[root@Git my_crm]# ls
git.txt hello.txt
[root@Git my_crm]# echo "hello guan" > guan.txt
[root@Git my_crm]# git add .
[root@Git my_crm]# git commit -m "learn git of pull"
[master e01d263] learn git of pull1 file changed, 1 insertion(+)create mode 100644 guan.txt
[root@Git my_crm]# git push -u origin master
Enumerating objects: 4, done.
Counting objects: 100% (4/4), done.
Delta compression using up to 2 threads
Compressing objects: 100% (2/2), done.
Writing objects: 100% (3/3), 305 bytes | 305.00 KiB/s, done.
Total 3 (delta 0), reused 0 (delta 0), pack-reused 0
remote: Powered by GITEE.COM [GNK-6.4]
To gitee.com:sound-of-birds-chirpingg/test_git.git35be056..e01d263 master -> master
branch 'master' set up to track 'origin/master'.
2.此时,机器B的内容还是更新之前的内容
3.在服务器B上,拉取代码仓库更新后的内容
git pull origin master
[root@jenkins test_git]# ls -a
. .. .git git.txt hello.txt
[root@jenkins test_git]# git pull origin master
remote: Enumerating objects: 4, done.
remote: Counting objects: 100% (4/4), done.
remote: Compressing objects: 100% (2/2), done.
remote: Total 3 (delta 0), reused 0 (delta 0), pack-reused 0
Unpacking objects: 100% (3/3), 285 bytes | 285.00 KiB/s, done.
From gitee.com:sound-of-birds-chirpingg/test_git* branch master -> FETCH_HEAD35be056..e01d263 master -> origin/master
Updating 35be056..e01d263
Fast-forwardguan.txt | 1 +1 file changed, 1 insertion(+)create mode 100644 guan.txt
[root@jenkins test_git]# ls
git.txt guan.txt hello.txt
4.此时服务器A和服务器B的本地代码,就和远程仓库的内容就保持一致了
七、Gitlab 安装搭建
Gitlab官网地址:https://gitlab.cn/
yum安装方式
1.安装和配置必须的依赖项
[root@Git ~]# yum install -y curl policycoreutils-python openssh-server perl
Last metadata expiration check: 1:10:38 ago on Sun 06 Aug 2023 01:16:17 AM CST.
Package curl-7.61.1-31.el8.x86_64 is already installed.
No match for argument: policycoreutils-python
Package openssh-server-8.0p1-17.el8.x86_64 is already installed.
Package perl-4:5.26.3-422.el8.x86_64 is already installed.
Error: Unable to find a match: policycoreutils-python
[root@Git ~]# yum install https://dl.fedoraproject.org/pub/epel/epel-release-latest-8.noarch.rpm
[root@Git ~]# yum install -y curl policycoreutils-python3 openssh-server
[root@Git ~]# systemctl enable sshd
[root@Git ~]# systemctl start sshd
[root@Git ~]# firewall-cmd --permanent --add-service=http
success
[root@Git ~]# firewall-cmd --permanent --add-service=https
success
[root@Git ~]# systemctl reload firewalld
[root@Git ~]# yum install postfix
Last metadata expiration check: 1:17:20 ago on Sun 06 Aug 2023 01:16:17 AM CST.
Package postfix-2:3.5.8-6.el8.x86_64 is already installed.
Dependencies resolved.
Nothing to do.
Complete!
[root@Git ~]# systemctl enable postfix
[root@Git ~]# systemctl start postfix
rpm 包安装方式
清华源gitlab的rpm包下载地址:https://mirrors.tuna.tsinghua.edu.cn/gitlab-ce/yum/el7/
[root@code ~]# mkdir /gitlab
[root@code ~]# cd /gitlab
[root@code gitlab]#
[root@code gitlab]# rpm -ivh gitlab-ce-16.0.0-ce.0.el7.x86_64.rpm
警告:gitlab-ce-16.0.0-ce.0.el7.x86_64.rpm: 头V4 RSA/SHA1 Signature, 密钥 ID f27eab47: NOKEY
准备中... ################################# [100%]
正在升级/安装...1:gitlab-ce-16.0.0-ce.0.el7 ################################# [100%]
It looks like GitLab has not been configured yet; skipping the upgrade script.*. *.*** ******** *****.****** *************** ********,,,,,,,,,***********,,,,,,,,,,,,,,,,,,,,*********,,,,,,,,,,,.,,,,,,,,,,,*******,,,,,,,,,,,,,,,,,,,,,*****,,,,,,,,,.,,,,,,,****,,,,,,.,,,***,,,,,*,._______ __ __ __/ ____(_) /_/ / ____ _/ /_/ / __/ / __/ / / __ `/ __ \/ /_/ / / /_/ /___/ /_/ / /_/ /\____/_/\__/_____/\__,_/_.___/Thank you for installing GitLab!
GitLab was unable to detect a valid hostname for your instance.
Please configure a URL for your GitLab instance by setting `external_url`
configuration in /etc/gitlab/gitlab.rb file.
Then, you can start your GitLab instance by running the following command:sudo gitlab-ctl reconfigureFor a comprehensive list of configuration options please see the Omnibus GitLab readme
https://gitlab.com/gitlab-org/omnibus-gitlab/blob/master/README.mdHelp us improve the installation experience, let us know how we did with a 1 minute survey:
https://gitlab.fra1.qualtrics.com/jfe/form/SV_6kVqZANThUQ1bZb?installation=omnibus&release=16-0[root@code gitlab]# ls
gitlab-ce-16.0.0-ce.0.el7.x86_64.rpm
2.修改gitlab的一些基础配置,然后运行gitlab页面
[root@code gitlab]# vim /etc/gitlab/gitlab.rb
#external_url 'http://gitlab.example.com'
external_url 'http://192.168.200.181'
...
### Email Settings
gitlab_rails['gitlab_email_from'] = 'guan12319@qq.com'
gitlab_rails['gitlab_email_display_name'] = 'guan_gitlab'
#gitlab_rails['gitlab_email_reply_to'] = 'noreply@example.com'
#gitlab_rails['gitlab_email_subject_suffix'] = ''
gitlab_rails['gitlab_email_smime_enabled'] = true...
### GitLab email server settings
gitlab_rails['smtp_enable'] = true
gitlab_rails['smtp_address'] = "smtp.qq.com"
gitlab_rails['smtp_port'] = 465
gitlab_rails['smtp_user_name'] = "2054210430@qq.com"
gitlab_rails['smtp_password'] = "smtp password" // 去邮箱的设置里获取授权码
gitlab_rails['smtp_domain'] = "smtp.qq.com"
gitlab_rails['smtp_authentication'] = "login"
gitlab_rails['smtp_enable_starttls_auto'] = true
gitlab_rails['smtp_tls'] = true
gitlab_rails['smtp_pool'] = false
[root@code gitlab]# grep -Ev '^#' /etc/gitlab/gitlab.rb | grep -Ev '^$'
3.测试gitlab命令行是否正确
执行gitlab的配置重新读取
gitlab-ctl reconfigure
测试发邮件
gitlab-rails console
Notify.test_email('guan12319@qq.com','hello','hello guan').deliver_now
4.修改了gitlab的配置文件,就需要重新加载gitlab配置文件
gitlab-ctl reconfigure
启动、停止、查看状态
gitlab-ctl start | restart | status | stop
初始化之后,gitlab组件都已经启动了
gitlab-ctl status
5.gitlab 运行的组件如下
GitLab 由主要由以下服务构成,他们共同承担了 Gitlab 的运作需要Nginx:静态 web 服务器。
gitlab-shell:用于处理 Git 命令和修改 authorized keys 列表。
gitlab-workhorse:轻量级的反向代理服务器。
logrotate:日志文件管理工具。
postgresql:数据库。
redis:缓存数据库。
sidekic:用于在后台执行队列任务(异步执行)。unigorn:An HTTP server for Rack applications, GitLab Rails 应用是托管在这个
6.访问gitlab
在浏览器中输入linux服务器的IP地址就行
7.gitlab相关访问命令和目录
gitlab-ctl start
gitlab-ctl stop
gitlab-ctl restart
gitlab-ctl status
gitlab-ctl stop postgresql
gitlab-ctl reconfigure
gitlab-ctl tail
gitlab-ctl tail redis
/var/opt/gitlab/git-data/repositories/ :库默认存储目录
/opt/gitlab :应用代码和相应的依赖程序
/var/opt/gitlab/ : gitlab-ctl reconfigure生成的数据和配置
/etc/gitlab :配置文件目录
/var/log/gitlab:此目录下存放了gitlab各个组件产生的日志
/var/opt/gitlab/backups : 备份文件生成的目录
Gitlab 汉化配置
1.获取汉化包
Gitlab汉化包下载地址:https://gitlab.com/xhang/gitlab
2.检查汉化包与gitlab的版本是否一致
3.关闭 gitlab 服务
gitlab-ctl stop
4.解压缩汉化包,拷贝其中内容
unzip 汉化包.zip
cp -rf 解压出来的汉化包目录/* /opt/gitlab/embedded/service/gitlab-rails/
5.重新启动
gitlab-ctl restart
检查gitlab的运行日志,检测所有组件的日志,看运行结果
6.重新在浏览器访问,就可以看到汉化的页面了