下面我把完整的提交過程和遇到的問題描述一下,并附上相關(guān)的解決方案
場景
給一個正在開發(fā)的項(xiàng)目添加新功能,該項(xiàng)目代碼放在github上面,我被添加為了該項(xiàng)目名為dev分支的合作者和敬,可以向上面push代碼,現(xiàn)在我已經(jīng)開發(fā)完了該功能细办,需要提交到遠(yuǎn)程分支了,整個過程使用git做了如下操作
安裝和clone
首先安裝好git然后到目錄下面蕾殴,以windows系統(tǒng)的D:\為例笑撞,從代碼庫clone下來,打開git bash
cd d:
git clone https://github.com/guanpengchn/demo.git
然后將demo.zip壓縮包解壓出來区宇,這個時候工程就在D:\demo下面
進(jìn)行開發(fā)
然后對下載下來的代碼進(jìn)行開發(fā)和更改娃殖,修改完成值戳,就到了下面比較關(guān)鍵的步驟了
提交本地倉庫
在提交遠(yuǎn)程倉庫之前议谷,要先在本地倉庫提交,首先要確定clone下來的工程里有沒有.git文件堕虹,如果沒有的話那么需要自己初始化(理論上應(yīng)該沒有)卧晓,使用如下代碼:
$ git init
如果有的話則可以跳過芬首,然后把修改提交至本地倉庫
$ git add .
$ git commit -m '修改主頁'
提交遠(yuǎn)程倉庫
提交遠(yuǎn)程倉庫之前需要關(guān)聯(lián)遠(yuǎn)程倉庫
$ git remote add origin https://github.com/guanpengchn/demo.git
其實(shí)這句代碼的含義就是相當(dāng)于給origin鍵加了一個https://github.com/guanpengchn/demo.git值,其實(shí)就是一個鍵值對逼裆,可以通過如下指令查看
$ git remote -v
origin https://github.com/guanpengchn/demo.git (fetch)
origin https://github.com/guanpengchn/demo.git (push)
關(guān)聯(lián)之后就可以提交了郁稍,由于我在本地的是主分支,所以也就是master分支胜宇,而遠(yuǎn)程我關(guān)聯(lián)的是dev分支耀怜,所以提交的時候要寫成如下格式
$ git push origin master:dev
如果倉庫里的代碼沒有被修改過,那么這個時候就成功了
同步倉庫再提交
但是如果遠(yuǎn)程分支被其他人提交修改了桐愉,我這里的代碼和他人不同步财破,就會報(bào)錯,所以這個時候要做同步工作从诲,使用如下指令
$ 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".
相當(dāng)于把遠(yuǎn)程github上的項(xiàng)目的dev分支的代碼拉到了本地和我改好的代碼合并左痢,會出現(xiàn)上面的信息,其中有一些信息是我們需要關(guān)注的系洛,也就是
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過程中做了自動合并描扯,但是發(fā)生了沖突定页,需要我們手動去解決,然后就把這些文件打開去查看沖突的位置绽诚,都已經(jīng)被git標(biāo)好了拯勉,留下需要的代碼即可
解決之后執(zhí)行
$ 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
根據(jù)提示,之后執(zhí)行
git add .
然后在執(zhí)行
$ git rebase --continue
Applying: 修改主頁
這個時候發(fā)現(xiàn)pull操作之后的沖突已經(jīng)都解決完了
然后再執(zhí)行
$ 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的遠(yuǎn)程分支dev上憔购,打開github切換分支查看提交就可以看到記錄了