美文网首页
git提交到github远程分支

git提交到github远程分支

作者: 牧码人小鹏 | 来源:发表于2017-09-15 15:37 被阅读412次

    下面我把完整的提交过程和遇到的问题描述一下,并附上相关的解决方案

    场景

    给一个正在开发的项目添加新功能,该项目代码放在github上面,我被添加为了该项目名为dev分支的合作者,可以向上面push代码,现在我已经开发完了该功能,需要提交到远程分支了,整个过程使用git做了如下操作

    安装和clone

    首先安装好git然后到目录下面,以windows系统的D:\为例,从代码库clone下来,打开git bash

    cd d:
    git clone https://github.com/guanpengchn/demo.git
    

    然后将demo.zip压缩包解压出来,这个时候工程就在D:\demo下面

    进行开发

    然后对下载下来的代码进行开发和更改,修改完成,就到了下面比较关键的步骤了

    提交本地仓库

    在提交远程仓库之前,要先在本地仓库提交,首先要确定clone下来的工程里有没有.git文件,如果没有的话那么需要自己初始化(理论上应该没有),使用如下代码:

    $ git init
    

    如果有的话则可以跳过,然后把修改提交至本地仓库

    $ git add .
    $ git commit -m '修改主页'
    

    提交远程仓库

    提交远程仓库之前需要关联远程仓库

    $ git remote add origin https://github.com/guanpengchn/demo.git
    

    其实这句代码的含义就是相当于给origin键加了一个https://github.com/guanpengchn/demo.git,其实就是一个键值对,可以通过如下指令查看

    $ git remote -v
    origin  https://github.com/guanpengchn/demo.git (fetch)
    origin  https://github.com/guanpengchn/demo.git (push)
    

    关联之后就可以提交了,由于我在本地的是主分支,所以也就是master分支,而远程我关联的是dev分支,所以提交的时候要写成如下格式

    $ git push origin master:dev
    

    如果仓库里的代码没有被修改过,那么这个时候就成功了

    同步仓库再提交

    但是如果远程分支被其他人提交修改了,我这里的代码和他人不同步,就会报错,所以这个时候要做同步工作,使用如下指令

    $ git pull --rebase origin dev
    warning: no common commits
    remote: Counting objects: 4365, done.
    remote: Compressing objects: 100% (242/242), done.
    remote: Total 4365 (delta 221), reused 299 (delta 143), pack-reused 3967
    Receiving objects: 100% (4365/4365), 22.13 MiB | 584.00 KiB/s, done.
    Resolving deltas: 100% (2571/2571), done.
    From https://github.com/guanpengchn/demo
     * branch            dev        -> FETCH_HEAD
     * [new branch]      dev        -> origin/dev
    First, rewinding head to replay your work on top of it...
    Applying: 修改主页
    .git/rebase-apply/patch:2753: trailing whitespace.
                    position:relative;
    .git/rebase-apply/patch:5715: trailing whitespace.
    
    .git/rebase-apply/patch:5740: trailing whitespace.
    
    .git/rebase-apply/patch:5770: trailing whitespace.
                }
    .git/rebase-apply/patch:5772: trailing whitespace.
    
    warning: squelched 79 whitespace errors
    warning: 84 lines add whitespace errors.
    error: Failed to merge in the changes.
    Using index info to reconstruct a base tree...
    Falling back to patching base and 3-way merge...
    Auto-merging server/config.js
    CONFLICT (add/add): Merge conflict in server/config.js
    Auto-merging client/home/assets/teaching/bg4.jpg
    CONFLICT (add/add): Merge conflict in client/common/components/Navbar.js
    Patch failed at 0001 修改主页
    The copy of the patch that failed is found in: .git/rebase-apply/patch
    
    When you have resolved this problem, run "git rebase --continue".
    If you prefer to skip this patch, run "git rebase --skip" instead.
    To check out the original branch and stop rebasing, run "git rebase --abort".
    

    相当于把远程github上的项目的dev分支的代码拉到了本地和我改好的代码合并,会出现上面的信息,其中有一些信息是我们需要关注的,也就是

    Auto-merging server/config.js
    CONFLICT (add/add): Merge conflict in server/config.js
    Auto-merging client/home/assets/teaching/bg4.jpg
    CONFLICT (add/add): Merge conflict in client/common/components/Navbar.js
    

    其中
    server/config.js
    client/home/assets/teaching/bg4.jpg
    client/common/components/Navbar.js
    是工程中的文件,上面的翻译过来也就是说,在pull过程中做了自动合并,但是发生了冲突,需要我们手动去解决,然后就把这些文件打开去查看冲突的位置,都已经被git标好了,留下需要的代码即可

    解决之后执行

    $ git rebase --continue
    client/common/components/Navbar.js: needs merge
    client/home/assets/teaching/bg4.jpg: needs merge
    server/config.js: needs merge
    You must edit all merge conflicts and then
    mark them as resolved using git add
    
    

    根据提示,之后执行

    git add .
    

    然后在执行

    $ git rebase --continue
    Applying: 修改主页
    

    这个时候发现pull操作之后的冲突已经都解决完了

    然后再执行

    $ git push origin master:dev
    Counting objects: 42, done.
    Delta compression using up to 4 threads.
    Compressing objects: 100% (41/41), done.
    Writing objects: 100% (42/42), 1.27 MiB | 282.00 KiB/s, done.
    Total 42 (delta 13), reused 0 (delta 0)
    remote: Resolving deltas: 100% (13/13), completed with 6 local objects.
    To https://github.com/guanpengchn/demo.git
       171b4ce..cec6ccf  master -> dev
    
    

    这个时候就真正的将代码提交到了github的远程分支dev上,打开github切换分支查看提交就可以看到记录了

    这里写图片描述这里写图片描述

    相关文章

      网友评论

          本文标题:git提交到github远程分支

          本文链接:https://www.haomeiwen.com/subject/smossxtx.html