zoukankan      html  css  js  c++  java
  • Git的常见基础操作命令

    Git的常见基础操作命令

    1安装初始化

    1.1安装git本地安装Windows版本

    1.2初始化Git用户信息配置

    • 配置git用户,用于数据提交者信息
    Dpad@DESKTOP-9O4NLO5 MINGW64 /D/baolin/Git-bash
    $ git config --global user.name "baolin.li"
    
    • 配置git邮箱,用于数据提交者联系方式
    Dpad@DESKTOP-9O4NLO5 MINGW64 /D/baolin/Git-bash
    $ git config --global user.email "baolin2200@163.com"
    
    • 添加颜色显示
    Dpad@DESKTOP-9O4NLO5 MINGW64 /D/baolin/Git-bash
    $ git config --global color.ui auto
    
    • 调整ASCII字符乱码问题
    Dpad@DESKTOP-9O4NLO5 MINGW64 /D/baolin/Git-bash
    $ git config --global core.quotepath off
    
    • 查看配置
    Dpad@DESKTOP-9O4NLO5 MINGW64 /D/baolin/Git-bash
    $ git config --list
    core.symlinks=false
    core.autocrlf=true
    core.fscache=true
    color.diff=auto
    color.status=auto
    color.branch=auto
    color.interactive=true
    help.format=html
    http.sslcainfo=D:/Program Files/Git/mingw64/ssl/certs/ca-bundle.crt
    diff.astextplain.textconv=astextplain
    rebase.autosquash=true
    credential.helper=manager
    user.name=baolin.li
    user.email=baolin2200@163.com
    color.ui=auto
    core.quotepath=off
    core.repositoryformatversion=0
    core.filemode=false
    core.bare=false
    core.logallrefupdates=true
    core.symlinks=false
    core.ignorecase=true
    core.hidedotfiles=dotGitOnly
    
    • 初始git工作目录:
    Dpad@DESKTOP-9O4NLO5 MINGW64 /D/baolin/Git-bash
    $ git init
    Initialized empty Git repository in D:/baolin/Git-bash/.git/
    

    2.基础语法操作:

    1.将数据提交到本地的管理库中:

    # 将数据提交的缓冲区 指定文件 a.txt
    $ git add a.txt
    
    # 提交缓冲区当前所有文件
    $ git add .
    
    # 提交到本地代码管理库
    $ git commit -m "第一次提交"
    [master (root-commit) fde70b1] 第一次提交
     1 file changed, 0 insertions(+), 0 deletions(-)
     create mode 100644 a.txt
    
    # 查看当前状态
    $ git status
    ... ...
    (use "git add <file>..." to include in what will be committed)
            b.txt
    

    2.查看提交的历史记录 git log 并做代码回滚

    # 查看历史提交记录
    $ git log
    commit ecaf4710f541d2a183ec6ded4ca7b9de5f5cc462 (HEAD -> master)
    Author: baolin2200 <baolin2200@163.com>
    Date:   Mon May 21 15:33:01 2018 +0800
    
        添加两行代码
    
    commit d63ddd2d7d62b68195a0c21a53ccd07bb3a7e1cb
    Author: baolin2200 <baolin2200@163.com>
    Date:   Mon May 21 15:30:17 2018 +0800
    
        第二次提交
    
    commit fde70b1e61063c38dbd27a8a24b565273a181cd1
    Author: baolin2200 <baolin2200@163.com>
    Date:   Mon May 21 15:22:02 2018 +0800
    
        第一次提交
    
    # 回滚到指定的记录 git reset --hard 
    $ git reset --hard d63ddd2d7d62b68195a0c21a53ccd07bb3a7e1cb
    

    3.完整的历史记录 git reflog

    # 查看完整全面的历史记录
    $ git reflog
    d63ddd2 (HEAD -> master) HEAD@{0}: reset: moving to d63ddd2d7d62b68195a0c21a53ccd07bb3a7e1cb
    ecaf471 HEAD@{1}: commit: 添加两行代码
    d63ddd2 (HEAD -> master) HEAD@{2}: commit: 第二次提交
    fde70b1 HEAD@{3}: commit (initial): 第一次提交
    
    # 回到指定的位置
    $ git reset --hard ecaf471
    HEAD is now at ecaf471 添加两行代码
    

    提交和回滚流程

    4.查看当前属于哪个git 库

    git remote -v
    

    5.查看所有分支

    git branch -a
    

    2.1当前状态暂存git stash:

    常见的 git stash 方法

    git stash           将当前工作区所有修改过的内容存储到“某个地方”,将工作区还原到当前版本未修改过的状态
    git stash list      查看“某个地方”存储的所有记录
    git stash clear     清空“某个地方”
    git stash pop       将第一个记录从“某个地方”重新拿到工作区(可能有冲突)
    git stash apply     编号, 将指定编号记录从“某个地方”重新拿到工作区(可能有冲突) 
    git stash drop      编号,删除指定编号的记录
    

    1.将当前开发一半的状态暂存

    $ git stash
    Saved working directory and index state WIP on master: ecaf471 添加两行代码
    

    2.通过pop将最近暂存数据恢复(如遇代码冲突需要手动处理)

    $ git stash pop
    

    3.分支 branch

    1.创建分支

    # 创建分支 dev 
    $ git branch dev
    
    # 创建 bug 分支,并切换到bug分支
    $ git checkout -b bug
    

    2.查看当前有哪些分支

    # 查看当前有哪些分支
    $ git branch
      dev
    * master
    

    3.切换分支

    $ git checkout dev
    Switched to branch 'dev'
    

    4.分支合并merge(将dev分支内容合并到master分支上)

    # 确定自己当前所在的分支为master
    Dpad@DESKTOP-14FUOOK MINGW64 /e/git_bash/git_file (master)
    $ git merge dev
    

    注:

        在出现bug时,应当创建新的bug分支,在新分支上修改错误后测试没问题,merge到master分支;并删除无用的 bug 分支;
    

    5.删除已经无用的分支 -d 分支名

    $ git branch -d bug
    

    6.修复完bug后,切换到dev分支继续开发需要先将线上的代码merge 到dev 进行更新

    # 如果遇到警示,先进行备注后保存
    $ git checkout dev
    $ git merge master
    

    4.GitHub、码云等远程仓库

    4.1代码上传到仓库中

    1.地址:

    https://gitee.com/
    https://github.com/
    ... ...
    

    2.创建远程仓库地址,并设定别名,将 https 的URL地址起个别名为 origin

    git remote add origin https://gitee.com/baolin2200/test.git
    

    3.将本地的 master 分支代码 推到远程 origin 地址

    # -u 指以后如果不写 origin 地址,默认向该地址提交
    $ git push origin master
    
    # 将本地的 dev 分支也推上 仓库来
    $ git push origin dev
    

    4.2代码下载到本地

    1.如果代码为公有代码可以直接下载,默认下载为 master 分支

    # 公有代码直接 clone 下载
    $ git clone https://gitee.com/baolin2200/test.git
    remote: Compressing objects: 100% (55/55), done.
    

    2.如需下载其他分支代码本地需先创建相应分支,并更新该分支

    # 创建并切换分支到 dev 
    $ git checkout -b dev
    
    # 更新 线上的 dev 分支信息到本地
    $ git pull origin dev
    
  • 相关阅读:
    Step by step Dynamics CRM 2013安装
    SQL Server 2012 Managed Service Account
    Step by step SQL Server 2012的安装
    Step by step 活动目录中添加一个子域
    Step by step 如何创建一个新森林
    向活动目录中添加一个子域
    活动目录的信任关系
    RAID 概述
    DNS 正向查找与反向查找
    Microsoft Dynamics CRM 2013 and 2011 Update Rollups and Service Packs
  • 原文地址:https://www.cnblogs.com/baolin2200/p/9183507.html
Copyright © 2011-2022 走看看