當(dāng)我們本地項(xiàng)目的基本架構(gòu)搭建完成后,一般需要將整個(gè)項(xiàng)目提交到 git 倉(cāng)庫(kù)進(jìn)行管理,方便我們后續(xù)協(xié)同開(kāi)發(fā)。如何才能完整的將全部代碼提交到遠(yuǎn)程的git倉(cāng)庫(kù)呢?一般我們有兩種解決方案:
- git clone 遠(yuǎn)程倉(cāng)庫(kù)到本地再進(jìn)行操作
- 先在本地搭建好項(xiàng)目然后再 git init 并 push 到遠(yuǎn)程倉(cāng)庫(kù)
安裝并設(shè)置 git,全局配置用戶名郵箱配置,也可以在項(xiàng)目根目錄對(duì)單個(gè)倉(cāng)庫(kù)進(jìn)行設(shè)置,去除--global
參數(shù)即可
$ git config --global user.name "Yours Name"
$ git config --global user.email "xxx@xx.com"
$ git config --list
初始化git,生成.git
文件夾
注意:必須在項(xiàng)目根目錄執(zhí)行此操作
$ cd www/dsp
$ git init
Initialized empty Git repository in /root/www/dsp/.git/
將所有文件添加到 git 倉(cāng)庫(kù)
注意:此操作會(huì)根據(jù)項(xiàng)目的
.gitignore
文件自動(dòng)排序不需要添加到 git 倉(cāng)庫(kù)的文件,如果出現(xiàn)warning: LF will be replaced by CRLF
,請(qǐng)執(zhí)行以下操作:
$ rm -rf .git
$ git config --gobal core.autocrlf false
$ git add .
提交到暫存區(qū),-m
是本次提交的注釋信息(必填)
[root@iZbp17c1cena5ecdzj78eyZ dsp] git commit -m 'initialization'
查看遠(yuǎn)程倉(cāng)庫(kù)信息,如果存在遠(yuǎn)程倉(cāng)庫(kù)信息則刪除git remote rm origin
$ git remote -v
添加新的遠(yuǎn)程倉(cāng)庫(kù)信息
$ git remote add origin https://gitee.com/xxx/xxx.git && git remote -v
origin https://gitee.com/meoin/dsp.git (fetch)
origin https://gitee.com/meoin/dsp.git (push)
先更新,再推送到遠(yuǎn)程倉(cāng)庫(kù)
注意:如果出現(xiàn)下圖所示錯(cuò)誤,需添加
--allow-unrelated-histories
即可
[root@iZbp17c1cena5ecdzj78eyZ dsp] git pull origin master
From https://gitee.com/meoin/dsp
* branch master -> FETCH_HEAD
fatal: refusing to merge unrelated histories
推送更新到遠(yuǎn)程倉(cāng)庫(kù)
注意:第一次推送master分支時(shí),加上了
-u
參數(shù),Git不但會(huì)把本地的master分支內(nèi)容推送的遠(yuǎn)程新的master分支,還會(huì)把本地的master分支和遠(yuǎn)程的master分支關(guān)聯(lián)起來(lái),后續(xù)推送不需要再使用-u
參數(shù)
$ git push -u origin master
建立本地分支與遠(yuǎn)程倉(cāng)庫(kù)分支的關(guān)聯(lián)關(guān)系,這樣我們就可以直接使用git push
進(jìn)行推送了,否則需要執(zhí)行git push origin master
$ git branch --set-upstream-to=origin/master master
Branch master set up to track remote branch master from origin.