通过重新尝试,我发现了能够直接成功的方法:
(1)打开终端,输入cd front_projects所在的目录地址,并创建all-proj,再把多个项目文件夹丢进去all-proj里
(2)在front_projects文件夹下,执行终端命令git init初始化本地git仓库
(3)在github上新建一个没有README文件的仓库,使得仓库里没有东西(如果想创建这个文件则要记得添加远程仓库地址后,先拉取,本地先不做任何操作)
(4)添加远程仓库地址git remote add origin htts://github.com/xxxxxx/front_proj.git(这里为了避免密钥的麻烦,我选择了https,不过这个办法每次上传代码到远端github时,都需要输入用户名密码确认,请务必注意,这里的用户名不是指github个人设置里的Name,而是邮箱)
(5)执行git add .添加本地修改
(6)执行git commit -m 'first push content...'
(7)执行git push -u origin master(或者git push --set upstream origin master,-u是--set upstream的缩写,这里master分支可以自己修改为其他指定分支),因为远端github仓库还没有创建分支,因此直接git push是会报错的,这句话的目的就在于在推送至远端仓库时,先创建同名远端分支,再进行代码推送
结果图:
-----------------------------
操作背景:在本地有10多个已存在项目,我打算在github上新建一个仓库cczq_project存放这些项目
途中会碰到3个问题:
一、上传的文件夹是空的,文件夹没有内容 // 因为git设定如果项目文件夹下包含其他git项目,他们的关系无法确认,因此上传的文件夹默认为空。
解决办法:
wlk-yodinfodeiMac:cczq_project wlk_yodinfo$ sudo rm -rf .git
Password:
wlk-yodinfodeiMac:cczq_project wlk_yodinfo$ ls -a
. .. .DS_Store README.md
wlk-yodinfodeiMac:cczq_project wlk_yodinfo$ git remote add origin https://github.com/xyq86565379/cczq_project.git
fatal: not a git repository (or any of the parent directories): .git
wlk-yodinfodeiMac:cczq_project wlk_yodinfo$ git init
Initialized empty Git repository in /Users/wlk_yodinfo/Documents/front_projects/react_native_project/cczq_project/.git/
wlk-yodinfodeiMac:cczq_project wlk_yodinfo$ git remote add origin https://github.com/xyq86565379/cczq_project.git
wlk-yodinfodeiMac:cczq_project wlk_yodinfo$ git pull --rebase origin master
remote: Enumerating objects: 3, done.
remote: Counting objects: 100% (3/3), done.
remote: Total 3 (delta 0), reused 0 (delta 0), pack-reused 0
Unpacking objects: 100% (3/3), done.
From https://github.com/xyq86565379/cczq_project
* branch master -> FETCH_HEAD
* [new branch] master -> origin/master
error: The following untracked working tree files would be overwritten by merge:
README.md
Please move or remove them before you merge.
Aborting
wlk-yodinfodeiMac:cczq_project wlk_yodinfo$ git add .
二、hint: You've added another git repository inside your current repository...
解决办法:删除已存在项目的.git,.gitmodules,.gitattributes文件,有就删掉,没有就不用,不过这三个文件都是默认隐藏的
hint: You've added another git repository inside your current repository.
hint: Clones of the outer repository will not contain the contents of
hint: the embedded repository and will not know how to obtain it.
hint: If you meant to add a submodule, use:
hint:
hint: git submodule add <url> Android-2C
hint:
hint: If you added this path by mistake, you can remove it from the
hint: index with:
hint:
hint: git rm --cached Android-2C
hint:
hint: See "git help submodule" for more information.
三、Updates were rejected because the tip of your current branch is behind...
出现这个问题是因为,我在github上创建仓库时默认选择了创建README文件,导致与本地版本不同出现冲突
解决办法1: 解决冲突
解决办法2: 新建一个分支,避开冲突,比如git checkout -b develop
$git push --set-upstream origin master
To https://github.com/xyq86565379/cczq_project.git
! [rejected] master -> master (non-fast-forward)
error: failed to push some refs to 'https://github.com/xyq86565379/cczq_project.git'
hint: Updates were rejected because the tip of your current branch is behind
hint: its remote counterpart. Integrate the remote changes (e.g.
hint: 'git pull ...') before pushing again.
hint: See the 'Note about fast-forwards' in 'git push --help' for details.
补充项目图: