GIT
Git是一個分布式的版本控制系統,只是軟件,需要你下載裝到電腦上,實現git功能。
Github、Gitee基於git的項目托管平台。Github是國外的,連接速度因人而異;另外Github收費用戶才能創建私有項目。
准備內容
- 注冊碼雲(Gitee),創建一個項目,得到項目url:https://gitee.com/YourGiteeName/projectname。https://gitee.com/signup
- 下載git, 默認安裝。https://git-scm.com/downloads
- 下載安裝VSCode。https://code.visualstudio.com/
一、生成ssh公鑰
1.打開Git Bash,按如下命令來生成 sshkey:
Administrator@JOY MINGW64 ~
$ ssh-keygen -t rsa -C joypoint@qq.com
Generating public/private rsa key pair.
Enter file in which to save the key (/c/Users/Administrator/.ssh/id_rsa):
Created directory '/c/Users/Administrator/.ssh'.
Enter passphrase (empty for no passphrase):
Enter same passphrase again:
Your identification has been saved in /c/Users/Administrator/.ssh/id_rsa.
Your public key has been saved in /c/Users/Administrator/.ssh/id_rsa.pub.
The key fingerprint is:
SHA256:UoQbxa…… joypoint@qq.com
The key's randomart image is:
+---[RSA 3072]----+
| .o=.o.+o o .. |
| . =o o. |
| +. +*+.. . |
|. . o o |
|.+ + . o |
|o = . |
|.o . |
|.o . |
| o |
+----[SHA256]-----+
還可以用以下命令指定id_rsa的別名,用以配置多個SSH-Key:
$ ssh-keygen -t rsa -C 'xxxxx@company.com' -f ~/.ssh/gitee_id_rsa
2.查看 public key:
Administrator@JOY MINGW64 ~
$ cat ~/.ssh/id_rsa.pub
cat: /c/Users/Administrator/: Is a directory
ssh-rsa AAAAB3NzaC1y…… joypoint@qq.com
打開碼雲SSH公鑰管理頁面: https://gitee.com/profile/sshkeys
填寫標題,如:yourname's SSH key
復制公鑰,如:ssh-rsa UoQbxa……
添加后,回到Git Bash中繼續其他操作。
3.用ssh命令測試是否配置成功:
Administrator@JOY MINGW64 ~
$ ssh -T git@gitee.com
The authenticity of host 'gitee.com (120.55.226.24)' can't be established.
ECDSA key fingerprint is SHA256:FQGC9…….
Are you sure you want to continue connecting (yes/no/[fingerprint])? Yes
Warning: Permanently added 'gitee.com,120.55.226.24' (ECDSA) to the list of known hosts.
Hi JoyPoint! You've successfully authenticated, but GITEE.COM does not provide shell access.
二、Git操作-初始化Git
Administrator@JOY MINGW64 ~
$ git config --global user.name JoyPoint
Administrator@JOY MINGW64 ~
$ git config --global user.email JoyPoint@qq.com
三、創建版本庫
1.首先,選擇一個合適的地方,創建一個空目錄YourProjName(名字任意):
Administrator@JOY MINGW64 /
$ cd /c/
Administrator@JOY MINGW64 /c
$ mkdir helloGIT
Administrator@JOY MINGW64 /c
$ cd helloGIT
(在第一次創建並初始化版本庫以后,再次需要修改該庫時,只需要在git bash中進入該目錄即可,並可以用git remote -v查看關聯情況)
2.第二步,通過git init命令把這個目錄變成Git可以管理的倉庫:
Administrator@JOY MINGW64 /c/helloGIT
$ git init
Initialized empty Git repository in C:/helloGIT/.git/
四、關聯遠程倉庫
1.把一個本地倉庫與一個雲端Gitee倉庫關聯:
項目地址形式為:https://gitee.com/YourGiteeName/YourProjName.git 或者 git@gitee.com:YourGiteeName/YourProjName.git
Administrator@JOY MINGW64 /c/helloGIT (master)
$ git remote add origin https://gitee.com/JoyPoint/helloGIT.git
# 如果你發現地址關聯有錯,或想關聯其他倉庫,可以執行以下命令重新設置關聯地址:
# $ git remote set-url origin https://gitee.com/JoyPoint/helloGIT.git
# 但一定要注意字母的大小寫,以及文本雙引號問題!!!
2.查看關聯細節:
Administrator@JOY MINGW64 /c/helloGIT (master)
$ git remote -v
origin https://gitee.com/JoyPoint/helloGIT.git (fetch)
origin https://gitee.com/JoyPoint/helloGIT.git (push)
五、同步(拉取)
同步,也可以稱之為拉取,在Git中是非常頻繁的操作,為了保證代碼一致性,盡可能的在每次操作前進行一次同步操作,在工作目錄下執行如下命令:
Administrator@JOY MINGW64 /c/helloGIT (master)
$ git pull origin master
remote: Enumerating objects: 6, done.
remote: Counting objects: 100% (6/6), done.
remote: Compressing objects: 100% (6/6), done.
remote: Total 6 (delta 0), reused 0 (delta 0)
Unpacking objects: 100% (6/6), done.
From https://gitee.com/JoyPoint/helloGIT
* branch master -> FETCH_HEAD
* [new branch] master -> origin/master
Administrator@JOY MINGW64 /c/helloGIT (master)
$ git status
On branch master
Changes not staged for commit:
(use "git add <file>..." to update what will be committed)
(use "git restore <file>..." to discard changes in working directory)
modified: README.md
no changes added to commit (use "git add" and/or "git commit -a")
六、提交
git作為支持分布式版本管理的工具,它管理的庫(repository)分為本地庫、遠程庫。如有本地庫源碼文件發生修改,需要將修改提交到遠程庫,這時需要暫存 (add)、提交(commit)、推送(push)三步:
Administrator@JOY MINGW64 /c/helloGIT (master)
$ git add -A
warning: LF will be replaced by CRLF in README.md.
The file will have its original line endings in your working directory
Administrator@JOY MINGW64 /c/helloGIT (master)
$ git commit -m "注意名稱大小寫和全角標點符號"
[master ee5acbb] 注意名稱大小寫和全角標點符號
1 file changed, 7 insertions(+), 5 deletions(-)
Administrator@JOY MINGW64 /c/helloGIT (master)
$ git push origin master
Enumerating objects: 5, done.
Counting objects: 100% (5/5), done.
Delta compression using up to 4 threads
Compressing objects: 100% (3/3), done.
Writing objects: 100% (3/3), 416 bytes | 416.00 KiB/s, done.
Total 3 (delta 2), reused 0 (delta 0)
remote: Powered By Gitee.com
To https://gitee.com/JoyPoint/helloGIT.git
babe7af..ee5acbb master -> master
七、VSCode中使用git
1.點擊 文件 > 將文件夾添加到工作區 > E:/YourProjName/ 就完成了。
無需任何配置,VSCode自動獲取.git配置實現代碼管理: 發生變動的文件或代碼會有顏色提示。
2.同步遠程倉庫:
- 選擇源控制欄(Source Control)中需要上傳的文件,點擊+號,暫存 (add);
- 在[ 消息 (按 Ctrl+Enter 提交) ]中輸入注釋, 提交(commit);
- 點擊更多動作中的push圖標,推送(push)。
_____________________
# Git配置多個SSH-Key # 在 ~/.ssh 目錄下新建一個config文件,添加如下內容(其中Host和HostName填寫git服務器的域名,IdentityFile指定私鑰的路徑) # gitee Host gitee.com HostName gitee.com PreferredAuthentications publickey IdentityFile ~/.ssh/gitee_id_rsa # github Host github.com HostName github.com PreferredAuthentications publickey IdentityFile ~/.ssh/github_id_rsa
______________________
基於碼雲的協同開發實踐 需求描述如下: 1.有一個基礎性工程共享給大家使用,缺省不允許使用者直接提交修改 2.使用者在使用過程中會發現問題,需要及時修改基礎工程代碼 3.修改的代碼需要提交給基礎工程管理者審核后合並 4.使用者需要更新最新的基礎工程代碼並再此上繼續工作 基於這個應用場景,可采用碼雲平台提供的fork和pull request(PR)結合來完成,具體操作步驟如下: 1.首先使用者需要復制一份基礎工程到自己的用戶下,Fork,在自己的用戶下生成工程副本。 2.然后使用git客戶端下載自己用戶下的工程副本進行使用,進行修改並提交。 3.提交后進入自己用戶下的工程副本頁面,進入Pull Requests頁面,並點擊新建Pull Request按鈕,創建PR,在創建PR頁面中,添加修改的內容說明,並指定審查人員,點擊創建。 4.等到基礎工程審查人員審查結束后,代碼已經進行了合並,有了新的版本,此時使用者可以進入自己的工程副本首頁,點擊工程名邊上的強制刷新按鈕獲取工程的最新版本。
——————————————
$ git push origin master remote: You do not have permission to push to the repository via HTTPS fatal: Authentication failed for 'https://gitee.com/someuser/someproject.git/' ———————————————————————————————— 原因分析: 原因之一: 這是由於沒有設置Gitee的SSH公鑰。在未設置SSH公鑰的情況下,可以使用git clone Gitee上的項目,但是不能git push項目到Gitee上,如果想push項目到Gitee,那么必須配置SSH公鑰。 解決方法: 生成公鑰和配置公鑰,可以參考Gitee幫助:https://gitee.com/help/articles/4191 。 原因之二: 可能是這台電腦以前使用過git,所以windows保存的賬號和密碼是其他人的,所以需要進行修改賬號和密碼: 解決方法: (一)進入控制面板 (二)選擇用戶賬戶 (三)選擇管理你的憑據 (四)選擇Windows憑據 (五)選擇git保存的用戶信息 (六)選擇編輯或者進行刪除操作 (七)完成
——————————————
在同一台電腦上切換不同gitee賬號: 1、不同用戶賬號對應的秘鑰對均已在本地生成好,並在gitee.com中添加; 2、.ssh/config文件中IdentityFile修改為對應賬號的rsa; 3、git config --global user.name\email 為對應賬號用戶名和郵箱; 4、檢查控制面板-用戶賬戶-管理你的憑據-普通憑據,確保憑證匹配或為空; 5、用ssh -T git@gitee.com檢查是否已關聯對應賬號;如果沒有請檢查上面幾步。 6、后續按照常規步驟關聯本地目錄和遠程庫; 7、git pull 后,如出現沒有readme.MD文件的問題,可以用$ git pull --rebase origin master再次拉取一次。
-------------------------------------------
實現同一本地倉庫與 Gitee 和 GitHub 兩個遠程庫同步更新 將本地的代碼倉庫與 Gitee 和 GitHub 兩個遠程庫同時關聯,即可實現本地倉庫與兩個遠程庫的同步更新 具體方法操作如下:
1:移除現在舊有的遠程服務器origin
git remote rm origin
2:關聯gitosc遠程庫
git remote add gitee https://gitee.com/xxx/xxx.git
git push -u gitosc master
關聯github遠程庫
git remote add github https://github.com/xxx/xxx.git
git push -u github master
現在,用git remote -v查看遠程庫信息,可以看到兩個遠程庫:
git remote -v
gitee git@gitee.com:xxx/xxx.git (fetch)
gitee git@gitee.com:xxx/xxx.git (push)
github git@github.com:xxx/xxx.git (fetch)
github git@github.com:xxx/xxx.git (push)
(2)同步更新 如果要推送到 GitHub,使用命令:
git push GitHub 分支名
eg:git push GitHub master
如果要推送到 Gitee,使用命令:
git push Gitee 分支名
可能提示push失敗這里可以嘗試用強制push:
$ git push github master -f
/* 由於是初始化項目,並從遠程倉庫pull,使用強制推送不會對項目造成影響 一般不推薦強制push */
至此,本地庫就可以同時與多個遠程庫互相同步
參考:
https://blog.csdn.net/watfe/article/details/79761741
https://gitee.com/help/articles/4229#article-header0