美文网首页
git将本地项目添加远程地址

git将本地项目添加远程地址

作者: RadishHuang | 来源:发表于2020-03-27 14:29 被阅读0次

如果是新的项目大致步骤如下

  • 创建初始化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 commitgit 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一样。成功将本地仓库和远程仓库关联合并到一起。

相关文章

  • git本地master与远程master冲突解决

    将本地已有项目上传至新建远程git仓库,首先在本地项目文件中添加远程仓库地址 运行时报错,出错提示 git fai...

  • git将本地项目添加远程地址

    如果是新的项目大致步骤如下 创建初始化git。git init 添加远程地址。git remote add ori...

  • 本地git项目,添加github远程地址

    进入本地仓库 添加远程地址git remote add origin [远程地址] 推送本地分支到远程分支git ...

  • git命令

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

  • git更换远程仓库

    一、替换远程仓库 删除本地仓库当前关联的无效远程地址,再为本地仓库添加新的远程仓库地址 Git修改远程仓库地址后推...

  • 本地git地址替换

    本地git地址替换gitlab的远程地址。查看当前项目origin路径 查看当前项目的远程路径 $ git rem...

  • 【Git】更换Git远程仓库地址

    项目组更换Git远程仓库地址,本地开发环境需要更换Git远程仓库地址,方法如下:git remote set-ur...

  • 2018-09-26把本地的项目上传到git仓库

    参考的文章:# 如何用命令将本地项目上传到git 操作的步骤: git查看远程仓库地址命令在项目地址下面输入:gi...

  • Git 复杂使用

    查看本地库里记录的远程库地址 git remote -v 这里把远程库的地址添加个标签叫origin git re...

  • git的基本使用

    标签(空格分隔): git 获取远程仓库的项目到本地 使用 git clone 远程仓库地址 (如果使用 git ...

网友评论

      本文标题:git将本地项目添加远程地址

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