git安装与使用

git安装与使用

一:Git是什么?

Git是目前世界上最先进的分布式版本控制系统。

二:SVN与Git的最主要的区别?

Git 是一种分布式版本控制系统,而SVN是一种集中式版本控制系统。在Git中,每个开发者都有一个完整的本地代码仓库,可以在本地进行版本控制和更改记录,然后与其他开发者共享更新。而在SVN中,所有的代码和历史记录都保存在中央仓库中,开发者需要与中央仓库进行交互才能进行版本控制和更改记录。

三:git安装与使用

安装

1.关闭防火墙

2.配置yum源

yum install git -y

修改环境变量,定制git的环境

git config控制git的行为,来定义环境变量

git config --global xxxx.xxx
  • system 针对任意登录该linux系统的用户都生效,git的配置信息,写入到/etc/gitconfig
  • global 全局,只针对当前登录的用户生效,git配置写入~/.gitconfig
  • local 本地,只针对某一个文件夹生效

用户git信息配置

git 分布式版本控制 每个人在自己的本地,都可以对代码版本进行管理

提交v1版本,---提交人姓名 ---提交人邮箱 ---提交的时间

提交v2版本,---提交人姓名 ---提交人邮箱 ---提交的时间

[xu@localhost ~]$ git config --global user.name "xu"
[xu@localhost ~]$ git config --global user.email "[email protected]"
[xu@localhost ~]$ git config --global color.ui true
[xu@localhost ~]$ cat ~/.gitconfig 
[user]
        name = xu
        email = [email protected]
[color]
        ui = true
        
[xu@localhost ~]$ git config --global --list
user.name=xu
[email protected]
color.ui=true

git的四个区域

git命令就是用于将文件改动切换到不同的空间来记录

工作区>暂存区>本地git仓库>远程代码仓库 github gitlab

  • Workspace 工作区 (linux上面普通文件夹)
  • Index / Stage /Cached 暂存区
  • Repository 本地仓库
  • Remote 远程仓库 (各个本地仓库的源码提交到远程仓库合并)

git管理代码有3个场景

1.本地已经有一个代码,需要用git管理

​ 进入写好代码的目录 使用git init 对git初始化,生成.git目录

2.本地没有代码,要新建一个git版本仓库

​ mkdir /my_code/

​ cd /my_code/ && git init

3.本地没有代码,也没有git版本仓库,去github代码托管平台下载一个git版本代码库

​ git clone https://github.com/xxx/xxx_code

​ git clone 下载一个已经被git管理的代码仓库

场景1:

[root@localhost learn_git]# echo "hello linux" > hi.sh
[root@localhost learn_git]# ls -a
.  ..  hi.sh
[root@localhost learn_git]# git init .
初始化空的 Git 版本库于 /learn_git/.git/
[root@localhost learn_git]# ls -a
.  ..  .git  hi.sh

场景2:

[root@localhost /]# git init learn_git2
初始化空的 Git 版本库于 /learn_git2/.git/

场景3:

[root@localhost learn_git3]# git clone https://gitee.com/pear-admin/pear-admin-flask.git
正克隆到 'pear-admin-flask'...
remote: Enumerating objects: 6642, done.
remote: Counting objects: 100% (1938/1938), done.
remote: Compressing objects: 100% (1481/1481), done.
remote: Total 6642 (delta 1063), reused 767 (delta 391), pack-reused 4704
接收对象中: 100% (6642/6642), 16.08 MiB | 2.83 MiB/s, done.
处理 delta 中: 100% (3197/3197), done.

[root@localhost learn_git3]# ls
pear-admin-flask
[root@localhost learn_git3]# cd pear-admin-flask/
[root@localhost pear-admin-flask]# ls -a
.   applications  dockercompose.yaml  Dockerfile     docs  .gitignore  migrate.bat  plugins    requirements.txt  run.sh  templates
..  app.py        dockerdata          .dockerignore  .git  LICENSE     migrate.sh   README.md  run.bat           static

`可以看到有.git`
`当文件没有发生改变时候`
[root@localhost pear-admin-flask]# git status
# 位于分支 master
无文件要提交,干净的工作区

`创建一个文件 再看一下git状态`
[root@localhost pear-admin-flask]# touch test.txt
[root@localhost pear-admin-flask]# git status
# 位于分支 master
# 未跟踪的文件:
#   (使用 "git add <file>..." 以包含要提交的内容)
#
#       test.txt
提交为空,但是存在尚未跟踪的文件(使用 "git add" 建立跟踪)

git生命周期

[root@localhost git_code]# mkdir /git_code
1.生产工作区
[root@localhost git_code]# git init hello_git
[root@localhost git_code]# cd hello_git/

2.查看git工作区中的本地仓库
[root@localhost hello_git]# ls -a
.  ..  .git

3.查看工作区的信息
[root@localhost hello_git]# git status
# 位于分支 master
#
# 初始提交
#
无文件要提交(创建/拷贝文件并使用 "git add" 建立跟踪)

`touch test.txt创建一个文件 再来git status`
[root@localhost hello_git]# git status
# 位于分支 master
#
# 初始提交
#
# 未跟踪的文件:
#   (使用 "git add <file>..." 以包含要提交的内容)
#
#       test.txt
提交为空,但是存在尚未跟踪的文件(使用 "git add" 建立跟踪)

4.提交到缓存区
`git add . 可以对所改变的的全部文件提交到暂存区,也可以单独指定文件 git add test.txt`
[root@localhost hello_git]# git status
# 位于分支 master
#
# 初始提交
#
# 要提交的变更:
#   (使用 "git rm --cached <file>..." 撤出暂存区)
#
#       新文件:    test.txt

`如果像删除暂存区的内容可以使用 git rm --cached filename 不再演示`
5.提交到本地仓库
[root@localhost hello_git]# git commit -m "xu first commit"
[master(根提交) 34a9852] xu first commit
 1 file changed, 0 insertions(+), 0 deletions(-)
 create mode 100644 test.txt
[root@localhost hello_git]# git status
# 位于分支 master
无文件要提交,干净的工作区

git使用补充

6.当在工作区需要改文件名时 需要使用git mv 命令 这样操作会被git所记录,直接使用mv则不会被记录
[root@localhost hello_git]# mv test.txt test2.txt
[root@localhost hello_git]# git status
# 位于分支 master
# 尚未暂存以备提交的变更:
#   (使用 "git add/rm <file>..." 更新要提交的内容)
#   (使用 "git checkout -- <file>..." 丢弃工作区的改动)
#
#       删除:      test.txt
#
# 未跟踪的文件:
#   (使用 "git add <file>..." 以包含要提交的内容)
#
#       test2.txt
修改尚未加入提交(使用 "git add" 和/或 "git commit -a")

`可以用 git checkout --<file> ...将放弃此改动`
`手动删除test2.txt`
[root@localhost hello_git]# rm -rf test2.txt 
[root@localhost hello_git]# ls
test.txt
`git checkout`
[root@localhost hello_git]# git checkout test.txt
[root@localhost hello_git]# git status
# 位于分支 master
无文件要提交,干净的工作区
[root@localhost hello_git]# ls
test.txt

7.正常使用git mv更改文件名
[root@localhost hello_git]# git mv test.txt test2.txt
[root@localhost hello_git]# ls
test2.txt
[root@localhost hello_git]# git status
# 位于分支 master
# 要提交的变更:
#   (使用 "git reset HEAD <file>..." 撤出暂存区)
#
#       重命名:    test.txt -> test2.txt
#
`提交`
[root@localhost hello_git]# git commit -m "把test.txt文件名改成test2.txt"[master d808cb5] 把test.txt文件名改成test2.txt
 1 file changed, 0 insertions(+), 0 deletions(-)
 rename test.txt => test2.txt (100%)
[root@localhost hello_git]# git status
# 位于分支 master
无文件要提交,干净的工作区

8.从本地仓库中,删除对某个文件的跟踪,此时状态回到“未跟踪”工作区
git rm --cached 
[root@localhost hello_git]# git rm --cached test2.txt 
rm 'test2.txt'
[root@localhost hello_git]# git status
# 位于分支 master
# 要提交的变更:
#   (使用 "git reset HEAD <file>..." 撤出暂存区)
#
#       删除:      test2.txt
#
# 未跟踪的文件:
#   (使用 "git add <file>..." 以包含要提交的内容)
#
#       test2.txt

9.此时对上述的删除动作,可以有三个选择
9.1直接删除文件
rm -rf test2.txt

9.2撤销刚才的git rm动作,继续回到存区
git reset HEAD test2.txt

9.3
git add test2.txt

git查看版本信息

1.查看git仓库的提交版本信息
[root@localhost hello_git]# git log
commit d808cb53c970c9fc64b2455da5e6dd5c49de8670
Author: x <[email protected]>
Date:   Thu Jul 20 01:53:13 2023 +0800

    把test.txt文件名改成test2.e

commit 34a985235c4898d7e94ee4fdcd4bf0978153ae6f
Author: x <[email protected]>
Date:   Thu Jul 20 01:21:13 2023 +0800

    xu first commit

2.显示简略版
[root@localhost hello_git]# git log --oneline
d808cb5 把test.txt文件名改成test2.txt
34a9852 xu first commit

3.显示最新的一条提交记录
[root@localhost hello_git]# git log -1
commit d808cb53c970c9fc64b2455da5e6dd5c49de8670
Author: x <[email protected]>
Date:   Thu Jul 20 01:53:13 2023 +0800

    把test.txt文件名改成test2.txt

git版本回退

git reset --hard选项可以进行版本重置,回退
git版本的管理,是通过指针管理的,那这个指针,名字叫做HEAD

HEAD表示当前版本
HEAD^表示上一个版本
HEAD^^表示上上个版本

git reset --hard 版本id号

[root@localhost hello_git]# git log --oneline
d808cb5 把test.txt文件名改成test2.txt
34a9852 xu first commit
[root@localhost hello_git]# git reset --hard HEAD^
HEAD 现在位于 34a9852 xu first commit
[root@localhost hello_git]# ls
test.txt
还可以使用git reset --hard 34a9852 指定版本号跟上述效果一样

`git log 对丢失回到此版本之后的版本`
[root@localhost hello_git]# git log --oneline
34a9852 xu first commit

git查案所有版本变动日志

查看git所记录的你每一次版本提交与回退的日志
git reflog
[root@localhost hello_git]# git reflog
34a9852 HEAD@{0}: reset: moving to HEAD^
d808cb5 HEAD@{1}: commit: 把test.txt文件名改成test2.txt
34a9852 HEAD@{2}: commit (initial): xu first commit

[root@localhost hello_git]# git reset --hard d808cb5
HEAD 现在位于 d808cb5 把test.txt文件名改成test2.txt

[root@localhost hello_git]# git log --oneline
d808cb5 把test.txt文件名改成test2.txt
34a9852 xu first commit

git stash临时空间

git stash 就是把暂存区还未提交的内容,临时存放到一个区域,便于日后再取回来使用
[root@localhost hello_git]# touch stash_test.txt
[root@localhost hello_git]# git status
# 位于分支 master
# 未跟踪的文件:
#   (使用 "git add <file>..." 以包含要提交的内容)
#
#       stash_test.txt
提交为空,但是存在尚未跟踪的文件(使用 "git add" 建立跟踪)
[root@localhost hello_git]# git add stash_test.txt 
[root@localhost hello_git]# git status
# 位于分支 master
# 要提交的变更:
#   (使用 "git reset HEAD <file>..." 撤出暂存区)
#
#       新文件:    stash_test.txt
#

1.git stash的使用
以上是通常的创建文件并存放暂存区的流程
但是假如这时候突然要你去做别的事情,比如再去开发另一个新功能,我们可以不删除这个文件,可以使用git stash放到临时区
[root@localhost hello_git]# git stash save "save stash.git.txt ing..."
Saved working directory and index state On master: save stash.git.txt ing...
HEAD 现在位于 d808cb5 把test.txt文件名改成test2.txt
[root@localhost hello_git]# git status
# 位于分支 master
无文件要提交,干净的工作区

2.git stash list查看临时保存中的文件
[root@localhost hello_git]# git stash list
stash@{0}: On master: save stash.git.txt ing...

3.git stash pop 恢复最新的stash进度到工作区 / git stash pop stash_id 恢复指定的stash进度
[root@localhost hello_git]# git stash pop stash@{0}
# 位于分支 master
# 要提交的变更:
#   (使用 "git reset HEAD <file>..." 撤出暂存区)
#
#       新文件:    stash_test.txt
#

[root@localhost hello_git]# git commit -m "提交了stash_test.txt文件"
[master d16d294] 提交了stash_test.txt文件
 1 file changed, 0 insertions(+), 0 deletions(-)
 create mode 100644 stash_test.txt
[root@localhost hello_git]# git status
# 位于分支 master
无文件要提交,干净的工作区

git分支学习

1.查看当前
git branch
[root@localhost hello_git]# git branch
* master

2.创建一个user2分支
git branch user2
[root@localhost hello_git]# git branch user2
[root@localhost hello_git]# git branch
* master
  user2

3.切换分支
[root@localhost hello_git]# git checkout user2
切换到分支 'user2'
[root@localhost hello_git]# git branch
  master
* user2

4.创建一个文件、跟踪并提交
[root@localhost hello_git]# echo "我是user2">user2.txt
[root@localhost hello_git]# git add user2.txt 
[root@localhost hello_git]# git status
# 位于分支 user2
# 要提交的变更:
#   (使用 "git reset HEAD <file>..." 撤出暂存区)
#
#       新文件:    user2.txt
#
检查日志
[root@localhost hello_git]# git log --oneline
800b648 user2提交 user2.txt文件
d16d294 提交了stash_test.txt文件
d808cb5 把test.txt文件名改成test2.txt
34a9852 xu first commit

5.切换为master 再次检查日志
[root@localhost hello_git]# git log --oneline
d16d294 提交了stash_test.txt文件
d808cb5 把test.txt文件名改成test2.txt
34a9852 xu first commit
会发现只有三条记录

6.可以选择删除这个分支的记录,也可以选择合并这个分支的提交记录合并到master

6.1删除改分支,改分支提交版本的信息也会随之删除 使用以下命令即可,不再演示
git branch -D user2

6.2合并user2分支到master主分支
[root@localhost hello_git]# git branch
* master
  user2
[root@localhost hello_git]# git merge user2
更新 d16d294..800b648
Fast-forward
 user2.txt | 1 +
 1 file changed, 1 insertion(+)
 create mode 100644 user2.txt

[root@localhost hello_git]# git log --oneline
800b648 user2提交 user2.txt文件
d16d294 提交了stash_test.txt文件
d808cb5 把test.txt文件名改成test2.txt
34a9852 xu first commit

7.当分支的提交记录合并后,该分支就可以删除了,随时用分支,随时创建即可 没有合并-D 合并以后删除用-d
[root@localhost hello_git]# git branch -d user2
已删除分支 user2(曾为 800b648)。

git分支合并冲突如何解决

1.创建且切换一个新的分支
git checkout -b user3

2.在user3分支下,创建文件,且提交新的版本记录
echo "如何解决git分支合并冲突的问题" > test3.txt
git add test3.txt
git commit -m "user3 commit test3.txt"

3.回到master分支,创建同样的文件,其提交新的版本记录
[root@localhost hello_git]# git log --oneline
93d87b0 master commit test3.txt
800b648 user2提交 user2.txt文件
d16d294 提交了stash_test.txt文件
d808cb5 把test.txt文件名改成test2.txt
34a9852 xu first commit

[root@localhost hello_git]# git checkout user3
切换到分支 'user3'
[root@localhost hello_git]# git log --oneline
5cde4c5 user3 commit test3.txt
800b648 user2提交 user2.txt文件
d16d294 提交了stash_test.txt文件
d808cb5 把test.txt文件名改成test2.txt
34a9852 xu first commit

4.此时在master分支上,合并user3分支,但是两分支都有test3.txt文件,并且里面都有内容,合并就会产生冲突,到底以哪一分支为准呢?
 切换到master分支并合并user3
 git checkout master
 git merge user3
 
 5.此时cat test3.txt 
[root@localhost hello_git]# cat test3.txt 
<<<<<<< HEAD
master创建test3.txt
=======
如何解决git分支合并冲突的问题
>>>>>>> user3

6.可以自由选择修改其中内容
[root@localhost hello_git]# vim test3.txt 
master创建test3.txt
如何解决git分支合并冲突的问题

7.添加并提交
git add .
git commit -m "最终master人为合并了test3.txt内容"

git标签学习

git tag
给每一个git版本,加上一个标签
就是一个便于记忆的标签,可以是字符,可以是数字

1.可以直接输入,对最新的版本记录加上一个标签
[root@localhost hello_git]# git tag -a "v1.0" -m "test3.txt 1.0版本"

2.查看tag标签
[root@localhost hello_git]# git tag
v1.0
v1.0.1

[root@localhost hello_git]# git log --oneline --decorate
f26efa2 (HEAD, tag: v1.0, master) 最终master人为合并了test3.txt内容
93d87b0 master commit test3.txt
5cde4c5 (user3) user3 commit test3.txt
800b648 user2提交 user2.txt文件
d16d294 提交了stash_test.txt文件
d808cb5 把test.txt文件名改成test2.txt
34a9852 xu first commit

[root@localhost hello_git]# git log --oneline --decorate --graph
*   f26efa2 (HEAD, tag: v1.0, master) 最终master人为合并了test3.txt内容
|\  
| * 5cde4c5 (user3) user3 commit test3.txt
* | 93d87b0 master commit test3.txt
|/  
* 800b648 user2提交 user2.txt文件
* d16d294 提交了stash_test.txt文件
* d808cb5 把test.txt文件名改成test2.txt
* 34a9852 xu first commit

3.也可以指定commit id加上tag标签
[root@localhost hello_git]# git tag -a "v1.0.1" 800b648 -m "这是user2提交user2.txt文件的tag"
[root@localhost hello_git]# git log --oneline --decorate
f26efa2 (HEAD, tag: v1.0, master) 最终master人为合并了test3.txt内容
93d87b0 master commit test3.txt
5cde4c5 (user3) user3 commit test3.txt
800b648 (tag: v1.0.1) user2提交 user2.txt文件
d16d294 提交了stash_test.txt文件
d808cb5 把test.txt文件名改成test2.txt
34a9852 xu first commit

4.查看标签里面的具体信息,以及关联的版本记录的具体信息
git show v1.0

5.删除tag
git tag -d v1.0.1

文章内容主要参考:b站linux超哥 https://www.bilibili.com/video/BV19K4y1H77x?p=1&vd_source=0318a6d26fb028b832726d62a0e9e2cb

热门相关:总裁别再玩了   富贵不能吟   锦庭娇   富贵不能吟   金粉