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

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值