zoukankan      html  css  js  c++  java
  • git的基本使用

    一 安装

     安装分为两种,一种是通过编译源代码来安装;另一种是使用为特定平台预编译好的安装包。

     yum install git
     brew install git

     如果你碰巧用Debian或Ubuntu Linux,通过一条sudo apt-get install git就可以直接完成Git的安装,非常简单。

     在window系统上面的安装:下载网址:https://git-scm.com/

        打开浏览器输入Git官网网站回车即可打开Git官网;
        点击里面的“Downloads for Windows”即调整到下载页面等待下载即可,现在最新版本为2.16.2
        我下载的安装包名为Git-2.16.2-64-bit.exe。
        双击既打开安装设置界面。然后点击“Next”;
        配置安装组件,默认即可,点击“Next”;默认已经都帮我们否选上了。包括创建桌面快捷方式、Git Bash、Git GUi、已经目录和后缀关联等;
        在“Adjusting your PATH environment”选项中,默认即可。点击“Next”;
        (这个步骤最好选择第二项“Use Git from the Windows Command Prompt”, 这样在Windows的命令行cmd中也可以运行git命令了。这样会对以后的一些操作带来方便);
        配置行结束标记,保持默认“Checkout Windows-style, commit Unix-style line endings”;
        在终端模拟器选择页面,默认即可;
        配置Git额外选择默认即可;
        安装完成,默认点”Finish”即可。
        安装完成后,还需要最后一步设置,在命令行输入:
    View Code

    二 版本控制软件

     SAN详细介绍:http://www.chinastor.com/baike/storage/0205200422016.html

     VSS详细介绍:https://baike.baidu.com/item/VSS%E4%BD%BF%E7%94%A8%E6%89%8B%E5%86%8C/7186209

     CVS详细介绍:https://blog.csdn.net/kenll/article/details/1526199

     Git:是一个开源的分布式版本控制软件,用以有效、高速的处理从很小到非常大的项目版本管理。 Git 最初是由Linus Torvalds设计开发的,用于管理Linux内核开发。Git 是根据GNU通用公共许可证版本2的条款分发的自由/免费软件,安装参见:http://git-scm.com/

      Github:是一个基于Git的远程文件托管平台(同GitCafe、BitBucket和GitLab等)。

      Git本身完全可以做到版本控制,但其所有内容以及版本记录只能保存在本机,如果想要将文件内容以及版本记录同时保存在远程,则需要结合GitHub来使用。

      使用场景:

        无GitHub:在本地 .git 文件夹内维护历时文件
        有GitHub:在本地 .git 文件夹内维护历时文件,同时也将历时文件托管在远程仓库
    
    其他:
    
        集中式:远程服务器保存所有版本,用户客户端有某个版本
        分布式:远程服务器保存所有版本,用户客户端有所有版本
    View Code

     版本控制工具包含两部分:

      客户端(本地):本地编写内容以及版本信息

      服务端(网盘):将内容和版本同时保存在远程(可有可无)

    三 使用

     某文件中的内容进行版本管理:

      首先进入文件夹

       用户登陆:

        $ git config --global user.name "Your Name"

        $ git config --global user.email "email@example.com"

       git init   :初始化,当前所有的文件夹可以被管理并且以后版本相关的数据都会存储到.get文件中去  

       git status  : 查看当前文件夹以及子目录中文件是否发生变化,内容修改/新增文件/删除

       git add  : 给发生变化的文件(贴上一个标签)或者将要发生变化的文件放到某个地方

       git commit -m 'xxxxxxxxxx'  :将‘绿色’的文件添加到版本中

       git log  :查看所有的版本提交记录

       git reset --hard  版本  :回退

       git reflog  : 插看所有的分支的所有的操作记录

    MacBook-Pro-4:pondo wupeiqi$ ls
    a.py        app01       db.sqlite3  manage.py   pondo       readme      static      templates
    
    MacBook-Pro-4:pondo wupeiqi$ git status                   # 文件颜色为红色,表示在工作区的被修改状态
    On branch master
    Untracked files:
      (use "git add <file>..." to include in what will be committed)
    
        a.py
    nothing added to commit but untracked files present (use "git add" to track)
    
    MacBook-Pro-4:pondo wupeiqi$ git add .                    # 将所有相较上一次版本之后所有的修改添加到暂存状态
    
    MacBook-Pro-4:pondo wupeiqi$ git status                   # 文件颜色为绿色,表示在版本库的暂存状态
    On branch master
    Changes to be committed:
      (use "git reset HEAD <file>..." to unstage)
    
        new file:   a.py
    
    MacBook-Pro-4:pondo wupeiqi$ git commit -m '又一次提交'     # 提交到版本库的分支 
    [master f139d5d] 又一次提交
     1 file changed, 0 insertions(+), 0 deletions(-)
     create mode 100644 a.py
     
    MacBook-Pro-4:pondo wupeiqi$ git log                      # 查看历史版本提交记录(根据版本commit值可以进行回滚)
    commit f139d5d0a648af06d8a1ecadd90faf572afc388a
    Author: 武沛齐 <you@example.com>
    Date:   Fri Aug 11 10:02:14 2017 +0800
    
        又一次提交
    
    commit df47fe49fc1f14d9cdd1534baa96f46ec71a9934
    Author: 武沛齐 <you@example.com>
    Date:   Fri Aug 11 08:49:49 2017 +0800
    
        第一次提交
    View Code

     其他使用:

      git diff 文件名  : 查看上次修改前的数据

      git rm 文件名  : 删除某个文件,也相当于修改了版本库

      git checkout -- 文件名  :假如误删了文件,因为版本库里面还有了,这样就可以将误删的文件恢复到最新的版本 

      mkdir  目录名 :创建一个空的文件夹

      cd  :切换

      ls  : 查看源程序目录

      touch 文件名.py  :创建新文件   

     Git把管理的文件分为了两个区域四个状态。

      

     git回滚功能:

      

      回滚到指定的版本:

    MacBook-Pro-4:pondo wupeiqi$ git log
    commit 0972f4bb43104baee15aeec2dd62bd0a307ec837
    Author: 武沛齐 <wupeiqi@live.com>
    Date:   Fri Aug 11 10:54:42 2017 +0800
     
        非洲专区上线
     
    commit 6c439d2fd0d943f36f3ee84e158ff86b052961d2
    Author: 武沛齐 <wupeiqi@live.com>
    Date:   Fri Aug 11 10:42:09 2017 +0800
     
        项目首次移植到Git控制版本
     
    MacBook-Pro-4:pondo wupeiqi$ git reset --hard 6c439d2fd0d943f36f3ee84e158ff86b052961d2
    HEAD is now at 6c439d2 项目首次移植到Git控制版本
     
    # 命令执行完,工作区的所有文件就变成未开发非洲专区功能之前了,太爽了有么有....
    View Code

      在回滚回去:

    MacBook-Pro-4:pondo wupeiqi$ git reflog
    6c439d2 HEAD@{2}: reset: moving to 6c439d2fd0d943f36f3ee84e158ff86b052961d2
    0972f4b HEAD@{3}: commit: 非洲专区上线
    6c439d2 HEAD@{4}: commit (initial): 项目首次移植到Git控制版本
     
    MacBook-Pro-4:pondo wupeiqi$ git reset --hard 0972f4b
    HEAD is now at 0972f4b 非洲专区上线
    View Code

     紧急修改bug:

      基于stash:

       git stash:用于将工作区发生变化的所有文件获取临时存储在“某个地方”,将工作区还原当前版本未操作前的状态;stash还可以将临时存储在“某个地方”的文件再次拿回到工作区。

    acBook-Pro-4:pondo wupeiqi$ vim app01/views.py             # 开发直播功能,刚开发到一半
     
    MacBook-Pro-4:pondo wupeiqi$ 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:   app01/views.py
     
    no changes added to commit (use "git add" and/or "git commit -a")
     
    MacBook-Pro-4:pondo wupeiqi$ git stash                     # 将开发到一半的直播功能,临时存储到“某个地方”
    Saved working directory and index state WIP on master: 0972f4b 非洲专区上线
    HEAD is now at 0972f4b 非洲专区上线
     
    MacBook-Pro-4:pondo wupeiqi$ git status                    # 工作区回到当前版本未做任何操作前
    On branch master
    nothing to commit, working tree clean
     
    MacBook-Pro-4:pondo wupeiqi$ vim pondo/settings.py         # 紧急修复bug
    MacBook-Pro-4:pondo wupeiqi$ 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:   pondo/settings.py
    no changes added to commit (use "git add" and/or "git commit -a")
     
    MacBook-Pro-4:pondo wupeiqi$ git add .                     # 添加到修改bug的代码到暂存状态
    MacBook-Pro-4:pondo wupeiqi$ git commit -m '紧急修复bug'     # 提交修复Bug的代码到分支
    [master 1300d33] 紧急修复bug
     1 file changed, 1 insertion(+)
     
    MacBook-Pro-4:pondo wupeiqi$ git stash pop                 # 将开发到一半的直播功能从“某个地方”再次拿会工作区继续开发
    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:   app01/views.py
     
    no changes added to commit (use "git add" and/or "git commit -a")
    Dropped refs/stash@{0} (059d78ca8fa204f9559bd3ce0ae76235969b4301)
    View Code

       自己修改bug

       git stash pop :这一步有可能出现冲突,因为在紧急修复bug的代码和通过stash存储在“某个地方”的代码会有重合部分,所以执行 git stash pop 时候就会出现冲突,

    a. 原来内容:
        from django.shortcuts import render,HttpResponse
    
        def index(request):
            return render(request,'index.html')
    
        def africa(request):
            return HttpResponse('非洲专区')
    
        
    b. 开发到一半直播功能:
        from django.shortcuts import render,HttpResponse
    
        def index(request):
            return render(request,'index.html')
    
        def africa(request):
            return HttpResponse('非洲专区')
    
    
        def live(request):
            print('开发到一半')
            return HttpResponse('....')
    
    
    c. 执行git stash,回到当前版本未修改状态:
        from django.shortcuts import render,HttpResponse
    
        def index(request):
            return render(request,'index.html')
    
        def africa(request):
            return HttpResponse('非洲专区')
    
    d. 修复Bug并提交:
        from django.shortcuts import render,HttpResponse
    
        def index(request):
            return render(request,'index.html')
    
        def africa(request):
            return HttpResponse('非洲xxxxx专区')
    
    
    e. 继续开发直播功能 git stash pop,此时会出现冲突:
        MacBook-Pro-4:pondo wupeiqi$ git stash pop
        Auto-merging app01/views.py
        CONFLICT (content): Merge conflict in app01/views.py
    
        表示app01/views.py存在冲突需要解决,此时文件内容为:
    
        from django.shortcuts import render,HttpResponse
    
            def index(request):
                return render(request,'index.html')
    
            def africa(request):
            <<<<<<< Updated upstream:               # 修复Bug时更改的内容
                return HttpResponse('非洲xxxx区')  
            =======                                  # 修复Bug前正在开发新功能时的内容
                return HttpResponse('非洲专区')
    
            def live(request):
                print('刚开发到一半')
                return HttpResponse('直播功能')
            >>>>>>> Stashed changes
    
    
        需要自行解决冲突,然后继续开发,如:
    
        from django.shortcuts import render,HttpResponse
    
            def index(request):
                return render(request,'index.html')
    
            def africa(request):
            
                return HttpResponse('非洲xxxx区')  
            
            def live(request):
                print('刚开发到一半')
                return HttpResponse('直播功能')
    View Code

      stash相关常用命令:

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

      基于多分支:branch

       branch称为分支,默认仅有一个名为master的分支。一般开发新功能流程为:开发新功能时会在分支dev上进行,开发完毕后再合并到master分支。

        

    MacBook-Pro-4:pondo wupeiqi$ git branch dev                 # 创建新分支,即:拷贝一份当前所在分支代码到新分支
    MacBook-Pro-4:pondo wupeiqi$ git checkout dev               # 切换到dev分支
    MacBook-Pro-4:pondo wupeiqi$ vim app01/views.py             # 开发功能
    MacBook-Pro-4:pondo wupeiqi$ git status                     # 查看状态,即:在dev分支修改了app01/views.py文件
    On branch dev
    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:   app01/views.py
    
    no changes added to commit (use "git add" and/or "git commit -a")
    MacBook-Pro-4:pondo wupeiqi$ git add .                      # 将修改文件添加到版本库的暂存区
    MacBook-Pro-4:pondo wupeiqi$ git commit -m '新功能开发完毕'    # 将暂存区的内容提交到当前所在分支,即:dev分支
    [dev 32b40cd] 新功能开发完毕
     1 file changed, 2 insertions(+) 
    MacBook-Pro-4:pondo wupeiqi$ git checkout master             # 切换回master分支 
    Switched to branch 'master'
    MacBook-Pro-4:pondo wupeiqi$ git merge dev                   # 将dev分支内容合并到master分支
    Updating 0972f4b..32b40cd
    Fast-forward
     app01/views.py | 2 ++
     1 file changed, 2 insertions(+)
    View Code

       遇到上文开发到一般需要临时修复Bug的情况,可以按照下图的流程进行:

        

    MacBook-Pro-4:pondo wupeiqi$ git branch                     # 当前在master分支
    * master
     
     
    MacBook-Pro-4:pondo wupeiqi$ git branch dev                 # 创建dev分支用于开发新功能
     
    MacBook-Pro-4:pondo wupeiqi$ git checkout dev               # 切换到dev分支
    Switched to branch 'dev'
     
    MacBook-Pro-4:pondo wupeiqi$ vim app01/views.py             # 开发新功能到一半,需要紧急修复Bug
     
    MacBook-Pro-4:pondo wupeiqi$ git add .
     
    MacBook-Pro-4:pondo wupeiqi$ git commit -m '新功能开发一半'
    [dev b3ac2cb] 新功能开发一半
     1 file changed, 2 insertions(+)
     
     
     
     
    MacBook-Pro-4:pondo wupeiqi$ git checkout master            # 切换回master分支
    Switched to branch 'master'
     
    MacBook-Pro-4:pondo wupeiqi$ git branch bug                 # 创建bug分支
     
    MacBook-Pro-4:pondo wupeiqi$ git checkout bug               # 切换到bug分支
    Switched to branch 'bug'
     
    MacBook-Pro-4:pondo wupeiqi$ vim pondo/settings.py          # 修改bug
     
    MacBook-Pro-4:pondo wupeiqi$ git add .                      # 提交bug
     
    MacBook-Pro-4:pondo wupeiqi$ git commit -m '紧急修复bug'      # 提交bug
    [bug f42f386] 紧急修复bug
     1 file changed, 1 insertion(+), 1 deletion(-)
     
     
    MacBook-Pro-4:pondo wupeiqi$ git checkout master            # 切换会master
    Switched to branch 'master'
     
    MacBook-Pro-4:pondo wupeiqi$ git merge bug                  # 将bug分支内容合并到master分支,表示bug修复完毕,可以上线
    Updating 0972f4b..f42f386
    Fast-forward
     pondo/settings.py | 2 +-
     1 file changed, 1 insertion(+), 1 deletion(-)
     
     
     
     
    MacBook-Pro-4:pondo wupeiqi$ git checkout dev               # 切换到dev分支,继续开发新功能
    Switched to branch 'dev'
     
    MacBook-Pro-4:pondo wupeiqi$ vim app01/views.py             # 继续开发其他一半功能
     
    MacBook-Pro-4:pondo wupeiqi$ git add .                      # 提交新功能
     
    MacBook-Pro-4:pondo wupeiqi$ git commit -m '继续开发完成'      # 提交功能
    [dev c0bfb27] 继续开发完成
     1 file changed, 1 insertion(+)
     
    MacBook-Pro-4:pondo wupeiqi$ git checkout master            # 切换回master分支
    Switched to branch 'master'
     
    MacBook-Pro-4:pondo wupeiqi$ git merge dev                  # 将dev分支合并到master分支
    Merge made by the 'recursive' strategy.
     app01/views.py | 3 +++
     1 file changed, 3 insertions(+)
    View Code

         git merge 时也可能会出现冲突,解决冲突的方式上述stash相同,即:找到冲突文件,手动修改冲突并提交,此处不再敖述

       branch相关常用命令:

        git branch 分支名称             创建分支
        git checkout 分支名称          切换分支
        git branch -m 分支名称        创建并切换到指定分支
        git branch                          查看所有分支
        git branch -d 分支名称         删除分支
        git merge 分支名称              将指定分支合并到当前分支
    View Code

     代码仓库:

      GitHub,一个基于Git实现的代码托管的平台,可以将内容以及版本记录在远程也保存一份,这样就不用U盘咯(类似于云盘)。PS: 类似GitHub的产品还有许多,如:GitLab、Bitbucket、码云等

       

     将本地版本推到代码仓库:

      git remote add origin 代码仓库的网址  :为地址去一个别名  origin

      git push origin master  :开始进行推送

      git clone 项目网址  :将项目从某个代码仓库获取

      git pull origin  分支  : 下载远程代码块的dev分支代码及快速合并

       git fetch origin dev  :从代码库获取dev分支最新内容到到版本库的分支

       git merge origin/dev  :将版本库的分支内容合并到工作区

     基于GitHub实现代码托管,需要一下步骤:

      注册GitHub

      创建仓库,创建完仓库后会有一个URL代指该仓库,如:

        

      git可以是用该URL进行向远程推送版本信息或获取版本信息

       

     基于GitHub进行代码远程托管。

      在家里,小P开发完毕部分功能将代码推送到GitHub。 

    MacBook-Pro-4:pondo wupeiqi$ git remote add origin https://github.com/WuPeiqi/pondo.git   # 为地址起一个别名origin
    MacBook-Pro-4:pondo wupeiqi$ git push origin master              # 将本地master分支内容以及版本信息推送到GitHub
    Username for 'https://github.com':                               # 输入GitHub用户名
    Password for 'https://wupeiqi@github.com':                       # 输入GitHub密码
    Counting objects: 2, done.
    Delta compression using up to 4 threads.
    Compressing objects: 100% (2/2), done.
    Writing objects: 100% (2/2), 270 bytes | 0 bytes/s, done.
    Total 2 (delta 1), reused 0 (delta 0)
    remote: Resolving deltas: 100% (1/1), completed with 1 local object.
    To https://github.com/WuPeiqi/pondo.git
       634aac4..274f1e4  master -> master
    MacBook-Pro-4:pondo wupeiqi$ git push origin dev              # 将本地dev分支内容以及版本信息推送到GitHub
    Counting objects: 3, done.
    Delta compression using up to 4 threads.
    Compressing objects: 100% (2/2), done.
    Writing objects: 100% (3/3), 261 bytes | 0 bytes/s, done.
    Total 3 (delta 1), reused 0 (delta 0)
    remote: Resolving deltas: 100% (1/1), completed with 1 local object.
    To https://github.com/WuPeiqi/pondo.git
       274f1e4..50e2169  dev -> dev
    View Code

      在公司,新电脑第一次使用,需要将代码从GitHub中获取并继续开发,开发完事下班就下班回家。

    MacBook-Pro-4:github wupeiqi$ git clone https://github.com/WuPeiqi/pondo.git    # 将项目从GitHub中获取
    Cloning into 'pondo'...
    remote: Counting objects: 31, done.
    remote: Compressing objects: 100% (26/26), done.
    remote: Total 31 (delta 2), reused 30 (delta 1), pack-reused 0
    Unpacking objects: 100% (31/31), done.
    MacBook-Pro-4:github wupeiqi$ cd pondo/
    MacBook-Pro-4:pondo wupeiqi$ git Branch                                          # 默认获取到得只有master分支
    * master
    MacBook-Pro-4:pondo wupeiqi$ git branch dev origin/dev                           # 创建dev分支且和远程dev分支同步
    Branch dev set up to track remote branch dev from origin.
    MacBook-Pro-4:pondo wupeiqi$ git checkout dev                                    # 切换到dev分支
    Switched to branch 'dev'
     
    MacBook-Pro-4:pondo wupeiqi$ vim app01/views.py                                  # 继续开发新功能
     
    MacBook-Pro-4:pondo wupeiqi$ git add .                                           # 添加文件到版本库的暂存状态
    MacBook-Pro-4:pondo wupeiqi$ git commit -m '公司开发功能1'                         # 提交新功能到版本库的分支
    [dev 9281447] 公司开发功能1
     1 file changed, 1 insertion(+), 1 deletion(-)
    MacBook-Pro-4:pondo wupeiqi$ git push origin dev                                 # 提交dev分支内容到远程GitHub托管仓库的dev分支
    Username for 'https://github.com': wupeiqi
    Password for 'https://wupeiqi@github.com':
    Counting objects: 4, done.
    Delta compression using up to 4 threads.
    Compressing objects: 100% (4/4), done.
    Writing objects: 100% (4/4), 427 bytes | 0 bytes/s, done.
    Total 4 (delta 2), reused 0 (delta 0)
    remote: Resolving deltas: 100% (2/2), completed with 2 local objects.
    To https://github.com/WuPeiqi/pondo.git
       50e2169..9281447  dev -> dev
    View Code

      在家里,由于白天在公司已经开发一部分功能并提交到GitHub,家里电脑的代码还是昨晚的版本,所以需要从GitHub拉去最新代码,然后继续开发。

    MacBook-Pro-4:pondo wupeiqi$ git checkout dev                                   # 切换到dev分支
    Already on 'dev'
    MacBook-Pro-4:pondo wupeiqi$ git pull origin dev                                # 从远程GitHub仓库获取dev分支最新内容,并合并到本地
    remote: Counting objects: 4, done.
    remote: Compressing objects: 100% (2/2), done.
    remote: Total 4 (delta 2), reused 4 (delta 2), pack-reused 0
    Unpacking objects: 100% (4/4), done.
    From https://github.com/WuPeiqi/pondo
     * branch            dev        -> FETCH_HEAD
       50e2169..9281447  dev        -> origin/dev
    Updating 50e2169..9281447
    Fast-forward
     app01/views.py | 2 +-
     1 file changed, 1 insertion(+), 1 deletion(-)
     MacBook-Pro-4:pondo wupeiqi$ vim app01/views.py                                # 继续开发新功能
     MacBook-Pro-4:pondo wupeiqi$ git add .                                         # 添加文件到版本库的暂存状态
    MacBook-Pro-4:pondo wupeiqi$ git commit -m '家里开发功能1'                        # 提交新功能到版本库的分支
    View Code

      在公司,由于昨天晚上在家已经开发了一部分功能,在公司需要先把昨晚开发的功能从GitHub中拉取,并继续开发。

    MacBook-Pro-4:pondo wupeiqi$ git checkout dev                                   # 切换到dev分支
    MacBook-Pro-4:pondo wupeiqi$ git fetch origin dev                               # 从GitHub仓库获取dev分支最新内容到版本库的分支
    remote: Counting objects: 3, done.
    remote: Compressing objects: 100% (1/1), done.
    remote: Total 3 (delta 1), reused 3 (delta 1), pack-reused 0
    Unpacking objects: 100% (3/3), done.
    From https://github.com/WuPeiqi/pondo
     * branch            dev        -> FETCH_HEAD
       150d891..65b6604  dev        -> origin/dev
    MacBook-Pro-4:pondo wupeiqi$ git merge origin/dev                               # 将版本库的分支内容合并到工作区
    Updating 150d891..65b6604
    Fast-forward
     readme | 5 ++++-
     1 file changed, 4 insertions(+), 1 deletion(-)
    MacBook-Pro-4:pondo wupeiqi$ vim app01/views.py                                 # 继续开发新功能
    MacBook-Pro-4:pondo wupeiqi$ git add .                                          # 添加文件到版本库的暂存状态
    MacBook-Pro-4:pondo wupeiqi$ git commit -m 'xxxxxxxxxxx'                        # 提交新功能到版本库的分支
    View Code

      长此以往,将Git和GitHub结合使用做到避免电脑损坏造成数据丢失以及多地开发的问题,上文执行过程中执行 【git pull origin 分支】命令等同于【git fetch origin 分支】+ 【git merge origin/分支】,并且在执行过程中可能会出现冲突,原因是由于本地代码和获取的最新代码有重合部分,那么就需要自己手动解决冲突然后再继续开发。

     面试:git rebase 作用?

                - git fetch origin dev 
                - git rebase origin/dev 
                - git rebase --skip
    View Code

     招人一起开发项目:

      把某个人设置成合作者:将其他用户添加到仓库合作者中之后,该用户就具有向当前仓库提交代码。

        

      组织:创建一个组织,然后再该组织下可以创建多个项目,组内成员可以向组内所有项目提交代码。PS:也可以对某个项目指定合作者

       

      协同开发命令和以上步骤类似,此处就不再重新写代码,而是使用文件描述三人协同开发整个过程。

        创建程序
            用户A创建程序,提交到GitHub
            用户B克隆项目
            用户C克隆项目
        开发功能
            用户A开发功能1
            用户B开发功能2
            用户C开发功能3
        提交
            用户A提交功能1,并push(A用户手速快,先提交。)
            用户B提交功能2,无法push,因为GitHub上已经有其他人提交的新代码。
            解决方法:从GitHub上获取最新代码并合并到本地,提交自己开发的功能2。
            用户C提交功能3,无法push,无法提交,因为GitHub上已经有其他人提交的新代码。
            解决方法:从GitHub上获取最新代码并合并到本地,提交自己开发的功能3。
        获取最新代码
            用户A获取最新代码
            用户B获取最新代码
            用户C获取最新代码
    View Code

      在上面红色标注的解决方法位置可以有三种方式操作,三者都可以完成合并并提交新功能,但是日志记录会有差异,如:前两者版本记录中会出现合并,而第三种可以保证版本记录干净整洁。

       先 git pull origin master   然后 git push origin master

       先 git fetch origin master 然后 git merge origin/master   再 git push origin master

    用户A:
        touch 4.py
        git add .
        git commit -m '功能4'
        git push origin master
     
    用户B:
        touch 5.py
        git add .
        git commit -m '功能5'
     
        git push origin master # 报错,因为GitHub中已经有人提交新代码
     
        git pull origin master
     
        git push origin master
    View Code

        

       先 git fetch origin master 然后 git rebase origin/master  再 git push origin master

    用户A:
        touch 4.py
        git add .
        git commit -m '功能4'
        git push origin master
     
    用户B:
        touch 5.py
        git add .
        git commit -m '功能5'
     
        git push origin master # 报错,因为GitHub中已经有人提交新代码
     
        git fetch origin master
     
        git rebase origin/master
     
        git push origin master   
    View Code

        

      其他:

                    协同开发
                master,用于线上发布
                dev,    开发测试 
                review  用于代码review
                fjh
                hg
                qb
                
                注意:推代码时,可能存在forword错误    
    View Code

      面试题:给github上的项目贡献代码

       fork:找到想搞的项目,fork一下,然后这个项目就在自己仓库出现了

        
       自己改 :从自己仓库获取代码并进行编辑提交
       创建pull request:创建并提交一个pull request,然后等待原作者是否同意这个pull request,如果同意那么在作者的源代码中就推出现小P提交的功能

        

    四 命令总结

                git init 
                git add . 
                git commit -m '好好写'
                git status 
                git config ....
                git log 
                git reflog
                git reset 
                
                git remove add origin 'xxxxxxx'
                git push origin dev 
                git pull origin dev 
                    git fetch origin dev 
                    git merge origin/dev 
                    git rebase origin/dev 
                git branch 
                git checkout 
                git merge 分支
                
                git stash 
                git stash list 
                git stash pop 
                
                git clone 'xxxx'
    View Code

    五 ssh的链接方式:

     https://blog.csdn.net/qq_33826977/article/details/78671443

      

     忽略自动检查: .gitignore
      .idea/

     基于tog常用命令实现版本:

                git tag
                git tag -a v.10 -m '描述信息'
                
                git push origin --tags
                git pull origin --tags
                
                git clone -b v1.0 https://gitee.com/wupeiqi/video.git
                
    View Code

    六  其他补充

     配置文件:

    Git的配置文件有三个:
    
        系统配置: /private/etc/gitconfig
        用户配置: ~/.gitconfig
        项目配置:.git/config
    View Code

     用户凭证:

    由于Git和Github交互操作可能会很频繁,那么一定少了用户授权的操作,为了防止每次操作重复输入用户名和密码,Git提供了两种解决方法:
    
        秘钥
        首先创建一对秘钥  ssh-keygen -t rsa,然后将 id_rsa.pub (公钥)内容拷贝到github中,日后操作无需再输入用户名和密码。
        注意:这种方式需要使用GIt中 git@github.com:WuPeiqi/xxxxx.git 格式地址。
        密码
        Https访问git时,避免每次操作需要输入用户名和密码,可以在配置文件中添加如下配置项:
            [credential]
            helper = store/cache/第三方
    
        store:
                表示将用户名和密码保存在硬盘上
                第一次输入过用户名和密码之后,用户名和密码就会保存在当前用户根目录的 .git-credentials 文件中,内容格式为:https://用户名:密码@github.com
    
                自动添加配置命令:git config credential.helper store
        cache:
                表示将用户名和密码保存在缓存中
                第一次输入过用户名和密码之后,用户名和密码就会保存在缓存中,默认超时时间是 900 秒,缓存相关文件保存在当前用户根目录的 git-credential-cache 中
                自动添加配置命令:
                       git config credential.helper cache
                       git config credential.helper 'cache --timeout=300'
    
        相关操作:
                清除缓存:git credential-cache exit
                指定超时:
                    [credential]
                    helper = cache --timeout=300
        注意:这种方式需要使用GIt中 https://github.com/WuPeiqi/xxxx.git 格式地址。
    View Code

     特殊符号:

      以斜杠“/”开头表示目录;
    
      以星号“*”通配多个字符;
    
      以问号“?”通配单个字符
    
      以方括号“[]”包含单个字符的匹配列表;
    
      以叹号“!”表示不忽略(跟踪)匹配到的文件或目录;
    View Code

      补充其他命令:

       ssh-keygen -t rsa -C '用户名'   :

      git remote rm origin    :删除链接的代码仓库  详细信息:https://www.cnblogs.com/lazb/articles/5597878.html

      git push -f origin 分支  :强制推送  详细信息:https://stackoverflow.com/questions/20939648/issue-pushing-new-code-in-github

      修改分支名称:

    给一个git分支改名的方法很简单
    
    如果对于分支不是当前分支,可以使用下面代码:
    
        git branch -m 原名 新
    
        1
    
    如果是当前,那么可以使用加上新名字
    
            git branch -m 原名 
    View Code

      什么是分支:https://www.zhihu.com/question/21995370

        https://www.cnblogs.com/lianghe01/p/5806518.html

      github的详细使用:https://blog.csdn.net/love_techlive/article/details/52300577

      其他:https://jingyan.baidu.com/album/f3e34a12a25bc8f5ea65354a.html?picindex=2

  • 相关阅读:
    我的博客开通啦
    (转载)IOS- Instruments使用之使用Leaks检测内存泄漏
    IOS Xcode -> instruments -> Leaks
    (转载) ios内存泄漏检查-leaks使用
    (转载)浅析MySQL中concat以及group_concat的使用
    内存分析工具 MAT 的使用 (转载)
    接口测试 -- 保存 requests 的 cookies
    接口测试 -- 关闭 requests 打开的 file
    SoapUI、Jmeter、Postman三种接口测试工具的比较分析
    postman使用技巧
  • 原文地址:https://www.cnblogs.com/fangjie0410/p/8696664.html
Copyright © 2011-2022 走看看