DevOps-Git

DevOps-Git

Version control software provides complete version management functions for storing and tracking the modification history of directories (folders) and files. The overarching goal of version control software is to support a company's configuration management activities and ultimately the development and maintenance activities of multiple versions, even as the software is released.

Insert image description here

Insert image description here

git installation

Insert image description here

https://git-scm.com/

yum install git -y


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

View parameter help


[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 identity settings

Because git is a distributed version control system, the codes submitted by different people need to be distinguished, so each person needs to set an identity.

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

Create a local repository

  • Working directory: It can also be called the workspace. It is a directory where project code files are stored.
  • Warehouse: version library. When the git init command initializes the working directory, a hidden subdirectory will be generated. git can be understood as the git warehouse or version library.
  • Warehouses are divided into local warehouses and remote warehouses

i3Insert image description here

Insert image description here

Create a local repository

  • Create working directory

    
    [root@workstation ~]# mkdir git_test
    
    
  • Create a local repository in the corresponding working directory

    
    [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
    
    

storage cache

It is a cache area (index/stage) to temporarily save your changes.

If a new file is created in the working directory, the new file needs to be added to the staging area.

Add files to the staging area

  • prepare a file

    [root@workstation git_test]# vi file1.py
    [root@workstation git_test]# cat file1.py
    print("hello git")
    
    
    
  • Submit to the staging area (reverse operation is git rm --cached file1.py)

    [root@workstation git_test]# git add file1.py
    
    
  • Check the .git subdirectory, there is an additional index

    
    [root@workstation git_test]# ls .git/
    branches  config  description  HEAD  hooks  index  info  objects  refs
    
    
  • Use stringsthe command to view the git add file list

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

git version control

Submit files (version 1)

Code file submission needs to be submitted before it can be included in version control.

  • git statusCheck what files need to be submitted in the working directory

    
    [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
    #
    
    
  • Use git commitcommit -m followed by a description of the commit

    
    [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
    
    
  • Check the status again and find that there are no files that need to be submitted.

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

Modify and resubmit (version 2)

  • Modify the file1.py file, I will add a sentence here

    
    [root@workstation git_test]# cat file1.py
    print("hello git")
    print("hello python")
    
    
  • View and notify that file1.py has been modified

    [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")
    
    
  • Use git diffto see what has been modified

    [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")
    
    
  • Modify Submit

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

Modify and submit again (version 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(+)

View commit history

  • git logView submission history version information

    
    [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
    
    
  • Use git log --pretty=onelineto view the submitted historical version information, and the displayed information will be more concise (the previous string can be regarded as the version number)

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

Version rollback and restoration

  • Use git reset --hard HEAD^ to roll back to the previous version (that is, version 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")
    
    
  • Use git reset --hard to the third version number (restore to the third version)

    If you forget what the third version number is, you can use git reflogto view all operation history

    
    [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
    
    
  • Revert to third version

    [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")
    
    
  • Roll back to the previous version. git reset --hard HEAD^^Roll back three versions and so on. git reset --hard HEAD^^^Roll back 100 versions, which can be replaced bygit 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")
    
    

Undo changes

  • Prepare a line or block of code that is written incorrectly

    
    [root@workstation git_test]# cat file1.py
    print("hello git")
    print("hello python")
    print("hello linux")
    print("error code")
    
    
  • Undo policy

    • Remove error code
    • git checkout -- 文件名Undo changes
    • The code is messed up. The temporary storage area is added but has not been commitsubmitted for use . Cancel git reset HEAD 文件名the addition of the temporary storage area, and then git checkout -- 文件名undo the modification.
    • If you write messy code, add a temporary storage area and submit it. Use version rollback.

Accidentally deleted recovery

As long as git addthe temporary storage area is reached, whether it exists or not , you can use to restore it git commitafter accidentally deleting it.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

If the file does not git addreach the temporary storage area and is deleted by mistake, it will be gone.


[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.

File deletion

  • Files that are not git addin the staging area can be deleted directly with rm.

  • git addFiles in the staging area, but no git commitsubmitted files. You need rm to delete the local and git rm file name.

    
    [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 addto the staging area and git commitsubmit the file. You need rm to delete the local, git rm file name, and finally submit for deletion.

    [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 branch management

Problem: Developer A develops a software module, but the development has not been completed yet, so he submits it because he is afraid of losing progress. Developer B does not know that A has not completed it and directly uses the files developed by A, which causes problems.

Solution: Developer A creates a branch of his own. This branch only belongs to A and will not affect others. After development is completed, merge it into the main branch of the project.

Insert image description here

View branches

By default, there is only one master branch. The one with * in front represents the current branch.

[root@workstation git_test]# git branch
* master

Create a branch

git branch 分支名to create a branch

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

switch branch

Use git checkout 分支名toggle


[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

merge branches

Develop a new code in the dev branch, add and submit it

[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

Switch to the master branch, but find that this file does not exist at all


[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

Merge the branches and check again to see it on the master branch.

[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

branch conflict

Some complex situations will cause conflicts. At this time, git cannot help us automatically merge branches. We have to handle conflicts manually.

  • Modify files in the dev branch

    
    [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
    冲突测试
    
    
  • Submit changes on the dev branch

    [root@workstation git_test]# git add file3.py
    [root@workstation git_test]# git commit -m "冲突测试"
    [dev 1b3ec99] 冲突测试
     1 file changed, 1 insertion(+)
    
    
  • Switch back to the master branch and modify the same files

    
    [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
    冲突
    
    
  • Submit changes on the master branch

    [root@workstation git_test]# git add file3.py
    [root@workstation git_test]# git commit -m "冲突测试"
    [master c0e2b26] 冲突测试
     1 file changed, 1 insertion(+)
    
    
  • Merging the dev branch to master will cause conflicts.

    [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.
    
    
  • Manual conflict resolution

    Git uses <<<<<<<<<<,==========,>>>>>>>>symbols to separate conflicting content. Manually remove these symbols and modify them to what you want.

    
    [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
    冲突解决
    
    
  • After resolving conflicts, add and submit and finally merge.

    
    [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.
    
    

delete branch

Use git branch -d 分支名to delete a branch (the current branch cannot be deleted)

[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

Guess you like

Origin blog.csdn.net/weixin_51882166/article/details/131957485