重庆分公司,新征程启航
为企业提供网站建设、域名注册、服务器等服务
29、公私钥的创建于配置github
为了在本地更好的操作远程仓库,可以在本地与远程仓库之间配置公私钥连接,首先可以查看本地是否有公私钥,如果没有可以使用以下命令生成。
$ ls ~/.ssh/ #查看是否存在公私钥
id_rsa id_rsa.pub known_hosts
ssh-keygen -t rsa -b 4096 -C "your_email@example.com" #如果不存在可使用该命令生成
生成公私钥后,将公钥配置到github上。点击头像,进入Your Profile->SSH and GPG Keys即可进入配置公钥界面
30.在GitHub上创建个人仓库
点击Create a repository链接
创建选项界面
创建成功后界面
31.把本地仓库同步到GitHub
首先将github的仓库地址配置到本地,可以在箭头指示的地方获取仓库的地址
$ git remote -v
zhineng file:///d/git_learning/.git (fetch)
zhineng file:///d/git_learning/.git (push)
$ git remote add github git@github.com:embedlinux/git_learning.git
$ git remote -v
github git@github.com:embedlinux/git_learning.git (fetch)
github git@github.com:embedlinux/git_learning.git (push)
zhineng file:///d/git_learning/.git (fetch)
zhineng file:///d/git_learning/.git (push)
将本地文件推送到github上,此时可以知道Jone和temp分支push成功,而master失败
$ git push github --all
Counting objects: 16, done.
Delta compression using up to 4 threads.
Compressing objects: 100% (9/9), done.
Writing objects: 100% (16/16), 1.29 KiB | 77.00 KiB/s, done.
Total 16 (delta 2), reused 0 (delta 0)
To github.com:embedlinux/git_learning.git
* [new branch] Jone -> Jone
* [new branch] temp -> temp
! [rejected] master -> master (fetch first)
error: failed to push some refs to 'git@github.com:embedlinux/git_learning.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.
此时由于github上和本地的master分支发生冲突,因此需要merge
$ git fetch github master #获取远程master分支
remote: Enumerating objects: 3, done.
remote: Counting objects: 100% (3/3), done.
remote: Compressing objects: 100% (2/2), done.
remote: Total 3 (delta 0), reused 0 (delta 0), pack-reused 0
Unpacking objects: 100% (3/3), done.
From github.com:embedlinux/git_learning
* branch master -> FETCH_HEAD
* [new branch] master -> github/master
$ git checkout master #本地切换到master分支
Switched to branch 'master'
$ git branch -av
Jone f0b5fe2 Add a file
* master e0326fb Merge three commits
temp b0fc955 Merge three commits
remotes/github/Jone f0b5fe2 Add a file
remotes/github/master 831e37e Initial commit
remotes/github/temp b0fc955 Merge three commits
remotes/zhineng/Jone f0b5fe2 Add a file
$ git merge github/master
fatal: refusing to merge unrelated histories
$ git merge --allow-unrelated-histories github/master #合并远程分支
Merge made by the 'recursive' strategy.
LICENSE | 21 +++++++++++++++++++++
1 file changed, 21 insertions(+)
create mode 100644 LICENSE
$ git push github master #再次push到github上,成功
Counting objects: 2, done.
Delta compression using up to 4 threads.
Compressing objects: 100% (2/2), done.
Writing objects: 100% (2/2), 373 bytes | 186.00 KiB/s, done.
Total 2 (delta 0), reused 0 (delta 0)
To github.com:embedlinux/git_learning.git
831e37e..707f0f8 master -> master
32.不同人修改了不同文件如何处理
当不同的人工作在同一个branch,但是修改的是不同文件时,此时可以使用merge命令对文件进行更新。例如,当前在远程修改了Jone分支的read1.txt文件。
在本地修改"second.txt"文件
$ git checkout Jone
Switched to branch 'Jone'
Your branch is up to date with 'zhineng/Jone'.
$ echo "Update the file " >> second.txt
$ git add .
$ git commit -m"Update second file"
[Jone 568a695] Update second file
1 file changed, 1 insertion(+)
$ git push github Jone #此时由于本地文件与远程不是同一个commit,出现问题
To github.com:embedlinux/git_learning.git
! [rejected] Jone -> Jone (fetch first)
error: failed to push some refs to 'git@github.com:embedlinux/git_learning.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.
$ git fetch github
$ git branch -av
* Jone 568a695 [behind 2] Update second file
master 707f0f8 Merge remote-tracking branch 'github/master'
temp b0fc955 Merge three commits
remotes/github/Jone 139cca2 Update read1.txt
remotes/github/master 707f0f8 Merge remote-tracking branch 'github/master'
remotes/github/temp b0fc955 Merge three commits
remotes/zhineng/Jone 0b88b5a Merge remote-tracking branch 'github/Jone' into Jone
$ git merge remotes/github/Jone
error: Your local changes to the following files would be overwritten by merge:
read1.txt
Please commit your changes or stash them before you merge.
Aborting
$ git merge remotes/github/Jone
Merge made by the 'recursive' strategy.
$ git push github
Counting objects: 6, done.
Delta compression using up to 4 threads.
Compressing objects: 100% (5/5), done.
Writing objects: 100% (6/6), 624 bytes | 156.00 KiB/s, done.
Total 6 (delta 3), reused 0 (delta 0)
To github.com:embedlinux/git_learning.git
139cca2..00233dd Jone -> Jone
33.不同人修改了同文件的同区域如何处理
当在开发中,不同工作人员对同一个文件的相同区域做了修改时,此时git进行merge就会发生错误,需要手工进行merge,确定需要保留的内容。首先远程修改first1.md文件。
$ echo "Update the file on local" >> first.md #本地也修改该文件
$ git commit -am"Update first.md"
[Jone b6870f1] Update first.md
1 file changed, 1 insertion(+)
$ git fetch github #获取远程仓库
remote: Enumerating objects: 5, done.
remote: Counting objects: 100% (5/5), done.
remote: Compressing objects: 100% (3/3), done.
remote: Total 3 (delta 1), reused 0 (delta 0), pack-reused 0
Unpacking objects: 100% (3/3), done.
From github.com:embedlinux/git_learning
00233dd..c87da97 Jone -> github/Jone
$ git branch -av
* Jone b6870f1 [ahead 3, behind 1] Update first.md
master 707f0f8 Merge remote-tracking branch 'github/master'
temp b0fc955 Merge three commits
remotes/github/Jone c87da97 Update first.md
remotes/github/master 707f0f8 Merge remote-tracking branch 'github/master'
remotes/github/temp b0fc955 Merge three commits
remotes/zhineng/Jone 0b88b5a Merge remote-tracking branch 'github/Jone' into Jone
$ git merge c87da97 #由于远程和本地修改相同文件,此时无法merge
Auto-merging first.md
CONFLICT (content): Merge conflict in first.md
Automatic merge failed; fix conflicts and then commit the result.
$ vim first.md #手工修改冲突文件
修改前
修改后,同时保留远程和本地的修改
$ git commit -am"Merge conflict file" #再次提交
[Jone c2568ff] Merge conflict file
$ git push github --all #push成功
Counting objects: 6, done.
Delta compression using up to 4 threads.
Compressing objects: 100% (5/5), done.
Writing objects: 100% (6/6), 601 bytes | 150.00 KiB/s, done.
Total 6 (delta 2), reused 0 (delta 0)
To github.com:embedlinux/git_learning.git
c87da97..c2568ff Jone -> Jone
另外有需要云服务器可以了解下创新互联scvps.cn,海内外云服务器15元起步,三天无理由+7*72小时售后在线,公司持有idc许可证,提供“云服务器、裸金属服务器、高防服务器、香港服务器、美国服务器、虚拟主机、免备案服务器”等云主机租用服务以及企业上云的综合解决方案,具有“安全稳定、简单易用、服务可用性高、性价比高”等特点与优势,专为企业上云打造定制,能够满足用户丰富、多元化的应用场景需求。