下面我把完整的提交過程和遇到的問題描述一下,并附上相關的解決方案
場景
給一個正在開發的項目添加新功能,該項目代碼放在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切換分支查看提交就可以看到記錄了