当我们本地项目的基本架构搭建完成后,一般需要将整个项目提交到 git 仓库进行管理,方便我们后续协同开发。如何才能完整的将全部代码提交到远程的git仓库呢?一般我们有两种解决方案:
- git clone 远程仓库到本地再进行操作
- 先在本地搭建好项目然后再 git init 并 push 到远程仓库
安装并设置 git,全局配置用户名邮箱配置,也可以在项目根目录对单个仓库进行设置,去除--global
参数即可
$ git config --global user.name "Yours Name"
$ git config --global user.email "xxx@xx.com"
$ git config --list
初始化git,生成.git
文件夹
注意:必须在项目根目录执行此操作
$ cd www/dsp
$ git init
Initialized empty Git repository in /root/www/dsp/.git/
将所有文件添加到 git 仓库
注意:此操作会根据项目的
.gitignore
文件自动排序不需要添加到 git 仓库的文件,如果出现warning: LF will be replaced by CRLF
,请执行以下操作:
$ rm -rf .git
$ git config --gobal core.autocrlf false
$ git add .
提交到暂存区,-m
是本次提交的注释信息(必填)
[root@iZbp17c1cena5ecdzj78eyZ dsp] git commit -m 'initialization'
查看远程仓库信息,如果存在远程仓库信息则删除git remote rm origin
$ git remote -v
添加新的远程仓库信息
$ 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)
先更新,再推送到远程仓库
注意:如果出现下图所示错误,需添加
--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
推送更新到远程仓库
注意:第一次推送master分支时,加上了
-u
参数,Git不但会把本地的master分支内容推送的远程新的master分支,还会把本地的master分支和远程的master分支关联起来,后续推送不需要再使用-u
参数
$ git push -u origin master
建立本地分支与远程仓库分支的关联关系,这样我们就可以直接使用git push
进行推送了,否则需要执行git push origin master
$ git branch --set-upstream-to=origin/master master
Branch master set up to track remote branch master from origin.