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:\Users\Administrator\.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。在開始。

  

  


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM