git同步两个(多个)仓库本地仓库网络仓库

环境:

远程仓库是gitee的,地址:git@gitee.com:你的用户名/hello.git

本地仓库,地址:git@192.168.200.132:/home/gitrepo/hello.git

没有仓库:

先创建仓库:新建一个叫 hello 的项目仓库

mkdir hello
cd hello
git init
touch README.md
git add README.md
git commit -m "first commit"
git remote add origin git@gitee.com:你的用户名/hello.git
git push -u origin master

  -u是提示要输入用户名、密码,可以将自己的公钥Pub放到gitee后台,提交时候就不用输入密码了。在Linux下可能输入密码也提交不成功,所以最好就是使用公钥。

windows下如何生成公钥和私钥:

首先Windows操作系统需要安装git.
安装完成后,再到任意的文件夹内,点击右键.选择git bash here
打开之后,输入ssh-keygen,一路按enter键.
全部结束后,再到C:UsersAdministrator.ssh 文件夹下,打开id_rsa.pub文件,复制文件内的公钥.
注意:.ssh是隐藏文件,需开启文件显示

Linux Centos7生成公钥和密钥:

yum -y  install curl-devel expat-devel gettext-devel openssl-devel zlib-devel perl-devel git

输入命令一路回车即可。

[root@CentOs7-2 ~]# ssh-keygen -t rsa
Generating public/private rsa key pair.
Enter file in which to save the key (/root/.ssh/id_rsa):
Enter passphrase (empty for no passphrase):
Enter same passphrase again:
Your identification has been saved in /root/.ssh/id_rsa.
Your public key has been saved in /root/.ssh/id_rsa.pub.
The key fingerprint is:
SHA256:6YANhNQ13h11PNUu******nkJtykfiN1aUZJt0 root@CentOs7-2
The key's randomart image is:
+---[RSA 2048]----+
| oo**+*+ooo.....o|
|  +o**o=o=E...= .|
| . o.Bo.+ .. . = |

|       o.        |
|        .        |
|                 |
|                 |
+----[SHA256]-----+

  证书创建完成。保存在当前用户主目录下.ssh文件下,"点" 开头的文件夹是隐藏文件夹,可以用 ls -a 可以看到。 

已有仓库:

先给仓库设置用户: 自己使用或测试随便填,实际生产要填真的,方便管理和溯源

#Git 全局设置:
git config --global user.name "你的名字"
git config --global user.email "你的邮箱"

删除之前的关联:

cd hello
git remote rm origin

   

将目录和远程版本库gitee关联,注意add后的local、gitee是用来区分俩个版本库的,随便写,不重复就行、

cd hello
git remote add local git@localhost:/home/gitrepo/hello.git
git remote add gitee git@gitee.com:你的用户名/hello.git

 

向远程库推送代码

git push  local master
git push  gitee master

  

 ===========     错误 收集    ============

错误:fatal: Not a git repository (or any of the parent directories): .git

翻译:提示说没有.git这样一个目录

解决:

#命令行 输入 
$ git init  

  

 错误:

error: src refspec master does not match any.
error: failed to push some refs to 'origin'

翻译:master是一个空目录。

解决:

1.首次创建,目录里什么也没有,可以新建个文件,内容随意,就是先随便写放点东西。

$ touch READMD

2.提交了一个空版本库,虽然你在这个文件夹放了文件,但是你没有执行$git add . 和git commit -m "备注" 这命令,所以git不知道你要提交什么。

$ git add . 
$ git commit -m "

3.新建装git软件,没有设置name 和 mail

#Git 全局设置:
git config --global user.name "你的名字"
git config --global user.email "你的邮箱"

4.没有初始化仓库 。如有个项目hello 要放本地D:git-repo下

$ cd d:git-repo
$ git init
$ git remote add git@gitee.com:你的用户名hello.git
$ cd hello
$ git add .
  # 查看暂存区状态
$ git status 
  # 提交到本地版本库里
$ git commit -m "备注"
  # 提交远程gitee仓库里
$ git push origin master

  

 如果是初学者操作过程中出现很问题,那直接把目录下隐藏的“.git” (注意git前有个点) 文件夹删除,重新克隆一份git clone。在开始。

  

  

原文地址:https://www.cnblogs.com/wutou/p/14078846.html