• GIT分布式版本控制系统 | 命令讲解入门


    Git概述

    Git是一个开源的分布式版本控制系统,可以有效、高速地处理从很小到非常大的项目版本管理。 也是Linus Torvalds为了帮助管理Linux内核开发而开发的一个开放源码的版本控制软件;分布式相比于集中式的最大区别在于开发者可以提交到本地,每个开发者通过克隆(git clone),在本地机器上拷贝一个完整的Git仓库。

    Git常用命令

    命令名称作用
    git config --global user.name 用户名设置用户签名
    git config --global user.email 邮箱设置用户邮箱
    git init初始化本地库
    git status查看本地库状态
    git add 文件名添加到缓存区
    git commit -m “日志信息” 文件名提交到本地库
    git reflog查看历史记录
    git reset --hard 版本号版本穿梭

    工作区(写代码) == git add ==》 暂存区(临时存储) == git commit == 》 本地库(历史版本)

    设置用户签名/邮箱

    ws199@DESKTOP-2N1I9JA MINGW64 ~/Desktop
    $ git config --global user.name weishuo
    
    ws199@DESKTOP-2N1I9JA MINGW64 ~/Desktop
    $ git config --global user.email weishuo@weishuo.com
    
    • 1
    • 2
    • 3
    • 4
    • 5

    初始化本地库——git init

    ws199@DESKTOP-2N1I9JA MINGW64 /d/GIT/Git-Space/git.demo
    $ git init
    Initialized empty Git repository in D:/GIT/Git-Space/git.demo/.git/
    
    ws199@DESKTOP-2N1I9JA MINGW64 /d/GIT/Git-Space/git.demo (master)
    $
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7

    查看本地库状态——git status

    ws199@DESKTOP-2N1I9JA MINGW64 /d/GIT/Git-Space/git.demo (master)
    $ git status
    On branch master	#当前本地库分支位置
    
    No commits yet		#目前没有文件要提交
    
    nothing to commit (create/copy files and use "git add" to track) 	#未提交,且无文件提交
    
    
    ws199@DESKTOP-2N1I9JA MINGW64 /d/GIT/Git-Space/git.demo (master)
    $ git status
    On branch master
    
    No commits yet
    
    Untracked files:
      (use "git add ..." to include in what will be committed)
            hello.txt
    
    nothing added to commit but untracked files present (use "git add" to track)
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21

    添加暂存区——git add

    git add 文件名
    ws199@DESKTOP-2N1I9JA MINGW64 /d/GIT/Git-Space/git.demo (master)
    $ git add hello.txt
    warning: LF will be replaced by CRLF in hello.txt.
    The file will have its original line endings in your working directory
    
    
    ws199@DESKTOP-2N1I9JA MINGW64 /d/GIT/Git-Space/git.demo (master)
    $ git status
    On branch master
    
    No commits yet
    
    Changes to be committed:
      (use "git rm --cached ..." to unstage)
            new file:   hello.txt
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16

    文件删除暂存区——rm --cached

    rm --cached 文件名
    #删除暂存区,仍然存在工作区
    ws199@DESKTOP-2N1I9JA MINGW64 /d/GIT/Git-Space/git.demo (master)
    $ git rm --cached hello.txt
    rm 'hello.txt'
    
    • 1
    • 2
    • 3
    • 4

    提交本地库——git commit -m

    将暂存区文件提交到本地库

    git commit -m “日志信息” 文件名
    ws199@DESKTOP-2N1I9JA MINGW64 /d/GIT/Git-Space/git.demo (master)
    $ git commit -m "first commit" hello.txt
    warning: LF will be replaced by CRLF in hello.txt.
    The file will have its original line endings in your working directory
    [master (root-commit) 26da280] first commit
     1 file changed, 7 insertions(+)
     create mode 100644 hello.txt
    
    ws199@DESKTOP-2N1I9JA MINGW64 /d/GIT/Git-Space/git.demo (master)
    $ git status
    On branch master
    nothing to commit, working tree clean
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13

    查看版本信息——git reflog

    ws199@DESKTOP-2N1I9JA MINGW64 /d/GIT/Git-Space/git.demo (master)
    $ git reflog
    26da280 (HEAD -> master) HEAD@{0}: commit (initial): first commit
    #版本号													版本
    
    • 1
    • 2
    • 3
    • 4

    查看版本详细信息——git log

    ws199@DESKTOP-2N1I9JA MINGW64 /d/GIT/Git-Space/git.demo (master)
    $ git log
    commit 26da28016adaee50fc76b4be71fbf2b4b3fda929 (HEAD -> master)
    Author: weishuo <weishuo@weishuo.com>
    Date:   Tue Apr 12 21:53:00 2022 +0800
    
        first commit
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8

    版本穿梭——git reset --hard 版本号

    ws199@DESKTOP-2N1I9JA MINGW64 /d/GIT/Git-Space/git.demo (master)
    $ git reset --hard ffce8cd
    HEAD is now at ffce8cd second commit
    
    ws199@DESKTOP-2N1I9JA MINGW64 /d/GIT/Git-Space/git.demo (master)
    $ git reflog
    ffce8cd (HEAD -> master) HEAD@{0}: reset: moving to ffce8cd
    534cbd3 HEAD@{1}: commit: third commit
    ffce8cd (HEAD -> master) HEAD@{2}: commit: second commit
    26da280 HEAD@{3}: commit (initial): first commit
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10

    分支的操作

    命令名称作用
    git branch 分支名创建分支
    git branch -v查看当前分支
    git checkout 分支名切换分支
    git merge 分支名把指定的分支合并到当前分支上

    查看当前分支——git branch -v

    ws199@DESKTOP-2N1I9JA MINGW64 /d/GIT/Git-Space/git.demo (master)
    $ git branch -v
    * master 534cbd3 third commit
    
    
    • 1
    • 2
    • 3
    • 4

    创建分支——git branch 分支名

    ws199@DESKTOP-2N1I9JA MINGW64 /d/GIT/Git-Space/git.demo (master)
    $ git branch hot-fix
    
    ws199@DESKTOP-2N1I9JA MINGW64 /d/GIT/Git-Space/git.demo (master)
    $ git branch -v
      hot-fix 534cbd3 third commit
    * master  534cbd3 third commit
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8

    切换分支——git checkout 分支名

    ws199@DESKTOP-2N1I9JA MINGW64 /d/GIT/Git-Space/git.demo (master)
    $ git checkout hot-fix
    Switched to branch 'hot-fix'
    
    ws199@DESKTOP-2N1I9JA MINGW64 /d/GIT/Git-Space/git.demo (hot-fix)
    $
    
    ws199@DESKTOP-2N1I9JA MINGW64 /d/GIT/Git-Space/git.demo (hot-fix)
    $ git branch -v
    * hot-fix 534cbd3 third commit
      master  534cbd3 third commit
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11

    合并分支——git merge 分支名

    ws199@DESKTOP-2N1I9JA MINGW64 /d/GIT/Git-Space/git.demo (master)
    $ git merge hot-fix
    Updating 534cbd3..017517e
    Fast-forward
     hello.txt | 13 +++----------
     1 file changed, 3 insertions(+), 10 deletions(-)
    
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8

    合并分支(冲突合并)

    检测有文件有两处修改

    显示冲突
    ws199@DESKTOP-2N1I9JA MINGW64 /d/GIT/Git-Space/git.demo (master)
    $ git merge hot-fix
    Auto-merging hello.txt
    CONFLICT (content): Merge conflict in hello.txt
    Automatic merge failed; fix conflicts and then commit the result.
    
    ws199@DESKTOP-2N1I9JA MINGW64 /d/GIT/Git-Space/git.demo (master|MERGING)
    $
    
    ws199@DESKTOP-2N1I9JA MINGW64 /d/GIT/Git-Space/git.demo (master|MERGING)
    $ git status
    On branch master
    You have unmerged paths.
      (fix conflicts and run "git commit")
      (use "git merge --abort" to abort the merge)
    
    Unmerged paths:
      (use "git add ..." to mark resolution)
            both modified:   hello.txt
    
    no changes added to commit (use "git add" and/or "git commit -a")
    
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    冲突合并
    # 1.查看冲突代码
    ws199@DESKTOP-2N1I9JA MINGW64 /d/GIT/Git-Space/git.demo (master|MERGING)
    $ vim hello.txt
    # <<<< HEAD  当前分支代码
    # <<<< HEAD 当前分支代码	=======
    # ======= 合并分支代码   >>>>>>> hot-fix
    #删除特殊符号 以及 重复代码
    
    #2.添加到暂存区
    ws199@DESKTOP-2N1I9JA MINGW64 /d/GIT/Git-Space/git.demo (master|MERGING)
    $ git add hello.txt
    
    #3.提交本地库(不能带文件名)
    ws199@DESKTOP-2N1I9JA MINGW64 /d/GIT/Git-Space/git.demo (master|MERGING)
    $ git commit -m "marge test"
    fatal: cannot do a partial commit during a merge.
    
    #4.切换分支查看
    ws199@DESKTOP-2N1I9JA MINGW64 /d/GIT/Git-Space/git.demo (master)
    $ git checkout hot-fix
    Switched to branch 'hot-fix'
    
    #冲突合并,合并当前分支,不合并被合并分支
    # head ---> master(分支内容)--->版本号
    # HEAD决定当前分支
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25

    Git团队协作

    在这里插入图片描述

    在这里插入图片描述

    命令名称作用
    git remote -v查看当前所有进程地址别名
    git remote add 别名 远程地址起别名
    git push 别名 分支推送本地分支上的内容到远程仓库
    git push 远程地址 分支推送本地分支上的内容到远程仓库
    git clone 远程地址将远程仓库的内容克隆到本地
    git pull 远程库地址别名 远程分支名将远程仓库对于分支最新内容拉下来后与当前本地分支直接合并
    git push -f 远程库地址别名 远程分支名强制推送

    创建远程库别名

    git remote -v #查看当前所有进程地址别名
    git remote add 别名 远程地址 #起别名

    ws199@DESKTOP-2N1I9JA MINGW64 /d/GIT/Git-Space/git.demo (hot-fix)
    $ git remote -v
    
    ws199@DESKTOP-2N1I9JA MINGW64 /d/GIT/Git-Space/git.demo (hot-fix)
    $ git remote add git.demo https://github.com/weishuoHH/git-demo.git
    
    ws199@DESKTOP-2N1I9JA MINGW64 /d/GIT/Git-Space/git.demo (hot-fix)
    $ git remote -v
    git.demo        https://github.com/weishuoHH/git-demo.git (fetch)
    git.demo        https://github.com/weishuoHH/git-demo.git (push)
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11

    推送本地分支上的内容到远程仓库

    git push 别名 分支 #推送本地分支上的内容到远程仓库

    ws199@DESKTOP-2N1I9JA MINGW64 /d/GIT/Git-Space/git.demo (master)
    $ git push git.demo master
    Enumerating objects: 21, done.
    Counting objects: 100% (21/21), done.
    Delta compression using up to 16 threads
    Compressing objects: 100% (14/14), done.
    Writing objects: 100% (21/21), 1.55 KiB | 792.00 KiB/s, done.
    Total 21 (delta 4), reused 0 (delta 0), pack-reused 0
    remote: Resolving deltas: 100% (4/4), done.
    To https://github.com/weishuoHH/git-demo.git
     * [new branch]      master -> master
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12

    拉取远程库到本地库

    git pull 远程库地址别名 远程分支名

    $ git pull git@gitee.com:wei-shuo-red/travel.git master --allow-unrelated-histories
    
    • 1
    ws199@DESKTOP-2N1I9JA MINGW64 /d/GIT/Git-Space/git.demo (master)
    $ git pull git.demo master
    remote: Enumerating objects: 5, done.
    remote: Counting objects: 100% (5/5), done.
    remote: Compressing objects: 100% (2/2), done.
    remote: Total 3 (delta 1), reused 0 (delta 0), pack-reused 0
    Unpacking objects: 100% (3/3), 649 bytes | 64.00 KiB/s, done.
    From https://github.com/weishuoHH/git-demo
     * branch            master     -> FETCH_HEAD
       b129c29..d40c0cd  master     -> git.demo/master
    Updating b129c29..d40c0cd
    Fast-forward
     hello.txt | 1 +
     1 file changed, 1 insertion(+)
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15

    克隆远程库到本地库

    git clone 远程地址 将远程仓库的内容克隆到本地

    clone操作:
    1.拉取代码
    2.初始化本地库
    3.创建别名

    ws199@DESKTOP-2N1I9JA MINGW64 /d/GIT/Git-lhc
    $ git clone https://github.com/weishuoHH/git-demo.git
    Cloning into 'git-demo'...
    remote: Enumerating objects: 27, done.
    remote: Counting objects: 100% (27/27), done.
    remote: Compressing objects: 100% (14/14), done.
    remote: Total 27 (delta 6), reused 20 (delta 4), pack-reused 0
    Receiving objects: 100% (27/27), done.
    Resolving deltas: 100% (6/6), done.
    
    ws199@DESKTOP-2N1I9JA MINGW64 /d/GIT/Git-lhc
    $ ll
    total 0
    drwxr-xr-x 1 ws199 197609 0 Apr 17 14:27 git-demo/
    
    #创建别名
    ws199@DESKTOP-2N1I9JA MINGW64 /d/GIT/Git-lhc/git-demo (master)
    $ git remote  -v
    origin  https://github.com/weishuoHH/git-demo.git (fetch)
    origin  https://github.com/weishuoHH/git-demo.git (push)
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20

    GitHub免密登录

    生成ssh免密登录命令 -t "以某种加密算法登录 -C "描述

    ws199@DESKTOP-2N1I9JA MINGW64 ~
    $ ssh-keygen.exe -t rsa -C weishuoHH@weishuo.com
    Generating public/private rsa key pair.
    Enter file in which to save the key (/c/Users/ws199/.ssh/id_rsa):
    Created directory '/c/Users/ws199/.ssh'.
    Enter passphrase (empty for no passphrase):
    Enter same passphrase again:
    Your identification has been saved in /c/Users/ws199/.ssh/id_rsa
    Your public key has been saved in /c/Users/ws199/.ssh/id_rsa.pub
    The key fingerprint is:
    SHA256:CVIbj1aIxRUz8t0m7WXpzBqwJ6RzNHze+B5dd5Jyiwk weishuoHH@weishuo.com
    The key's randomart image is:
    +---[RSA 3072]----+
    |     +=o*.       |
    |    ..oO = o   . |
    |    . = o O = +  |
    |     o . = X O . |
    |        S E B O +|
    |         o + O ++|
    |            + + .|
    |             . . |
    |              .  |
    +----[SHA256]-----+
    
    ws199@DESKTOP-2N1I9JA MINGW64 ~
    $
    
    #通过ssh拉去远程库代码
    ws199@DESKTOP-2N1I9JA MINGW64 /d/GIT/Git-Space/git.demo (master)
    $ git pull git@github.com:weishuoHH/git-demo.git master
    The authenticity of host 'github.com (20.205.243.166)' can't be established.
    ED25519 key fingerprint is SHA256:+DiY3wvvV6TuJJhbpZisF/zLDA0zPMSvHdkr4UvCOqU.
    This key is not known by any other names
    Are you sure you want to continue connecting (yes/no/[fingerprint])? y
    Please type 'yes', 'no' or the fingerprint: yes
    Warning: Permanently added 'github.com' (ED25519) to the list of known hosts.
    From github.com:weishuoHH/git-demo
     * branch            master     -> FETCH_HEAD
    Already up to date.
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
  • 相关阅读:
    ZYNQ之中断机制
    信息化发展24
    LeetCode50天刷题计划(Day 25— 旋转图像(11.20-12.30)
    数据结构从入门到精通——算法的时间复杂度和空间复杂度
    城市排水监测方案(dtu终端配合工业路由器精准监测)
    Python综合练习题
    【Java牛客刷题】入门篇(02)
    不知不觉做测试也两年了,该学点什么才能更有发展前景~
    移动魔百盒CM311-1sa_ZG代工_S905L3A 安卓9.0 鸿蒙动画_线刷固件包
    易语言编程之CE过驱动保护(ACE)调试教程
  • 原文地址:https://blog.csdn.net/weixin_62765017/article/details/128166435