• Git学习记录


    Introduction

    git 是一款版本管理软件,适用目前绝大多数操作系统;Github 是一个代码托管平台,与 Git 没有任何关系,只不过 Git 可以基于 Github 进行分布式云存储与交互,因此往往需要结合二者从而达到相对良好的 Teamwork 状态。本文是我基于 Git 的版本管理学习记录,涉及到的指令只是冰山一角,但是使用频率较高。详细的指令请跳转至官方教学 https://git-scm.com/book/zh/v2

    全文分为两个部分,分别为 Git 版本管理的 Architecture 与 Git 的相关命令。其中 Architecture 使用 Xmind 绘制,Git 相关命令采用 Git Bash 模拟 Unix 命令行终端。本地 OS 为 Microsoft Windows 11

    Architecture

    工作区暂存区仓库区
    Git Architecture

    Command

    零、常用命令

    # 从服务器克隆仓库
    git clone https://github.com/<UserName>/<ProjectName>.git
    
    • 1
    • 2
    # 查看当前文件状态
    git status
    
    • 1
    • 2
    # 从当前版本开始查询 commit 日志
    git log
    
    • 1
    • 2
    # 查看所有 commit 日志
    git reflog
    
    • 1
    • 2

    一、配置

    1.1 初始化
    # 初始化仓库
    git init
    
    • 1
    • 2
    1.2 查看配置
    # 查看 git 配置信息
    git config --list
    
    • 1
    • 2
    # 查看 git 用户名、密码、邮箱的配置
    git config user.name
    git config user.password
    git config user.email
    
    • 1
    • 2
    • 3
    • 4
    # 查看代理
    git config --global --get http.proxy
    git config --global --get https.proxy
    
    • 1
    • 2
    • 3
    1.3 编辑配置
    # 配置(修改) Email & Pwd & Username (局部)
    git config user.name "xxx"
    git config user.password "xxx"
    git config user.email "xxx@xxx.com"
    
    # 配置(修改) Email & Pwd & Username (全局)
    git config --global user.name xxx
    git config --global user.password xxx
    git config --global user.email "xxx@xxx.com"
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    # 配置代理
    git config --global http.proxy 127.0.0.1:<VpnPort>
    git config --global https.proxy 127.0.0.1:<VpnPort>
    
    # 取消代理
    git config --global --unset http.proxy
    git config --global --unset https.proxy
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    # 连接远程服务器
    git remote add <RemoteName> https://github.com/用户名/仓库名.git
    
    # 查看所有连接的远程
    git remote -v
    
    # 修改远程别名
    git remote rename <OldRemoteName> <NewRemoteName>
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8

    二、迭代

    2.1 工作区到暂存区
    # 工作区到暂存区(单文件)
    git add <FileName>
    
    # 工作区到暂存区(全部变动文件)
    git add .
    
    • 1
    • 2
    • 3
    • 4
    • 5
    2.2 暂存区到仓库区
    # 暂存区到仓库区
    git commit -m ''
    
    • 1
    • 2
    2.3 仓库区到服务器
    # 仓库区到云服务器(常规方法)
    git push <RemoteName> <BranchName>
    
    # 仓库区到云服务器(初始配置仓库推送默认地址)
    git push -u <RemoteName> <BranchName>
    
    # 仓库区到云服务器(已配置默认推送地址后)
    git push
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    # 强制覆盖推送
    git push --force <RemoteName> <BranchName>
    
    • 1
    • 2

    三、回溯

    3.1 工作区到未修改状态
    # 取消修改
    git checkout -- <FileName>
    
    • 1
    • 2
    3.2 暂存区到工作区状态
    # 取消 add(一个文件)
    git reset -- <FileName>
    
    # 取消 add(全部文件)
    git reset
    
    • 1
    • 2
    • 3
    • 4
    • 5
    3.3 仓库区到暂存区状态
    # 取消 commit 直接到工作区
    git reset --hard '<版本序列>'
    
    # 取消上一次 comment 并进入 vim 编辑模式
    git commit --amend
    
    • 1
    • 2
    • 3
    • 4
    • 5
    3.4 取消服务器的修改
    # 删除本地文件后重新推送(单文件)
    git rm <FileName>
    git push
    
    # 删除本地文件后重新推送(文件夹)
    git rm -r <FolderName>
    git push
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    # 取消 push
    # 本地回溯 comment 到某一个版本,重新 push
    
    • 1
    • 2
    # 取消某些文件的版本管理
    # 参考 https://www.cnblogs.com/zhangcaihua/p/15261392.html
    
    • 1
    • 2

    四、分支

    4.1 创建分支
    # 创建分支
    git branch <BranchName>
    
    # 远程同步
    git push <RemoteName> <BranchName>
    
    • 1
    • 2
    • 3
    • 4
    • 5
    4.2 删除分支
    # 切换到另一个分支再进行删除操作
    git switch <AnotherBranchName>
    git branch -d <BranchName>
    
    # 远程同步
    git push <RemoteName> --delete <BranchName>
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    4.3 修改分支

    如果修改的分支为远程保护分支,则在远程更新之前,需要在远程相应的服务商家那里对保护分支进行重新设定

    # 修改名称
    git branch -m <OldName> <NewName>
    
    # 远程同步
    git push <RemoteName> <NewName>
    git push <RemoteName> --delete <OldName>
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    4.4 合并分支
    # 首先将当前分支切换到需要被合并的分支 ,接着合并需要被合并的分支 
    git merge <TodoBranch>
    
    • 1
    • 2
  • 相关阅读:
    机器人编程学习有哪些好处?
    win10系统x64安装java环境以及搭建自动化测试环境
    zemax埃尔弗目镜
    【Java集合类面试十八】、ConcurrentHashMap是怎么分段分组的?
    Rust安装(windows)
    智慧公厕高精尖技术揭秘,让卫生管理更智能、更舒适
    解决IDEA控制台中文乱码问题(Tomcat、动态网页项目)
    两台Linux文件夹单向同步【inotify、rsync、ssh】
    【笔记】期末的jsp复习之数据库
    关于修改了mysql的my_conf文件之后,不能生效问题
  • 原文地址:https://blog.csdn.net/qq_73408594/article/details/132486321