天天看點

Git & Github操作指南

完成代碼,不是一蹴而就的工作,需要你不斷去調試,重構,疊代。有些時候還需要你回到更早的版本去再去試錯和優化。

在這個過程當中,git可以非常友善的完成代碼版本管理的工作。

是以不光要把代碼在電腦上面寫好,還要同步到github和大家共享。這樣可以互相交流和學習。

什麼是版本控制

git對各個代碼檔案進行版本控制,畢竟項目的代碼總是在不斷的疊代和優化,這中間其實會經曆一個又一個的版本,這就需要一個高效的版本控制工具将其管理起來。

建立一個目錄,并且将這個目錄作為項目目錄,和這個項目相關的所有檔案都會放到這個目錄裡面。

lulei@luleideMacBook-Pro awesomeProject1 % ls
go.mod  main.go
lulei@luleideMacBook-Pro awesomeProject1 % pwd
/Users/lulei/go/src/awesomeProject1      

接下來初始化這個倉庫

lulei@luleideMacBook-Pro awesomeProject1 % ls -al
total 16
drwxr-xr-x  5 lulei  staff  160  5 24 11:11 .
drwxr-xr-x  6 lulei  staff  192  5 23 08:09 ..
drwxr-xr-x  6 lulei  staff  192  5 24 07:42 .idea
-rw-r--r--  1 lulei  staff   32  5 18 09:41 go.mod
-rw-r--r--  1 lulei  staff  373  5 24 11:11 main.go


lulei@luleideMacBook-Pro awesomeProject1 % git init
hint: Using 'master' as the name for the initial branch. This default branch name
hint: is subject to change. To configure the initial branch name to use in all
hint: of your new repositories, which will suppress this warning, call:
hint: 
hint:   git config --global init.defaultBranch <name>
hint: 
hint: Names commonly chosen instead of 'master' are 'main', 'trunk' and
hint: 'development'. The just-created branch can be renamed via this command:
hint: 
hint:   git branch -m <name>
Initialized empty Git repository in /Users/lulei/go/src/awesomeProject1/.git/


//可以看到多了.git這個目錄
lulei@luleideMacBook-Pro awesomeProject1 % ls -al
total 16
drwxr-xr-x  6 lulei  staff  192  5 24 15:22 .
drwxr-xr-x  6 lulei  staff  192  5 23 08:09 ..
drwxr-xr-x  9 lulei  staff  288  5 24 15:22 .git
drwxr-xr-x  6 lulei  staff  192  5 24 07:42 .idea
-rw-r--r--  1 lulei  staff   32  5 18 09:41 go.mod
-rw-r--r--  1 lulei  staff  373  5 24 11:11 main.go      

上面初始化完成之後,還需要對倉庫進行簡單的配置。這樣更好的能夠讓git記錄到底誰在什麼時候對相應的檔案進行什麼操作。

lulei@luleideMacBook-Pro awesomeProject1 % git config --global user.name "lulei"

lulei@luleideMacBook-Pro awesomeProject1 % git config --global user.email "[email protected]"

lulei@luleideMacBook-Pro awesomeProject1 % git config --global --list
user.name=lulei
[email protected]      

當多人協作的時候,一起開發代碼,那上面的配置是十分有意義的。

git簡單操作

檢視目前狀态,可以看到沒有東西需要送出

lulei@luleideMacBook-Pro awesomeProject1 % git status
On branch master

No commits yet

Untracked files:
  (use "git add <file>..." to include in what will be committed)
  .idea/
  go.mod
  main.go

nothing added to commit but untracked files present (use "git add" to track)      

 可以看到git跟蹤到變化啦,需要去送出

lulei@luleideMacBook-Pro awesomeProject1 % git add .
lulei@luleideMacBook-Pro awesomeProject1 % git status
On branch master

No commits yet

Changes to be committed:
  (use "git rm --cached <file>..." to unstage)
  new file:   .idea/.gitignore
  new file:   .idea/awesomeProject1.iml
  new file:   .idea/modules.xml
  new file:   go.mod
  new file:   main.go      

-m是做注釋,每當釋出新版本,做一個版本說明。送出新版本就使用這個指令。

lulei@luleideMacBook-Pro awesomeProject1 % git commit -m "go awesomeProject1 project"
[master (root-commit) fd1c0b4] go awesomeProject1 project
 5 files changed, 59 insertions(+)
 create mode 100644 .idea/.gitignore
 create mode 100644 .idea/awesomeProject1.iml
 create mode 100644 .idea/modules.xml
 create mode 100644 go.mod
 create mode 100644 main.go


lulei@luleideMacBook-Pro awesomeProject1 % git status                                
On branch master
nothing to commit, working tree clean      
Git &amp; Github操作指南

從無到有建立檔案,或者對原來的檔案進行了修改。git add之後就從以修改變為以暫存的狀态。最後一種狀态就是以送出,運作commit指令之後。

Git &amp; Github操作指南

 這三種狀态又對應三個區域。

Git &amp; Github操作指南

建立檔案和對檔案的編輯操作都是在工作區域進行的, 目前這個檔案是修改狀态,并且處于工作區域。

git add 之後,檔案狀态就發生了變化,從工作區域移動到暫存區域,狀态從以修改變為了以暫存。

git commit 之後就從暫存到送出狀态了,從暫存區域移動到了倉庫區域。

git log指令可以顯示曆史所有送出

lulei@luleideMacBook-Pro awesomeProject1 % git log
commit 18421665952d87455f170174c916c3095fced526 (HEAD -> master)
Author: lulei <[email protected]>
Date:   Tue May 24 15:52:46 2022 +0800

    go awesomeProject1 project

commit fd1c0b41066849ba8bb00002b9adf9a78070ba78
Author: lulei <[email protected]>
Date:   Tue May 24 15:45:45 2022 +0800

    go awesomeProject1 project      

将本地倉庫同步到遠端GitHub倉庫

github就是開源項目的聚集平台,每個人都可以将自己代碼釋出到上面。

本地對代碼做了修改和送出,怎麼将其同步到GitHub遠端倉庫呢?

添加倉庫有兩種方式,一種是從頭建立一個倉庫,另外一種是fork别人的倉庫。

Git &amp; Github操作指南
Git &amp; Github操作指南
Git &amp; Github操作指南

倉庫選擇ssh協定, 因為使用https協定的話,在同步本地倉庫和遠端倉庫的時候,都需要輸入使用者名和密碼,使用ssh協定隻需要一開始配置好公鑰和私鑰就行了。

現在是需要将本地倉庫同步到遠端倉庫

給我們的本地倉庫添加遠端倉庫

lulei@luleideMacBook-Pro awesomeProject1 % git remote add origin [email protected]:lovekeepcoding/test_git.git

lulei@luleideMacBook-Pro awesomeProject1 % git remote
origin      

 配置公鑰和私鑰,私鑰不要洩漏出去,公鑰無所謂

lulei@luleideMacBook-Pro awesomeProject1 % ssh-keygen -t rsa -C "[email protected]"
Generating public/private rsa key pair.
Enter file in which to save the key (/Users/lulei/.ssh/id_rsa): 
Enter passphrase (empty for no passphrase): 
Enter same passphrase again: 
Your identification has been saved in /Users/lulei/.ssh/id_rsa.
Your public key has been saved in /Users/lulei/.ssh/id_rsa.pub.
The key fingerprint is:
SHA256:WJSTIo3pVBP1z8EXZZhtV9Pq9cI1l+9xgW03EfEUTX0 [email protected]
The key's randomart image is:
+---[RSA 3072]----+
|     =+ooo    .@#|
|    = oo+. .  o=E|
|   o . .... o +o*|
|    .  o   o +.B=|
|      . S   oo.oO|
|              +.+|
|               oo|
|                .|
|                 |
+----[SHA256]-----+


lulei@luleideMacBook-Pro awesomeProject1 % cd /Users/lulei/.ssh
lulei@luleideMacBook-Pro .ssh % ls
id_rsa  私鑰  id_rsa.pub 公鑰 known_hosts      

 将公鑰裡面的内容複制

lulei@luleideMacBook-Pro .ssh % cat id_rsa.pub 
ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAABgQDh/yd+c8pi10Sk83D3OqMwKN8WkxTw+TB6nAOhk4Eqqs1pTTq9dqT9H+cUL1oXVizHWeM6++AGxUWECDiF9Xswd9BSM665bgeDFWPsuDp8IfFhZVSzUP0WUXMyC5l9Iy5uuwKuZ3BeEzIyDBZX52/PN4PVCZHsGrfKWdZDDxS273SJtEM0qpSEKy0WMptyy6NWrM04B++CUmH7FVwWYydWs40euhzjs+CT2l2jboxbJ2eyHfM4Px6QyA10vfwhVN6hnTdRvTI7DFN2l7ZRWuWilDx3c7LWVoLngnU5BnwLlzM54u2ZnIpdHt9V0EjQtCZZqhgtpn4sfBK5shOzSsGTIs4KIDxQMbdIiJNOtxGksb9vogHceg2ZlsXOv5ItGiwOqH2Xzn6Yqm+jkZS+o/x/igHJ62bvYf+rpD+/tCjsRJWLmP1OtTkQptBGPB1bGG60CgBLz/EZtHunzObc9buy5eR4g5dE6tq4q69h8L3zIiYrG8fIqZuZZ/oi0= [email protected]      
Git &amp; Github操作指南
lulei@luleideMacBook-Pro awesomeProject1 % ssh -T [email protected]                            
The authenticity of host 'github.com (20.205.243.166)' can't be established.
ECDSA key fingerprint is SHA256:p2QAMXNIC1TJYWeIOttrVc98/R1BUFWu3/LiyKgUfQM.
Are you sure you want to continue connecting (yes/no/[fingerprint])? yes
Warning: Permanently added 'github.com,20.205.243.166' (ECDSA) to the list of known hosts.
Hi lovekeepcoding! You've successfully authenticated, but GitHub does not provide shell access.      
lulei@luleideMacBook-Pro awesomeProject1 % git remote -v
origin  https://github.com/lovekeepcoding/test_git.git (fetch)
origin  https://github.com/lovekeepcoding/test_git.git (push)


lulei@luleideMacBook-Pro awesomeProject1 % git remote remove origin


lulei@luleideMacBook-Pro awesomeProject1 % git remote add origin [email protected]:lovekeepcoding/test_git.git

lulei@luleideMacBook-Pro awesomeProject1 % git remote -v           
origin  [email protected]:lovekeepcoding/test_git.git (fetch)
origin  [email protected]:lovekeepcoding/test_git.git (push)

lulei@luleideMacBook-Pro awesomeProject1 % git push -u origin main 
Enumerating objects: 12, done.
Counting objects: 100% (12/12), done.
Delta compression using up to 8 threads
Compressing objects: 100% (11/11), done.
Writing objects: 100% (12/12), 1.62 KiB | 1.62 MiB/s, done.
Total 12 (delta 1), reused 0 (delta 0), pack-reused 0
remote: Resolving deltas: 100% (1/1), done.
To github.com:lovekeepcoding/test_git.git
 * [new branch]      main -> main
Branch 'main' set up to track remote branch 'main' from 'origin'.