将本地项目推送到远程git新仓库

使用命令行的方式,首先在本地新创建一个git仓库(如果当前项目还没有仓库的话):

在git bash下

1. mkdir project
2. cd project
3. git init (初始化仓库)
4.复制项目文件到刚创建的新仓库目录

准备工作做好后,接着就是把本地仓库项目推送到远程仓库:

1.git add . (将当前目录下的文件加入到仓库)
2.git commit -m "the first commit project" (第一次提交项目文件)
3.git remote add origin https://git.coding.net/solent/xingzuo.git
4.git push -u origin master (推送远程仓库)

执行第四步的时候,报错如下:

To https://git.coding.net/solent/xingzuo.git
 ! [rejected] master -> master (fetch first)
error: failed to push some refs to 'https://git.coding.net/solent/xingzuo.git'
hint: Updates were rejected because the remote contains work that you do
hint: not have locally. This is usually caused by another repository pushing
hint: to the same ref. You may want to first integrate the remote changes
hint: (e.g., 'git pull ...') before pushing again.
hint: See the 'Note about fast-forwards' in 'git push --help' for details.

报错因为远程仓库与本地仓库文件不一致,使用git pull拉下远程代码到本地

5.git pull

接着又报如下错误:

warning: no common commits
remote: Counting objects: 5, done.
remote: Compressing objects: 100% (4/4), done.
remote: Total 5 (delta 0), reused 0 (delta 0)
Unpacking objects: 100% (5/5), done.
From https://git.coding.net/solent/xingzuo
 * [new branch] master -> origin/master
There is no tracking information for the current branch.
Please specify which branch you want to merge with.
See git-pull(1) for details.

  git pull <remote> <branch>

If you wish to set tracking information for this branch you can do so with:

  git branch --set-upstream-to=origin/<branch> master

报错原因是没有指定本地master和远程origin/master的连接,执行git branch –set-upstream master origin/master,设置链接

6.git branch --set-upstream master origin/master

The --set-upstream flag is deprecated and will be removed. Consider using --trac k or --set-upstream-to

Branch master set up to track remote branch master from origin.

然后再执行下git pull

7.git pull
fatal: refusing to merge unrelated histories

这回报更严重的致命错误,根据提示然后如下执行:

8.git pull origin master --allow-unrelated-histories 

From https://git.coding.net/solent/xingzuo
* branch master -> FETCH_HEAD
Merge made by the 'recursive' strategy.
.gitignore | 46 +++++++++++++++
LICENSE | 191     
+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
README.md | 1 +
3 files changed, 238 insertions(+)
create mode 100644 .gitignore
create mode 100644 LICENSE
create mode 100644 README.md

最后再执行推送到远程仓库的命令,就不会再报错了

9.git push -u origin master 

Counting objects: 2279, done.
Delta compression using up to 4 threads.
Compressing objects: 100% (2227/2227), done.
Writing objects: 100% (2279/2279), 46.29 MiB | 543.00 KiB/s, done.
Total 2279 (delta 349), reused 0 (delta 0)
To https://git.coding.net/solent/xingzuo.git
29b55c0..7c92f17 master -> master
Branch master set up to track remote branch master from origin.
    原文作者:parvin
    原文地址: https://segmentfault.com/a/1190000009491296
    本文转自网络文章,转载此文章仅为分享知识,如有侵权,请联系博主进行删除。
点赞