美文网首页LNMPiOS项目前端之他山之石
将本地已存在的项目提交到远程git仓库

将本地已存在的项目提交到远程git仓库

作者: pijh | 来源:发表于2017-11-01 17:45 被阅读17次

当我们本地项目的基本架构搭建完成后,一般需要将整个项目提交到 git 仓库进行管理,方便我们后续协同开发。如何才能完整的将全部代码提交到远程的git仓库呢?一般我们有两种解决方案:

  • git clone 远程仓库到本地再进行操作
  • 先在本地搭建好项目然后再 git init 并 push 到远程仓库
安装并设置 git,全局配置用户名邮箱配置,也可以在项目根目录对单个仓库进行设置,去除--global参数即可
[root@iZbp17c1cena5ecdzj78eyZ ~]# git config --global user.name "Yours Name"
[root@iZbp17c1cena5ecdzj78eyZ ~]# git config --global user.email "xxx@xx.com"
[root@iZbp17c1cena5ecdzj78eyZ ~]# git config --list
初始化git,生成.git文件夹

注意:必须在项目根目录执行此操作

[root@iZbp17c1cena5ecdzj78eyZ ~]# cd www/dsp
[root@iZbp17c1cena5ecdzj78eyZ 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
[root@iZbp17c1cena5ecdzj78eyZ dsp]# git add .
提交到暂存区,-m是本次提交的注释信息(必填)
[root@iZbp17c1cena5ecdzj78eyZ dsp] git commit -m 'initialization'
查看远程仓库信息,如果存在远程仓库信息则删除git remote rm origin
[root@iZbp17c1cena5ecdzj78eyZ dsp] git remote -v
添加新的远程仓库信息
[root@iZbp17c1cena5ecdzj78eyZ dsp] 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参数

[root@iZbp17c1cena5ecdzj78eyZ dsp] git push -u origin master
建立本地分支与远程仓库分支的关联关系,这样我们就可以直接使用git push进行推送了,否则需要执行git push origin master
[root@iZbp17c1cena5ecdzj78eyZ dsp] git branch --set-upstream-to=origin/master master
Branch master set up to track remote branch master from origin.

相关文章

  • git命令

    将本地新建的项目提交到远程仓库的步骤 初始化本地仓库git init 将本地内容添加至git本地暂存区中git a...

  • GIT简单使用

    本地配置全局git 创建本地仓库提交到git上 该针对本地没有存在的项目。 提交本地已存在的项目到git 针对本地...

  • Git命令

    git push 作用:将本地仓库中代码提交到远程仓库 语法 :git push 仓库地址 master git ...

  • 将本地已存在的项目提交到远程git仓库

    当我们本地项目的基本架构搭建完成后,一般需要将整个项目提交到 git 仓库进行管理,方便我们后续协同开发。如何才能...

  • 码云和git的使用

    一、本地项目上传到远程仓库 建立本地git仓库 将本地项目工作区的所有文件添加到暂存区 将暂存区的文件提交到本地仓...

  • 4、git

    一、为本地仓库设置远程仓库 1、建立好本地仓库,git init,git add .等操作,将代码提交到本地仓库 ...

  • 关于将GIT本地代码提交到远程仓库

    将GIT本地代码提交到远程仓库,有以下几种情况 本地还没有任何代码 1.直接用git clone 将远程仓库clo...

  • Git常用命令笔记

    git命令使用 1 创建远程仓库(初始化--提交到本地仓库--提交到远程仓库) $ git init ...

  • git常用命令

    从远程仓库克隆,并在本地修改后,提交到远程仓库 git clone 远程仓库地址 将远程仓库克隆到本...

  • 初始化项目

    从初始化到提交到远程仓库 #初始化本地仓库git init #将本地内容添加至git索引中git add . #将...

网友评论

    本文标题:将本地已存在的项目提交到远程git仓库

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