zoukankan      html  css  js  c++  java
  • GIT

    git简介

    • linus用c语言编写
    • 2005年诞生
    • 分布式版本管理系统
    • 速度快,适合大规模,跨地区多人协同开发

    git生态

    • git 分布式版本管理系统
    • gitlab git私库解决方案
    • github git共有库解决方案

    git安装

    • 不建议用yum install git,安装的是1.8版本,生产环境2.7以上
    • yum install curl-devel expat-devel gettext-devel openssl-devel zlib-devel gcc perl-ExtUtils-MakeMaker epel-release
    • wget https://github.com/git/git/archive/v2.7.4.zip
    • unzip v2.7.4.zip
    • make prefix=/usr/local/git all
    • make prefix=/usr/local/git install
    • rm -fr /usr/bin/git
    • ln -s /usr/local/git/bin/git /usr/bin/git
    • git --version

    git配置

    • 设置与配置 git config
    • 帮助命令 git help
    • 初始化
    • [root@localhost test]# git init 该目录受版本库控制
    • [root@localhost test]# git config --global user.name "joker" 谁操作
    • [root@localhost test]# git config --global user.email "joker@126.com" 操作者邮箱
    • [root@localhost test]# git config --list
    • user.name=joker
    • user.email=joker@126.com
    • core.repositoryformatversion=0
    • core.filemode=true
    • core.bare=false
    • core.logallrefupdates=true
    • 获取
    • git clone http://xxx.git

    四个区域

    • 给我们的直觉,本地仓库,暂存区域,工作目录就是我们初始化的目录

    四种状态

    • 工作目录新放入的文件叫做为追踪的文件untracked(这个新文件只能说是放到文件里面了,和git库没有任何关联)
    • git add 直接推送暂存区域staged
    • commit 存成一个版本,变成未被修改的状态unmodified,放到了本地仓库
    • 通过修改这个文件 就会从未被修改的状态变成修改的状态Modifed,那修改的文件会从本地仓库重新拉回到工作目录里面来
    • git add 放到了暂存区域staged
    • commit 存成一个版本,放到本地仓库里面

    常用命令

    • git add               加入暂存(索引区)
    • git status                查看状态
    • git status -s            状态概览
    • git diff                    尚未暂存的文件
    • git diff --staged      暂存区文件
    • git commit -m ""      提交更新
    • git reset                  回滚
    • git rm                     从版本库中移除
    • git rm --cached README 从暂存区中移除
    • git mv                     相当于 git rm git add三个命令
    • [root@localhost test]# echo "pay" > pay.html
      [root@localhost test]# echo "center" > news.html
      [root@localhost test]# git add pay.html news.html 
      [root@localhost test]# git status
      On branch master
      Changes to be committed:
        (use "git reset HEAD <file>..." to unstage)
      
              new file:   news.html
              new file:   pay.html
      
      [root@localhost test]# git rm --cached pay.html 
      rm 'pay.html'
      [root@localhost test]# git status
      On branch master
      Changes to be committed:
        (use "git reset HEAD <file>..." to unstage)
      
              new file:   news.html
      
      Untracked files:
        (use "git add <file>..." to include in what will be committed)
      
              pay.html
       

    分支管理

    • 每次切换分支,也就是指针变动的时候

    分支命令

    • git branch
    • git branch –v
    • git branch –merged
    • git branch --no-merged
    • git branch -b testing
    • git checkout
    • git merge              合并,注意指针,在master主分支合并分支
    • git log
    • git stash
    • git tag

    git高级

    git checkout命令

    • git checkout 用于切换分支
    • git checkout -- file.ext 撤销对文件的修改,从本地库里面拉出来覆盖到工作目录
    • [root@localhost test]# echo "222" >> index.html 
      [root@localhost 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:   index.html
      
      no changes added to commit (use "git add" and/or "git commit -a")
      [root@localhost test]# git checkout -- index.html 
      [root@localhost test]# git status
      On branch master
      nothing to commit, working directory clean
      [root@localhost test]# cat index.html 
      joker  

    git reset命令

    意义在于,区域用不用保留

    • --soft 缓存区和工作目录都不会被改变
    • --mixed 默认选项。缓存区和你指定的提交同步,但工作目录不受影响
    • --hard 缓存区和工作目录都同步到你指定的提交
    • 工作目录的回滚
      git checkout -- file.ext 撤销对文件的修改
      暂存区的回滚
      git rm --cached 撤回缓存区的文件

    • [root@localhost test]# echo 333 >> index.html 
      [root@localhost test]# echo 333 >> news.html 
      [root@localhost test]# git add news.html 
      [root@localhost test]# git status
      On branch master
      Changes to be committed:
        (use "git reset HEAD <file>..." to unstage)
      
              modified:   news.html
      
      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:   index.html
      
      [root@localhost test]# git log
      commit c203cce4db08d7787c48cf6fe7f5edda50b7c906
      Merge: fe7194c 885b5d3
      Author: joker <joker@126.com>
      Date:   Mon Dec 17 22:24:56 2018 -0500
      
          Merge branch 'about'
      
      commit 885b5d3dedfc59346535588685f4f41464f85594
      Author: joker <joker@126.com>
      Date:   Mon Dec 17 22:13:14 2018 -0500
      
          about
      
      commit fe7194ca1d692cf55316481b2d34b3de2c354b9e
      Author: joker <joker@126.com>
      Date:   Mon Dec 17 22:05:06 2018 -0500
      
          pay
      
      commit c415b7b4c9d5c7dc6abf00fedca3a0d1307e524f
      Author: joker <joker@126.com>
      Date:   Mon Dec 17 10:06:55 2018 -0500
      
          news
      
      commit 8c8152d501f7eb25a78c4d576a9ff2ce834ae65f
      Author: joker <joker@126.com>
      Date:   Mon Dec 17 10:01:32 2018 -0500
      
          first commit
      [root@localhost test]# git reset --hard c415b7b(回滚到哪一次提交)
      HEAD is now at c415b7b news
      [root@localhost test]# git status
      On branch master
      nothing to commit, working directory clean
      [root@localhost test]# git log
      commit c415b7b4c9d5c7dc6abf00fedca3a0d1307e524f
      Author: joker <joker@126.com>
      Date:   Mon Dec 17 10:06:55 2018 -0500
      
          news
      
      commit 8c8152d501f7eb25a78c4d576a9ff2ce834ae65f
      Author: joker <joker@126.com>
      Date:   Mon Dec 17 10:01:32 2018 -0500
      
          first commit

    文件层操作

    • 运行git reset HEAD foo.py 会将当前的foo.py从缓存区中移除出去,而不会影响工作目录中对foo.py的更改。
    • [root@localhost test]# echo "222" > pay.html
    • [root@localhost test]# git add pay.html
    • [root@localhost test]# git status
    • On branch master
    • Changes to be committed:
    • (use "git reset HEAD <file>..." to unstage)
    • new file: pay.html
    • [root@localhost test]# git reset pay.html

    git reflog

    • 分析你所有分支的头指针的日志来查找出你在重写历史上可能丢失的提交
    • [root@localhost test]# git reflog
      c415b7b HEAD@{0}: reset: moving to c415b7b4c9d5c7dc6abf00fedca3a0d1307e524f
      885b5d3 HEAD@{1}: merge res: Fast-forward
      c415b7b HEAD@{2}: checkout: moving from res to master
      885b5d3 HEAD@{3}: checkout: moving from 885b5d3dedfc59346535588685f4f41464f85594 to res
      885b5d3 HEAD@{4}: checkout: moving from master to 885b5d3
      c415b7b HEAD@{5}: checkout: moving from reset to master
      c415b7b HEAD@{6}: checkout: moving from master to reset
      c415b7b HEAD@{7}: checkout: moving from 885b5d3dedfc59346535588685f4f41464f85594 to master
      885b5d3 HEAD@{8}: checkout: moving from master to 885b5d3
      c415b7b HEAD@{9}: reset: moving to c415b7b
      c203cce HEAD@{10}: merge about: Merge made by the 'recursive' strategy.
      fe7194c HEAD@{11}: checkout: moving from about to master
      885b5d3 HEAD@{12}: checkout: moving from master to about
      fe7194c HEAD@{13}: checkout: moving from about to master
      885b5d3 HEAD@{14}: commit: about
      c415b7b HEAD@{15}: checkout: moving from master to about
      fe7194c HEAD@{16}: commit: pay
      c415b7b HEAD@{17}: checkout: moving from about to master
      c415b7b HEAD@{18}: checkout: moving from master to about
      c415b7b HEAD@{19}: commit: news
      8c8152d HEAD@{20}: commit (initial): first commit
      [root@localhost test]# git checkout 885b5d3
      Note: checking out '885b5d3'.
      
      You are in 'detached HEAD' state. You can look around, make experimental
      changes and commit them, and you can discard any commits you make in this
      state without impacting any branches by performing another checkout.
      
      If you want to create a new branch to retain commits you create, you may
      do so (now or later) by using -b with the checkout command again. Example:
      
        git checkout -b <new-branch-name>
      
      HEAD is now at 885b5d3... about
      [root@localhost test]# git status
      HEAD detached at 885b5d3
      nothing to commit, working directory clean
      [root@localhost test]# git branch rese
      [root@localhost test]# git checkout rese
      Switched to branch 'rese'
      [root@localhost test]# git log
      commit 885b5d3dedfc59346535588685f4f41464f85594
      Author: joker <joker@126.com>
      Date:   Mon Dec 17 22:13:14 2018 -0500
      
          about
      
      commit c415b7b4c9d5c7dc6abf00fedca3a0d1307e524f
      Author: joker <joker@126.com>
      Date:   Mon Dec 17 10:06:55 2018 -0500
      
          news
      
      commit 8c8152d501f7eb25a78c4d576a9ff2ce834ae65f
      Author: joker <joker@126.com>
      Date:   Mon Dec 17 10:01:32 2018 -0500
      
          first commit
      [root@localhost test]# git checkout master
      Switched to branch 'master'
      [root@localhost test]# git log
      commit c415b7b4c9d5c7dc6abf00fedca3a0d1307e524f
      Author: joker <joker@126.com>
      Date:   Mon Dec 17 10:06:55 2018 -0500
      
          news
      
      commit 8c8152d501f7eb25a78c4d576a9ff2ce834ae65f
      Author: joker <joker@126.com>
      Date:   Mon Dec 17 10:01:32 2018 -0500
      
          first commit
      [root@localhost test]# git merge rese
      Updating c415b7b..885b5d3
      Fast-forward
       about.html | 1 +
       1 file changed, 1 insertion(+)
       create mode 100644 about.html
      [root@localhost test]# git log
      commit 885b5d3dedfc59346535588685f4f41464f85594
      Author: joker <joker@126.com>
      Date:   Mon Dec 17 22:13:14 2018 -0500
      
          about
      
      commit c415b7b4c9d5c7dc6abf00fedca3a0d1307e524f
      Author: joker <joker@126.com>
      Date:   Mon Dec 17 10:06:55 2018 -0500
      
          news
      
      commit 8c8152d501f7eb25a78c4d576a9ff2ce834ae65f
      Author: joker <joker@126.com>
      Date:   Mon Dec 17 10:01:32 2018 -0500
      
          first commit  

    远程仓库

    远程命令

    • git clone https://github.com/jokerbj/cmdb.git
    • git pull                         拉下来,合并
    • git fetch                       拉下来,手动合并
    • git push origin master    上传
    • git remote                     查看远程分支
    • git remote –v                查看远程地址
    • git remote add origin http://xxx  添加远程地址
    • git remote show origin
    • git remote rename pb paul 变更远程地址名字
    • git tag -a v1.0 -m ‘abc’    
    • git push origin --delete dev 删除远程分支

    标签管理

    标签

    • git tag 不加任务参数,表示显示标签,并且按时间
    • git tag 标签名,默认是给最近的一次提交打标签
    • git tag 标签名 commitid 给相应的提交打上标签
    • git show 标签名 用于显示跟该标签相关的那次提交的相关信息
    • git tag -d 标签名 删除标签名,不会删除提交
    • git push origin 标签名 把某个标签(必须是本地存在的,否则推送失败)推送到远程服务器

    删除远程标签的步骤

    • 删除本地标签 git tag -d v0.9
    • 再删除远程的 git push origin :refs/tags/v0.9
    • 标签跟commit挂钩,只要commit存在于多个分支,那么这几个分支就都能看到这个标签

     

  • 相关阅读:
    克如斯卡尔 P1546
    真正的spfa
    第四课 最小生成树 要点
    关于vscode中nullptr未定义
    cmake学习笔记
    python学习笔记
    (BFS 图的遍历) 2906. kotori和迷宫
    (图论基础题) leetcode 997. Find the Town Judge
    (BFS DFS 并查集) leetcode 547. Friend Circles
    (BFS DFS 图的遍历) leetcode 841. Keys and Rooms
  • 原文地址:https://www.cnblogs.com/jokerbj/p/10295972.html
Copyright © 2011-2022 走看看