将本地代码同步到gitee和github中去

今天给大家介绍一个怎么使用,把我们本地的仓库推到gitee和github上面去。现在远程仓库有两个,一个是gitee和github远程仓库,地址对应的我本地也就是 work space。

第一步,在gitee和github中建立仓库地址

注册gitee和注册github,直接通过官网注册

gitee官网https://gitee.com/

github官网https://github.com/

本地也需要安装git

第二步,在本地的某个文件夹新家git仓库

在本地D:\work\workspace中初始化git

git init

然后在workspace中会产生一个.git的文件夹

第三步, 关联远程仓库地址

1.添加github

本地仓库workspace,右键打开gitbsah,输入下面命令

git remote add github [email protected]:xxxx/javaCore.git

2.添加gitee

本地仓库workspace,右键打开gitbsah,输入下面命令

 git remote add gitee [email protected]:xxx/java-Core.git

以上仓库地址,更具自己注册实际来填写

第四步,修改当前仓库地址中的 .git的config配置

在执行git init的文件夹下,打开.git,有一个config文件,修改配置

[core]
    repositoryformatversion = 0
    filemode = true
    bare = false
    logallrefupdates = true
    symlinks = false
    ignorecase = true
[remote "github"]
    url = [email protected]:xxxx/javaCore.git
    fetch = +refs/heads/*:refs/remotes/github/*
[remote "gitee"]
    url = [email protected]:xxx/java-Core.git
    fetch = +refs/heads/*:refs/remotes/gitee/*
[branch "master"]
    remote = origin
    merge = refs/heads/master

将filemode = false改为true

第五步,查看远程仓库地址

执行下面指令

git remote -v

可以查看到有远程仓库地址

$ git remote -v
gitee   [email protected]:xxx/java-Core.git (fetch)
gitee   [email protected]:xxx/java-Core.git (push)
github  [email protected]:xxx/javaCore.git (fetch)
github  [email protected]:xxx/javaCore.git (push)

以上就是添加完成的仓库地址

第六步,添加密钥,并检查密钥是否可行

1.生成ssh密钥

windows系统中打开cmd

执行下面这个命令

ssh-keygen -t rsa -C [email protected]

然后一路回车健,默认会生成到C:\Users\DELL\.ssh 这个地址然后打开.ssh,会生成id_rsa,id_rsa.pub,这一对公私钥,把公钥id_rsa.pub用编辑器打开

2.添加ssh密钥

在gitee和github的个人信息中添加ssh公钥,登陆自己gitee账户,和github账户

3.检查是否添加成功

ssh -T [email protected]
ssh -T [email protected]

会出现 You've successfully authenticated。你已经通过验证

第七步,测试提交代码

1.添加测试文件

在本地workspace中新建一个test.txt文件

2.推送到远程仓库

git pull gitee master
git pull github master
git add .
git commit -m "test file"
git push gitee master
git push github master

在远程端,查看刚刚添加的文件

第八步,常见错误总结

在这个过程中遇到的一些坑,在下面总结

$ git pull gitee master
From gitee.com:xxx/java-Core
 * branch            master     -> FETCH_HEAD
fatal: refusing to merge unrelated histories


$ git add .

DELL@felix MINGW64 /d/work/workspace (master)
$ git push gitee master
To gitee.com:xxxxx/java-Core.git
 ! [rejected]        master -> master (non-fast-forward)
error: failed to push some refs to 'gitee.com:xxxx/java-Core.git'
hint: Updates were rejected because the tip of your current branch is behind
hint: its remote counterpart. Integrate the remote changes (e.g.
hint: 'git pull ...') before pushing again.
hint: See the 'Note about fast-forwards' in 'git push --help' for details



 git pull gitee master
From gitee.com:xxxxx/java-Core
 * branch            master     -> FETCH_HEAD
fatal: refusing to merge unrelated histories

git pull origin master --allow-unrelated-histories
fatal: 'origin' does not appear to be a git repository
fatal: Could not read from remote repository.

Please make sure you have the correct access rights
and the repository exists.


git pull gitee  master --allow-unrelated-histories
From gitee.com:xxxx/java-Core
 * branch            master     -> FETCH_HEAD
Merge made by the 'recursive' strategy.
 LICENSE | 201 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
 1 file changed, 201 insertions(+)
 create mode 100644 LICENSE


 $ git pull gitee master
From gitee.com:xxxxx/java-Core
 * branch            master     -> FETCH_HEAD
Already up to date

第九步,个人总结

想着已学习的方式,将本地代码,把本地代码同步到不同的远程仓库中去,以记录学习为主目的,掌握技能为主要想法,如果有说的或是做的不对的地方,请多指正,我认为在不同的讨论中肯定回得不一样的答案,欢迎提出不同的声音,谢谢大家!

03-05 22:54