赞
踩
1)ssh-keygen
本地端命令行上执行ssh-keygen
命令,
得到生成的密钥(公钥):cat ~/.ssh/id_rsa.pub
,
打开GitHub或Gitee,在头像展开设置中找到SSH公钥,新建SSH,随便取名,并将上面id_rsa.pub
内容复制粘贴到公钥文本框中,到此密钥便配置完成。
通过命令查看本地与远端Gitee是否连接成功:
ssh -T git@gitee.com
Hi asdfv1929(@asdfv1929)! You've successfully authenticated, but GITEE.COM does not provide shell access.
在Gitee上新建仓库test
:
创建完成后,页面会跳转到如下内容,显示常用的Git命令操作:
通过git remote add name url
在本地仓库端添加远程仓库的路径:
git remote add gitee https://gitee.com/asdfv1929/test.git
git remote -v
查看本地的远程仓库:
git remote
gitee
git remote -v # -v 表示显示详细内容
gitee https://gitee.com/asdfv1929/test.git (fetch)
gitee https://gitee.com/asdfv1929/test.git (push)
先看一下本地有哪些分支,之后便将本地仓库中的所有分支都推送到远端仓库上。
git branch -av
* master cb27ad9 add file2 second edit
temp 1395813 add readme
git push gitee --all # 所有分支都推送到指定名称的远端上,,或者是指定分支名
Enumerating objects: 7, done.
Counting objects: 100% (7/7), done.
Delta compression using up to 6 threads
Compressing objects: 100% (5/5), done.
Writing objects: 100% (7/7), 561 bytes | 561.00 KiB/s, done.
Total 7 (delta 1), reused 0 (delta 0), pack-reused 0
remote: Powered by GITEE.COM [GNK-6.4]
To https://gitee.com/asdfv1929/test.git
* [new branch] master -> master
* [new branch] temp -> temp
推送成功后,远程仓库中刷新下页面就能看到已包含相关内容:
在推送过程中也可能会遇到这样的问题:
git push gitee master # 将本地仓库的master分支推送到远端
To https://gitee.com/asdfv1929/test.git
! [rejected] master -> master (fetch first)
error: failed to push some refs to 'https://gitee.com/asdfv1929/test.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.
上述过程中,其他分支推送正常,但master分支报错:! [rejected] master -> master (fetch first)
,
这是因为远端仓库中也有个master分支(两边分支内容不一致),此时需要先将远端master拉取到本地,进行合并后,再去push到远端。
拉取指定远程端的特定分支master到本地:
git fetch gitee master # 拉取指定远程端的特定分支,会形成单独的一棵树
remote: Enumerating objects: 5, done.
remote: Counting objects: 100% (5/5), done.
remote: Compressing objects: 100% (2/2), done.
remote: Total 3 (delta 1), reused 0 (delta 0), pack-reused 0
Unpacking objects: 100% (3/3), 956 bytes | 43.00 KiB/s, done.
From https://gitee.com/asdfv1929/test
* branch master -> FETCH_HEAD
0c04709..7610824 master -> gitee/master
在本地端将远端的master分支合并到本地端master分支上:
git checkout master # 先切换到本地master分支
git branch -av # 查看所有分支
* master 5343aab second edit
temp 1395813 add readme
remotes/gitee/master 7610824 update readme.
remotes/gitee/temp 1395813 add readme
# 合并
git merge --allow-unrelated-histories gitee/master
Auto-merging readme
CONFLICT (content): Merge conflict in readme
Automatic merge failed; fix conflicts and then commit the result.
但此时遇到报错,Git提示在readme文件中发生冲突。
那么在本地就去看下readme文件的内容,可以发现有如下一些东西:
cat readme
this is a test of push
<<<<<<< HEAD
second edit
=======
hahhahahahaha
hahahahahah
>>>>>>> gitee/master
<<<<<<< HEAD
和 “======” 之间的内容是当前分支之前的修改,
“======” 和>>>>>>> gitee/master
之间内容是远端master分支上的修改。
因此,Git在这边报冲突,其实就是它无法判断哪些需要保留哪些需要删除,此时就需要人为去干预,去保留或是删除文件中冲突的部分。
这边选择都保留下来:
cat readme
this is a test of push
second edit
hahhahahahaha
hahahahahah
保留或删除的人为干预操作做完之后,就可以再次进行添加、提交与最后的推送到远端库上。
git add . git status On branch master All conflicts fixed but you are still merging. (use "git commit" to conclude merge) Changes to be committed: modified: readme git commit -m "merge readme" [master 73c6ad9] merge readme git push gitee master Enumerating objects: 10, done. Counting objects: 100% (10/10), done. Delta compression using up to 6 threads Compressing objects: 100% (5/5), done. Writing objects: 100% (6/6), 548 bytes | 548.00 KiB/s, done. Total 6 (delta 2), reused 0 (delta 0), pack-reused 0 remote: Powered by GITEE.COM [GNK-6.4] To https://gitee.com/asdfv1929/test.git 7610824..73c6ad9 master -> master
远端仓库上的内容已发生更新:
pull = fetch + merge
本文主要讲得是将本地端代码同步到远程仓库中,这个远程仓库可以是互联网上的Gitee或GitHub公共库,也可以是公司或部门内部的私有代码库。对于这同步过程中的操作,得牢记于心。
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。