Git | 提交本地库到远程

当我们在 github 上新建了一个项目,然后希望将本地一个文件夹的内容提交到该项目中,通常有以下做法:

  • 直接 clone 远程库
git clone https://github.com/*/*.git
# 转移文件到该文件夹中
git add .
git commit -m "commit"
git push

克隆远程库到本地的另一个文件夹中,然后将需求文件夹中的内容全部复制到该文件夹中,最后提交即可。

  • 未建立本地仓库

如果需求文件夹未建立本地仓库,即不存在.git文件夹,且未使用过commit操作,可以直接在需求文件夹中执行 git 命令。

git init 
git remote add origin https://github.com/*/*.git
git pull origin master
git add .
git commit -m "commit"
git push --set-upstream origin master
  • 已建立本地仓库

如果需求文件夹已建立本地仓库,当执行 pull 时会因为两个仓库提交历史不一致,无法拉取远程信息。报错信息如:refusing to merge unrelated histories。此时可以在后面添加--allow-unrelated-histories命令,允许不相关历史进行提交。

git remote add origin https://github.com/*/*.git
git pull origin master --allow-unrelated-histories
git push --set-upstream origin master

你可能感兴趣的:(Git | 提交本地库到远程)