git将本地项目添加远程地址
2020-03-27 本文已影响0人
RadishHuang
如果是新的项目大致步骤如下
- 创建初始化git。
git init
- 添加远程地址。
git remote add origin git@xxxx.git(远程仓库地址)
$ git init
Initialized empty Git repository in /Users/n-321/Desktop/code/lrl-book/.git/
$ git remote add origin git@gitee.com:radishhuang/test-git.git
$ git add .
$ git commit -m "初始化"
- 这时候尝试的去
git add
以及git commit
,git push
提交的时候,不出意外,会有一行这样的提示
git push
fatal: The current branch master has no upstream branch.
To push the current branch and set the remote as upstream, use
git push --set-upstream origin master
N-321deMacBook-Pro:test-git n-321$
- 按照提醒,我们尝试
git push --set-upstream origin master
,不出意外的,很不给面子的报错了。大概意思是我们需要先把代码拉到本地,才能提交
N-321deMacBook-Pro:test-git n-321$ git push --set-upstream origin master
To gitee.com:radishhuang/test-git.git
! [rejected] master -> master (fetch first)
error: failed to push some refs to 'git@gitee.com:radishhuang/test-git.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.
N-321deMacBook-Pro:test-git n-321$
- 于是我们按照提醒,就pull下,果然还是会不给面子的报错
N-321deMacBook-Pro:test-git n-321$ git pull
warning: no common commits
remote: Counting objects: 100% (4/4), done.
remote: Compressing objects: 100% (4/4), done.
remote: Total 4 (delta 0), reused 0 (delta 0), pack-reused 0
Unpacking objects: 100% (4/4), done.
From gitee.com:radishhuang/test-git
* [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
N-321deMacBook-Pro:test-git n-321$
- 这个问题是因为 两个 根本不相干的 git 库, 一个是本地库, 一个是远端库, 然后本地要去推送到远端, 远端觉得这个本地库跟自己不相干, 所以告知无法合并。重点来了,我们强制把远程的仓库和本地的仓库合并。
git pull git@gitee.com:radishhuang/test-git.git master --allow-unrelated-histories
N-321deMacBook-Pro:test-git n-321$ git pull git@gitee.com:radishhuang/test-git.git master --allow-unrelated-histories
From gitee.com:radishhuang/test-git
* branch master -> FETCH_HEAD
Merge made by the 'recursive' strategy.
README.en.md | 36 ++++++++++++++++++++++++++++++++++++
README.md | 37 +++++++++++++++++++++++++++++++++++++
2 files changed, 73 insertions(+)
create mode 100644 README.en.md
create mode 100644 README.md
- 这时候尝试提交,当然还是会失败,但是有提醒第一次提交要多加一些参数。用这个命令提交
git push --set-upstream origin master
N-321deMacBook-Pro:test-git n-321$ git push
fatal: The current branch master has no upstream branch.
To push the current branch and set the remote as upstream, use
git push --set-upstream origin master
N-321deMacBook-Pro:test-git n-321$ git push --set-upstream origin master
Enumerating objects: 6, done.
Counting objects: 100% (6/6), done.
Delta compression using up to 12 threads
Compressing objects: 100% (5/5), done.
Writing objects: 100% (5/5), 10.50 MiB | 2.05 MiB/s, done.
Total 5 (delta 0), reused 0 (delta 0)
remote: Powered by GITEE.COM [GNK-3.8]
To gitee.com:radishhuang/test-git.git
630bef5..6fc3e69 master -> master
总结:
- 创建初始化git。
git init
- 添加远程地址。
git remote add origin git@xxxx.git(远程仓库地址)
- 强制将远程仓库和本地仓库合并。
git pull git@xxxx.git(远程仓库地址) master --allow-unrelated-histories
- 如果项目有冲突先解决冲突,并且
git add
以及git commit
代码到本地。 - 第一次推到远程仓库
git push --set-upstream origin master
- 接下来就跟正常使用git一样。成功将本地仓库和远程仓库关联合并到一起。