29、公私鑰的創(chuàng)建于配置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上創(chuàng)建個人倉庫
點擊Create a repository鏈接
創(chuàng)建選項界面
創(chuàng)建成功后界面
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分支發(fā)生沖突,因此需要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,出現(xiàn)問題
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.不同人修改了同文件的同區(qū)域如何處理
當在開發(fā)中,不同工作人員對同一個文件的相同區(qū)域做了修改時,此時git進行merge就會發(fā)生錯誤,需要手工進行merge,確定需要保留的內(nèi)容。首先遠程修改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
另外有需要云服務(wù)器可以了解下創(chuàng)新互聯(lián)scvps.cn,海內(nèi)外云服務(wù)器15元起步,三天無理由+7*72小時售后在線,公司持有idc許可證,提供“云服務(wù)器、裸金屬服務(wù)器、高防服務(wù)器、香港服務(wù)器、美國服務(wù)器、虛擬主機、免備案服務(wù)器”等云主機租用服務(wù)以及企業(yè)上云的綜合解決方案,具有“安全穩(wěn)定、簡單易用、服務(wù)可用性高、性價比高”等特點與優(yōu)勢,專為企業(yè)上云打造定制,能夠滿足用戶豐富、多元化的應(yīng)用場景需求。