DevOps-Git

DevOps-Git

版本控制软件提供完备的版本管理功能,用于存储,追踪目录(文件夹)和文件的修改历史。版本控制软件的最高目标是支持公司的配置管理活动,最终多个版本的开发和维护活动,即使发布软件。

在这里插入图片描述

在这里插入图片描述

git安装

在这里插入图片描述

https://git-scm.com/

yum install git -y


[root@workstation ~]# git --version
git version 1.8.3.1

查看参数帮助


[root@workstation ~]# git --version
git version 1.8.3.1
[root@workstation ~]#
[root@workstation ~]# git --help
usage: git [--version] [--help] [-c name=value]
           [--exec-path[=<path>]] [--html-path] [--man-path] [--info-path]
           [-p|--paginate|--no-pager] [--no-replace-objects] [--bare]
           [--git-dir=<path>] [--work-tree=<path>] [--namespace=<name>]
           <command> [<args>]

The most commonly used git commands are:
   add        Add file contents to the index
   bisect     Find by binary search the change that introduced a bug
   branch     List, create, or delete branches
   checkout   Checkout a branch or paths to the working tree
   clone      Clone a repository into a new directory
   commit     Record changes to the repository
   diff       Show changes between commits, commit and working tree, etc
   fetch      Download objects and refs from another repository
   grep       Print lines matching a pattern
   init       Create an empty Git repository or reinitialize an existing one
   log        Show commit logs
   merge      Join two or more development histories together
   mv         Move or rename a file, a directory, or a symlink
   pull       Fetch from and merge with another repository or a local branch
   push       Update remote refs along with associated objects
   rebase     Forward-port local commits to the updated upstream head
   reset      Reset current HEAD to the specified state
   rm         Remove files from the working tree and from the index
   show       Show various types of objects
   status     Show the working tree status
   tag        Create, list, delete or verify a tag object signed with GPG

'git help -a' and 'git help -g' lists available subcommands and some
concept guides. See 'git help <command>' or 'git help <concept>'
to read about a specific subcommand or concept.

git身份设置

因为git是分布式版本控制系统,不同的人提交的代码需要区分,所以每个人需要设置一个身份标识。

[root@workstation ~]# git config --global user.name "rkun18"
[root@workstation ~]# git config --global user.email "rkun18@outlook.com"
[root@workstation ~]# git config --global color.ui true
[root@workstation ~]# git config --list
user.name=rkun18
user.email=rkun18@outlook.com
color.ui=true

创建本地仓库

  • 工作目录:也可以叫工作区,是存放项目代码文件的一个目录。
  • 仓库:版本库,在git init命令初始化工作目录会会产生一个隐藏的子目录.git,可以理解为git的仓库或版本库
  • 仓库分为本地仓库和远程仓库

i3在这里插入图片描述

在这里插入图片描述

创建本地仓库

  • 创建工作目录

    
    [root@workstation ~]# mkdir git_test
    
    
  • 在对应的工作目录中创建本地仓库

    
    [root@workstation ~]# cd git_test/
    [root@workstation git_test]# git init
    Initialized empty Git repository in /root/git_test/.git/
    #产生一个.git的子目录,所有出代码数据以外的相关数据都在此目录,不要修改它(它叫做仓库/版本库)
    [root@workstation git_test]# ls .git/
    branches  config  description  HEAD  hooks  info  objects  refs
    
    

暂存区

就是一个缓存区域(index/stage),临时保存你的更改。

如果在工作目录创建了一个新文件,需要将新文件添加到暂存区。

添加文件到暂存区

  • 准备一个文件

    [root@workstation git_test]# vi file1.py
    [root@workstation git_test]# cat file1.py
    print("hello git")
    
    
    
  • 提交到暂存区(逆向操作为 git rm --cached file1.py)

    [root@workstation git_test]# git add file1.py
    
    
  • 查看.git子目录,多了一个index

    
    [root@workstation git_test]# ls .git/
    branches  config  description  HEAD  hooks  index  info  objects  refs
    
    
  • 使用 strings 命令查看git add 文件列表

    
    [root@workstation git_test]# strings .git/index
    DIRC
    file1.py
    #这里可以看到file1.py文件添加到index文件里去了
    

git版本控制

提交文件(1版本)

代码文件提交需要commit提交后才能纳入版本控制

  • git status查看工作目录里有那些文件需要提交

    
    [root@workstation git_test]# git status
    # On branch master
    #
    # Initial commit
    #
    # Changes to be committed:
    #   (use "git rm --cached <file>..." to unstage)
    #
    #       new file:   file1.py
    #
    
    
  • 使用git commit提交 -m后接提交的说明信息

    
    [root@workstation git_test]# git commit -m "提交file1.py"
    [master (root-commit) 9a26ead] 提交file1.py
     1 file changed, 1 insertion(+)
     create mode 100644 file1.py
    
    
  • 再次查看状态,发现没有需要提交的文件

    
    [root@workstation git_test]# git status
    # On branch master
    nothing to commit, working directory clean
    
    

修改再提交(2版本)

  • 修改file1.py文件,这里我加一句

    
    [root@workstation git_test]# cat file1.py
    print("hello git")
    print("hello python")
    
    
  • 查看,通知file1.py被修改

    [root@workstation git_test]# git status
    # On branch master
    # Changes not staged for commit:
    #   (use "git add <file>..." to update what will be committed)
    #   (use "git checkout -- <file>..." to discard changes in working directory)
    #
    #       modified:   file1.py
    #
    no changes added to commit (use "git add" and/or "git commit -a")
    
    
  • 使用git diff查看修改了什么

    [root@workstation git_test]# git diff file1.py
    diff --git a/file1.py b/file1.py
    index 81bc9dd..aeea3d5 100644
    --- a/file1.py
    +++ b/file1.py
    @@ -1 +1,2 @@
     print("hello git")
    +print("hello python")
    
    
  • 修改提交

    
    [root@workstation git_test]# git add file1.py
    [root@workstation git_test]# git commit -m "添加一行代码"
    [master 58a0633] 添加一行代码
     1 file changed, 1 insertion(+)
    
    

再修改提交(3版本)

#再次添加代码

[root@workstation git_test]# vi file1.py
[root@workstation git_test]# cat file1.py
print("hello git")
print("hello python")
print("hello linux")


[root@workstation git_test]# git add file1.py
[root@workstation git_test]# git commit -m "添加一行代码"
[master e059aae] 添加一行代码
 1 file changed, 1 insertion(+)

查看提交历史

  • git log 查看提交历史版本信息

    
    [root@workstation git_test]# git log
    commit e059aaeb9ec15ddf650020b2a21b44abc02de9c6
    Author: rkun18 <rkun18@outlook.com>
    Date:   Wed Jul 19 10:46:47 2023 -0400
    
        添加一行代码
    
    commit 58a0633d037ccc84060317bccc1f831606dec8c0
    Author: rkun18 <rkun18@outlook.com>
    Date:   Wed Jul 19 10:43:18 2023 -0400
    
        添加一行代码
    
    commit 9a26ead76c4c93419c75b6ff18d1e0f4ed4ea15b
    Author: rkun18 <rkun18@outlook.com>
    Date:   Wed Jul 19 10:28:03 2023 -0400
    
        提交file1.py
    
    
  • 使用 git log --pretty=oneline 查看提交的历史版本信息,查看的显示信息更简介(前面字符串可以看作版本号)

    
    [root@workstation git_test]# git log --pretty=oneline
    e059aaeb9ec15ddf650020b2a21b44abc02de9c6 添加一行代码
    58a0633d037ccc84060317bccc1f831606dec8c0 添加一行代码
    9a26ead76c4c93419c75b6ff18d1e0f4ed4ea15b 提交file1.py
    
    

版本回退与还原

  • 使用git reset --hard HEAD^ 回退到上一个版本(也就是2版本)

    [root@workstation git_test]# git reset --hard HEAD^
    HEAD is now at 58a0633 添加一行代码
    [root@workstation git_test]# cat file1.py
    print("hello git")
    print("hello python")
    
    
  • 使用 git reset --hard 第三个版本号 (还原到第三个版本)

    如果忘记第三个版本号是什么,可以使用 git reflog 查看所有操作历史

    
    [root@workstation git_test]# git reflog
    58a0633 HEAD@{0}: reset: moving to HEAD^
    e059aae HEAD@{1}: commit: 添加一行代码
    58a0633 HEAD@{2}: commit: 添加一行代码
    9a26ead HEAD@{3}: commit (initial): 提交file1.py
    
    
  • 还原到第三个版本

    [root@workstation git_test]# git reset --hard e059aae
    HEAD is now at e059aae 添加一行代码
    [root@workstation git_test]# cat file1.py
    print("hello git")
    print("hello python")
    print("hello linux")
    
    
  • 回退上上个版本 git reset --hard HEAD^^ 回退三个版本依次类推 git reset --hard HEAD^^^ 回退100个版本,可以换成 git reset --hard HEAD~100

    [root@workstation git_test]# git reset --hard HEAD~2
    HEAD is now at 9a26ead 提交file1.py
    [root@workstation git_test]# cat file1.py
    print("hello git")
    
    

撤销修改

  • 准备一行或一段写错的代码

    
    [root@workstation git_test]# cat file1.py
    print("hello git")
    print("hello python")
    print("hello linux")
    print("error code")
    
    
  • 撤销策略

    • 删除错误代码
    • git checkout -- 文件名 撤销修改
    • 写乱了代码,添加暂存区但还没commit提交 使用git reset HEAD 文件名 取消暂存区添加,再 git checkout -- 文件名 撤销修改
    • 如果写乱代码,添加暂存区并提交。使用版本回退。

误删恢复

只要git add 到了暂存区,无论有没有 git commit,误删后都可以使用 git checkout --文件名 来恢复

[root@workstation git_test]# touch file2.py
[root@workstation git_test]# git add file2.py
[root@workstation git_test]# rm -rf file2.py
[root@workstation git_test]# ls
file1.py
[root@workstation git_test]# git checkout -- file2.py
[root@workstation git_test]# ls
file1.py  file2.py

如果文件没有 git add 到暂存区 ,误删除了就没了


[root@workstation git_test]# touch file3.py
[root@workstation git_test]# rm -rf file3.py
[root@workstation git_test]# git checkout -- file3.py
error: pathspec 'file3.py' did not match any file(s) known to git.

文件删除

  • 没有 git add 到暂存区的文件直接rm 删除

  • git add 到暂存区的文件,但没有git commit 提交的文件。需要rm 删除本地,还要git rm文件名

    
    [root@workstation git_test]# touch file3.py
    [root@workstation git_test]# git add file3.py
    [root@workstation git_test]# rm -rf file3.py
    [root@workstation git_test]# git rm file3.py
    rm 'file3.py'
    
    
  • git add 到暂存区的文件,并git commit 提交的文件。需要rm 删除本地,还要git rm文件名,最后提交删除

    [root@workstation git_test]# touch file3.py
    [root@workstation git_test]# git add file3.py
    [root@workstation git_test]# git commit -m "提交了file3.py"
    [master c2c88b8] 提交了file3.py
     2 files changed, 0 insertions(+), 0 deletions(-)
     create mode 100644 file2.py
     create mode 100644 file3.py
    [root@workstation git_test]# rm -rf file3.py
    [root@workstation git_test]# git rm file3.py
    rm 'file3.py'
    [root@workstation git_test]# git commit -m "删除了file3.py"
    [master 8a37dad] 删除了file3.py
     1 file changed, 0 insertions(+), 0 deletions(-)
     delete mode 100644 file3.py
    
    

git分支管理

问题:开发者A开发一个软件的模块,还没有开发完成,害怕进度丢失就提交。开发者B不知道A没有完成,直接使用A开发的文件,这样就造成了问题。

解决:开发者A创建一个属于自己的分支,这个分支只属于A,不会影响其他人。开发完成后,合并到项目主分支即可。

在这里插入图片描述

查看分支

默认只有一个master分支,前面有*号的代表当前分支

[root@workstation git_test]# git branch
* master

创建分支

git branch 分支名来创建分支

[root@workstation git_test]# git branch dev
[root@workstation git_test]# git branch
  dev
* master

切换分支

使用git checkout 分支名 切换


[root@workstation git_test]# git checkout dev
M       file1.py
D       file2.py
Switched to branch 'dev'
[root@workstation git_test]# git branch
* dev
  master

合并分支

在dev分支新开发一个代码,添加并提交

[root@workstation git_test]# git branch
* dev
  master
[root@workstation git_test]# echo "new feature" > file3.py
[root@workstation git_test]# git add file3.py
[root@workstation git_test]# git commit -m "增加新特性"
[dev ec7eff5] 增加新特性
 1 file changed, 1 insertion(+)
 create mode 100644 file3.py

切换master分支上,却发现根本没有这个文件


[root@workstation git_test]# git checkout master
M       file1.py
D       file2.py
Switched to branch 'master'
[root@workstation git_test]# cat file3.py
cat: file3.py: No such file or directory

合并分支,再查看能在master分支上查看到了

[root@workstation git_test]# git merge dev
Updating 8a37dad..ec7eff5
Fast-forward
 file3.py | 1 +
 1 file changed, 1 insertion(+)
 create mode 100644 file3.py
[root@workstation git_test]# cat file3.py
new feature

分支冲突

有些复杂情况会造成冲突,这个时候git就不能帮我们自动合并分支。我们就要手动处理冲突。

  • 在dev分支修改文件

    
    [root@workstation git_test]# git checkout dev
    M       file1.py
    D       file2.py
    Switched to branch 'dev'
    [root@workstation git_test]# echo "冲突测试" >> file3.py
    [root@workstation git_test]# cat file3.py
    new feature
    冲突测试
    
    
  • 提交dev分支上的修改

    [root@workstation git_test]# git add file3.py
    [root@workstation git_test]# git commit -m "冲突测试"
    [dev 1b3ec99] 冲突测试
     1 file changed, 1 insertion(+)
    
    
  • 切回master分支,也修改相同的文件

    
    [root@workstation git_test]# git checkout master
    M       file1.py
    D       file2.py
    Switched to branch 'master'
    [root@workstation git_test]# echo "冲突" >> file3.py
    [root@workstation git_test]# cat file3.py
    new feature
    冲突
    
    
  • 提交master分支上的修改

    [root@workstation git_test]# git add file3.py
    [root@workstation git_test]# git commit -m "冲突测试"
    [master c0e2b26] 冲突测试
     1 file changed, 1 insertion(+)
    
    
  • 合并dev分支到master,就会出现冲突

    [root@workstation git_test]# git merge dev
    Auto-merging file3.py
    CONFLICT (content): Merge conflict in file3.py
    Automatic merge failed; fix conflicts and then commit the result.
    
    
  • 手工解决冲突

    git使用<<<<<<<<<<,==========,>>>>>>>>符号分隔冲突内容,手动删除这些符号,并修改成你想要的内容。

    
    [root@workstation git_test]# cat file3.py
    new feature
    <<<<<<< HEAD
    冲突
    =======
    冲突测试
    >>>>>>> dev
    
    [root@workstation git_test]# vi file3.py
    [root@workstation git_test]# cat file3.py
    new feature
    冲突解决
    
    
  • 解决冲突后添加并提交最后再合并

    
    [root@workstation git_test]# git add file3.py
    [root@workstation git_test]# git commit -m "冲突解决"
    [master 73bcce3] 冲突解决
    [root@workstation git_test]# git merge dev
    Already up-to-date.
    
    

删除分支

使用git branch -d 分支名 来删除分支(不能删除当前分支)

[root@workstation git_test]# git branch
  dev
* master
[root@workstation git_test]# git branch -d dev
Deleted branch dev (was 1b3ec99).
[root@workstation git_test]# git branch
* master

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mfbz.cn/a/52753.html

如若内容造成侵权/违法违规/事实不符,请联系我们进行投诉反馈qq邮箱809451989@qq.com,一经查实,立即删除!

相关文章

Eureka注册中心 与 OpenFeign调用接口

需求 一个应用通过接口&#xff0c;调用另一个应用的接口。使用OpenFeign来实现接口调用。 说明 通过OpenFeign&#xff08;本文接下来简称Feign&#xff09;调用远程接口&#xff0c;需要Eureka注册中心的支持。 OpenFeign调用接口的逻辑如下&#xff1a; 提供接口的应用…

笔记本触摸板没反应怎么办?只需要4个方法!快速解决!

“大家知道为什么笔记本触摸板没反应吗&#xff1f;我的鼠标不见了现在触摸板也没反应&#xff0c;根本就用不了电脑了&#xff0c;有什么方法可以解决吗&#xff1f;” 触摸板是笔记本电脑上最重要的输入设备之一&#xff0c;它可以提供便捷的操作方式。对于很多朋友来说&…

原型模式——对象的克隆

1、简介 1.1、概述 可以通过一个原型对象克隆出多个一模一样的对象&#xff0c;该模式被称为原型模式。 在使用原型模式时&#xff0c;需要首先创建一个原型对象&#xff0c;再通过复制这个原型对象来创建更多同类型的对象。 1.2、定义 原型模式&#xff08;Prototype Patt…

前端试用期工作总结范文5篇

前端试用期工作总结 &#xff08;篇1&#xff09; 时间飞逝&#xff0c;转眼间&#xff0c;做为一名Web前端开发的正式员工已经有两个月之久。在这个难忘而又夸姣的 日子里&#xff0c;我深入体会到了公司的积极氛围和各个部门的巨大魅力&#xff0c;目睹了公司一步步走向成熟…

(202307)wonderful-sql:复杂一点的查询(task3)

教程链接&#xff1a;Datawhale - 一个热爱学习的社区 知识学习 1 视图 视图是一张虚拟的表。《sql基础教程第2版》用一句话非常凝练的概括了视图与表的区别---“是否保存了实际的数据”。 通过定义视图可以将频繁使用的SELECT语句保存以提高效率。通过定义视图可以使用户看…

Games101学习笔记 - 变换矩阵基础

二维空间下的变换 缩放矩阵 缩放变换: 假如一个点&#xff08;X,Y&#xff09;。x经过n倍缩放&#xff0c;y经过m倍缩放&#xff0c;得到的新点&#xff08;X1&#xff0c;Y1&#xff09;&#xff1b;那么新点和远点有如下关系&#xff0c;X1 n*X, Y1 m*Y写成矩阵就是如下…

C++ 自定义数据类型

C自定义数据类型有&#xff1a;枚举类型、结构类型、联合类型、数组类型、类类型 1. typedef 声明 在编写程序时&#xff0c;除了可以使用内置的基本数据类型名和自定义的数据类型名以外&#xff0c;还可以为一个已有的数据类型另外命名。这样&#xff0c;就可以根据不同的应…

word怎么转换成pdf?分享几种转换方法

word怎么转换成pdf&#xff1f;将Word文档转换成PDF文件有几个好处。首先&#xff0c;PDF文件通常比Word文档更容易在不同设备和操作系统上查看和共享。其次&#xff0c;PDF文件通常比Word文档更难以修改&#xff0c;这使得它们在需要保护文件内容的情况下更加安全可靠。最后&a…

创建维基WIKI百科和建立百度百科有何不同?

很多企业有出口业务&#xff0c;想在互联网上开展全球性网络营销&#xff0c;维基百科往往被认为是开展海外营销的第一站。其作用相当于开展国内网络营销的百度百科&#xff0c;经常有些企业给小马识途营销顾问提供的词条内容就是百度百科的内容&#xff0c;可事实上两个平台的…

1227. 分巧克力(简单,易懂)

输入样例&#xff1a; 2 10 6 5 5 6输出样例&#xff1a; 2 这个题就是基础的二分问题&#xff0c;做题思路&#xff1a; 找到一个数&#xff0c;让其满足&#xff0c;所有小块的边值&#xff0c;且最终的总和要大于等于我们的K 第一次做错了&#xff01;&#xff01; #in…

云计算——虚拟化层架构

作者简介&#xff1a;一名云计算网络运维人员、每天分享网络与运维的技术与干货。 座右铭&#xff1a;低头赶路&#xff0c;敬事如仪 个人主页&#xff1a;网络豆的主页​​​​​ 前言 本章将会讲解云计算的虚拟化层架构&#xff0c;了解云计算虚拟化层都有哪些架构模式…

Zotero ubuntu2023安装 关联 ubuntu文献翻译

一、准备下载的软件&#xff1a; Zotero | Downloads 1. Zotero-6.0.26_linux-x86_64.tar.bz2 下面是插件 zotfile-5.1.2-fx.xpi zotero-pdf-translate.xpi jasminum-v0.2.6.xpi 2.2.5 Tampermonkey 4.11.crx 所准备的文件&#xff0c;都已经在这个链接的压缩包下面 …

25.10 matlab里面的10中优化方法介绍—— 函数fmincon(matlab程序)

1.简述 关于非线性规划 非线性规划问题是指目标函数或者约束条件中包含非线性函数的规划问题。 前面我们学到的线性规划更多的是理想状况或者说只有在习题中&#xff0c;为了便于我们理解&#xff0c;引导我们进入规划模型的一种情况。相比之下&#xff0c;非线性规划会更加贴近…

代码随想录算法训练营第三十天 | 单调栈系列复习

单调栈系列复习 每日温度未看解答自己编写的青春版重点题解的代码日后再次复习重新写 下一个更大元素 I未看解答自己编写的青春版重点题解的代码日后再次复习重新写 下一个更大元素II未看解答自己编写的青春版重点题解的代码日后再次复习重新写 接雨水未看解答自己编写的青春版…

VUE使用docxtemplater导出word(带图片) 踩坑 表格循环空格 ,canvas.toDataURL图片失真模糊问题

参考&#xff1a;https://www.codetd.com/article/15219743 安装 // 安装 docxtemplater npm install docxtemplater pizzip --save // 安装 jszip-utils npm install jszip-utils --save // 安装 jszip npm install jszip --save // 安装 FileSaver npm install file-save…

深度学习实践——循环神经网络实践

系列实验 深度学习实践——卷积神经网络实践&#xff1a;裂缝识别 深度学习实践——循环神经网络实践 深度学习实践——模型部署优化实践 深度学习实践——模型推理优化练习 代码可见于&#xff1a; 深度学习实践——循环神经网络实践 0 概况1 架构实现1.1 RNN架构1.1.1 RNN架…

mac版窗口管理 Magnet for mac中文最新

magnet mac版是一款运行在苹果电脑上的一款优秀的窗口大小控制工具&#xff0c;拖拽窗口到屏幕边缘可以自动半屏&#xff0c;全屏或者四分之一屏幕&#xff0c;还可以设定快捷键完成分屏。这款专业的窗口管理工具当您每次将内容从一个应用移动到另一应用时&#xff0c;当您需要…

调整数组顺序使奇数位于偶数前面——剑指 Offer 21

文章目录 题目描述法一 两次遍历法二 双指针一次遍历法三 原地交换 题目描述 法一 两次遍历 class Solution{ public:vectro<int> exchange(vector<int>& nums){vector<int> res;for(auto & num : nums){if(num%21){res.push_back(num);}}for(auto &…

【宝藏系列】STM32之C语言基础知识

【宝藏系列】STM32之C语言基础知识 文章目录 【宝藏系列】STM32之C语言基础知识1️⃣位操作2️⃣define宏定义3️⃣ifdef条件编译4️⃣extern变量声明5️⃣typedef类型别名 C语言是单片机开发中的必备基础知识&#xff0c;本文列举了部分 STM32 学习中比较常见的一些C语言基础知…

Java代码连接RabbitMQ服务器

目录 1.添加依赖 2.生产者代码 3.消费者代码 4.效果 1.发送消息 2.消费消息 5.注意 1.添加依赖 <dependency><groupId>com.rabbitmq</groupId><artifactId>amqp-client</artifactId><version>5.12.0</version></dependenc…
最新文章