001 手把手用Git,Git從入門到上傳本地專案到Github,看這篇就夠了

皿哥的技術人生發表於2022-06-11

安裝git

下載Git
下載好後,一路next即可
安裝好後,開啟Git bash,進行配置

首先配置自己的身份

git config --global user.name "Name"
git config --global user.email "name@gmail.com"  

mark

檢查是否配置成功:

git config --global user.name 
git config --global user.email 

mark

建立程式碼倉庫

倉庫是用於儲存版本管理所需資訊的地方,所有本地提交的程式碼都會被提交到程式碼倉庫中,如果有需要還可以再推送到遠端倉庫中。

若要為某專案建立程式碼倉庫,則在git bash中,進入該專案根目錄下,然後執行git init即可:

mark

進入對應目錄,看到生成了一個.git隱藏資料夾,表示倉庫建立成功
mark
如果想要刪除本地倉庫, 只需要刪除這個.git隱藏檔案即可

提交原生程式碼:

程式碼倉庫建立完成之後即可提交程式碼,提交程式碼只需要使用add和commit兩個命令即可

  • add命令用於新增想要提交的程式碼
  • commit則是真正去執行提交操作

例如:
//新增build.gradle檔案

git add build.gradle

// 新增app目錄

git add app

// 新增所有檔案

git add .

現在本地倉庫內的所有檔案都已經新增好了,可以進行一次提交了,執行如下命令:

git commit -m "First commit"

注意:在commit命令之後,我們一定需要通過-m引數來加上提交的描述資訊,沒有描述資訊的提交被認為是不合法的。
執行完上條命令,所有的程式碼都已經成功提交了!

檢視修改內容

git status
在專案根目錄下執行 git status命令,就可以通過Git來檢視自上次提交後哪些檔案有變化

知道了哪些檔案有變化,要如何知道這些檔案具體變動了什麼呢?
需要用到git diff命令

git diff

如果需要知道具體某個檔案變動了什麼,

git diff + 檔名稱
例如:
git diff app/src/main/java/com/example/mosesmin/MainActivity.java

撤銷未提交也未新增的修改

如上文所述:

  • 所謂提交,即執行了git commit命令
  • 所謂新增,即執行了git add命令

要撤銷未提交也未新增的修改,用chekout命令

git checkout

例如,如果對app/src/main/java/com/example/mosesmin/MainActivity.java進行了修改,但是沒有執行git add命令新增它,就可以為其執行git diff命令

git diff app/src/main/java/com/example/mosesmin/MainActivity.java

執行了上述命令後,我們對MainActivity.java 這個檔案所做的修改就應該被撤銷了,我們重新執行一下git status命令,檢查一下,可以發現,專案中沒有任何可以提交的檔案,說明撤銷操作確實是成功了。

撤銷未提交已新增的修改

要撤銷未提交卻已新增的修改,先用reset命令執行一下,取消新增;再執行一遍checkout命令,執行撤銷。
如果一個檔案已經執行了新增操作

git reset

檢視提交記錄

檢視提交記錄,使用git log命令

git log

如果提交記錄很多,可以在命令中制定id,並加上 -1引數,表示我們只想看到一條記錄,如下所示:

git log 提交記錄id號 -1

如果想要檢視查詢的這條提交記錄中具體修改了什麼內容,可以在命令中加入 -p引數,命令如下所示:

git log 提交記錄id號 -1 -p

查詢的結果中,減號代表刪除的部分,加號代表增加的部分

分支的用法

回顧一下上文,如果需要對某專案進行建立倉庫和提交專案專案程式碼到倉庫,則執行下述命令

git init
git add .
git commit -m "First Commit"

分支的概念:
分支是版本控制中比較高階且比較重要的一個概念,它主要的作用就是在現有程式碼的基礎上開闢一個分叉口,使得程式碼可以在主幹線和分支線上同時進行開發,且相互之間不受影響。
分支的工作原理示例圖如下:
mark

使用git branch命令檢視分支
如果要建立分支,例如執行如下命令:

git branch version1.0

如果是在master分支下,則執行:

git branch -m version1.0

版本庫的切換:
mark

與遠端版本庫協作--例如與Github上的倉庫協作

去Github上建立一個倉庫(詳細的建立過程這裡不介紹了,不會可以度娘):AndroidProgramming3eMm

建立完成之後的快速指南頁面:Quick setup

看到官方推薦的在本地建立新倉庫並與Github上AndroidProgramming3eMm倉庫關聯的操作的步驟,我在每個步驟後加了註釋:

echo "# AndroidProgramming3eMm" >> README.md
git init  // 建立倉庫,把這個目錄變成Git可以管理的倉庫
git add README.md // 檔案新增到倉庫
git commit -m "first commit" //把檔案提交到倉庫
git branch -M main // 建立分支
git remote add origin https://github.com/mosesmindev/AndroidProgramming3eMm.git // 將本地倉庫與遠端倉庫相關聯
git push -u origin main /把本地庫的所有內容推送到遠端庫上

看到其中涉及分支的命令:
git branch -M main

其實以前的命令是:
git branch -m master

之所以github官方有修改,是近年來西方左派的政治正確導致的,特別是2020年的黑命貴運動等,master主人的意思對黑人不友好,所以改為了main主要

我們這裡分支沿用:git branch -m master
最後一步推送到遠端庫:git push -u origin master

如果本地已經有了倉庫,與Github上AndroidProgramming3eMm倉庫關聯的操作的步驟:

git remote add origin https://github.com/mosesmindev/AndroidProgramming3eMm.git
git branch -M main
git push -u origin main

我們本地已經有了倉庫,所以操作步驟如下:

git branch -m master //其實也不用這步,因為我們使用Git bash預設的倉庫分支就是master
git remote add origin https://github.com/mosesmindev/AndroidProgramming3eMm.git
git push -u origin master // 暫時不確認-u引數的含義

但是我們發現執行 git push -u origin master 後上傳程式碼並沒有成功,出現瞭如下報錯資訊:

$ git push -u origin master
error: src refspec master does not match any
error: failed to push some refs to 'https://github.com/mosesmindev/AndroidProgramming3eMm.git'

mark
因為我們沒有對Github賬戶設定SSH key

為Github賬戶設定SSH key

cd ~/.ssh //檢視C:\Users\使用者名稱.ssh 是否有key
ssh-keygen -t rsa -C "PeterChenjinxu@outlook.com" // 如果沒有要自己生成

mark

mark
如上圖,我們得到提示:

Your identification has been saved in /c/Users/HONOR/.ssh/id_rsa
Your public key has been saved in /c/Users/HONOR/.ssh/id_rsa.pub

表示我們成功生成了key,路徑為:/c/Users/HONOR/.ssh/id_rsa.pub,HONOR為使用者名稱,具體到您自己的使用者名稱下查詢;我們在該目錄下用記事本或其他文字編輯器開啟
id_rsa.pub,的到ssh key公鑰。
如下圖,我們使用SublimeText開啟id_rsa.pub
mark

之後,切換到github網站,展開個人頭像的小三角,點選settings
mark
然後開啟SSH and GPG keys選單
mark
點選New SSH key新增金鑰
mark
填上標題,建議跟倉庫名稱 AndroidProgramming3eMm 保持一致吧,方便日後區分;接著將id_rsa.pub檔案中key貼上到此,最後點選Add SSH key生成金鑰吧
mark
建立成功:

如此,github賬號的SSH keys配置完成。

上傳專案到Github

如果已經設定了SSH key,此時執行git push依然出現下圖問題,可能是本地倉庫為空(注意咯:git是不能管理空的資料夾的,資料夾裡必須有檔案才能add),或者本地倉庫沒有正確的專案導致的
mark

我們在本地倉庫AndroidProgramming3eMm下拷貝一個用Android Studio的標準Android專案GeoQuiz
mark
GeoQuiz結構如下:
mark

然後我們執行相關的命令:

git init // 建立倉庫,初始化建立成功後你會發現專案裡多了一個隱藏資料夾.git,這個目錄是Git用來跟蹤管理版本庫的,一般不要動

git add . // 接著,將所有檔案新增到倉庫

怕上圖中的warning壞事兒,可以再git add一次

git commit -m "001 first commit by MosesMin" //然後,把檔案提交到倉庫,雙引號內是提交註釋

git remote add origin https://github.com/mosesmindev/AndroidProgramming3eMm.git // 關聯Github上建立的倉庫AndroidProgramming3eMm

git push -u origin master // 上傳原生程式碼到Github上建立的倉庫AndroidProgramming3eMm

到此,原生程式碼已經推送到github倉庫了,見證成功的時刻到了,我們現在去githubt倉庫看看:
剛建立的空專案是這樣的:

重新整理一下頁面,見證成功的時刻來臨了,我們的提交註釋和提交的本地倉庫內容都在了
mark

copy記錄一下整個過程:
HONOR@MosesMin-HonorMagicbook16pro2021 MINGW64 /e/DownloadFiles/BaiduNetdiskDownload/AndroidStudy/00 PDF/AndroidProgramming3eMm
$ git init
Initialized empty Git repository in E:/DownloadFiles/BaiduNetdiskDownload/AndroidStudy/00 PDF/AndroidProgramming3eMm/.git/

HONOR@MosesMin-HonorMagicbook16pro2021 MINGW64 /e/DownloadFiles/BaiduNetdiskDownload/AndroidStudy/00 PDF/AndroidProgramming3eMm (master)
$ git add.
git: 'add.' is not a git command. See 'git --help'.

The most similar command is
        add

HONOR@MosesMin-HonorMagicbook16pro2021 MINGW64 /e/DownloadFiles/BaiduNetdiskDownload/AndroidStudy/00 PDF/AndroidProgramming3eMm (master)
$ git add .
warning: LF will be replaced by CRLF in 01_FirstApp/GeoQuiz/.gitignore.
The file will have its original line endings in your working directory
warning: LF will be replaced by CRLF in 01_FirstApp/GeoQuiz/.idea/compiler.xml.
The file will have its original line endings in your working directory
warning: LF will be replaced by CRLF in 01_FirstApp/GeoQuiz/.idea/copyright/profiles_settings.xml.
The file will have its original line endings in your working directory
warning: LF will be replaced by CRLF in 01_FirstApp/GeoQuiz/.idea/encodings.xml.
The file will have its original line endings in your working directory
warning: LF will be replaced by CRLF in 01_FirstApp/GeoQuiz/.idea/gradle.xml.
The file will have its original line endings in your working directory
warning: LF will be replaced by CRLF in 01_FirstApp/GeoQuiz/.idea/misc.xml.
The file will have its original line endings in your working directory
warning: LF will be replaced by CRLF in 01_FirstApp/GeoQuiz/.idea/modules.xml.
The file will have its original line endings in your working directory
warning: LF will be replaced by CRLF in 01_FirstApp/GeoQuiz/.idea/runConfigurations.xml.
The file will have its original line endings in your working directory
warning: LF will be replaced by CRLF in 01_FirstApp/GeoQuiz/app/.gitignore.
The file will have its original line endings in your working directory
warning: LF will be replaced by CRLF in 01_FirstApp/GeoQuiz/app/build.gradle.
The file will have its original line endings in your working directory
warning: LF will be replaced by CRLF in 01_FirstApp/GeoQuiz/app/proguard-rules.pro.
The file will have its original line endings in your working directory
warning: LF will be replaced by CRLF in 01_FirstApp/GeoQuiz/app/src/androidTest/java/com/bignerdranch/android/geoquiz/ExampleInstrumentedTest.java.
The file will have its original line endings in your working directory
warning: LF will be replaced by CRLF in 01_FirstApp/GeoQuiz/app/src/main/AndroidManifest.xml.
The file will have its original line endings in your working directory
warning: LF will be replaced by CRLF in 01_FirstApp/GeoQuiz/app/src/main/java/com/bignerdranch/android/geoquiz/QuizActivity.java.
The file will have its original line endings in your working directory
warning: LF will be replaced by CRLF in 01_FirstApp/GeoQuiz/app/src/main/res/layout/activity_quiz.xml.
The file will have its original line endings in your working directory
warning: LF will be replaced by CRLF in 01_FirstApp/GeoQuiz/app/src/main/res/values-w820dp/dimens.xml.
The file will have its original line endings in your working directory
warning: LF will be replaced by CRLF in 01_FirstApp/GeoQuiz/app/src/main/res/values/colors.xml.
The file will have its original line endings in your working directory
warning: LF will be replaced by CRLF in 01_FirstApp/GeoQuiz/app/src/main/res/values/dimens.xml.
The file will have its original line endings in your working directory
warning: LF will be replaced by CRLF in 01_FirstApp/GeoQuiz/app/src/main/res/values/strings.xml.
The file will have its original line endings in your working directory
warning: LF will be replaced by CRLF in 01_FirstApp/GeoQuiz/app/src/main/res/values/styles.xml.
The file will have its original line endings in your working directory
warning: LF will be replaced by CRLF in 01_FirstApp/GeoQuiz/app/src/test/java/com/bignerdranch/android/geoquiz/ExampleUnitTest.java.
The file will have its original line endings in your working directory
warning: LF will be replaced by CRLF in 01_FirstApp/GeoQuiz/build.gradle.
The file will have its original line endings in your working directory
warning: LF will be replaced by CRLF in 01_FirstApp/GeoQuiz/gradle.properties.
The file will have its original line endings in your working directory
warning: LF will be replaced by CRLF in 01_FirstApp/GeoQuiz/gradle/wrapper/gradle-wrapper.properties.
The file will have its original line endings in your working directory
warning: LF will be replaced by CRLF in 01_FirstApp/GeoQuiz/gradlew.
The file will have its original line endings in your working directory
warning: LF will be replaced by CRLF in 01_FirstApp/GeoQuiz/settings.gradle.
The file will have its original line endings in your working directory
warning: LF will be replaced by CRLF in 01_FirstApp/LICENSE.txt.
The file will have its original line endings in your working directory

HONOR@MosesMin-HonorMagicbook16pro2021 MINGW64 /e/DownloadFiles/BaiduNetdiskDownload/AndroidStudy/00 PDF/AndroidProgramming3eMm (master)
$ git add .

HONOR@MosesMin-HonorMagicbook16pro2021 MINGW64 /e/DownloadFiles/BaiduNetdiskDownload/AndroidStudy/00 PDF/AndroidProgramming3eMm (master)
$ git commit -m "001 first commit by MosesMin"
[master (root-commit) db96b62] 001 first commit by MosesMin
 35 files changed, 843 insertions(+)
 create mode 100644 01_FirstApp/.DS_Store
 create mode 100644 01_FirstApp/GeoQuiz/.gitignore
 create mode 100644 01_FirstApp/GeoQuiz/.idea/compiler.xml
 create mode 100644 01_FirstApp/GeoQuiz/.idea/copyright/profiles_settings.xml
 create mode 100644 01_FirstApp/GeoQuiz/.idea/encodings.xml
 create mode 100644 01_FirstApp/GeoQuiz/.idea/gradle.xml
 create mode 100644 01_FirstApp/GeoQuiz/.idea/misc.xml
 create mode 100644 01_FirstApp/GeoQuiz/.idea/modules.xml
 create mode 100644 01_FirstApp/GeoQuiz/.idea/runConfigurations.xml
 create mode 100644 01_FirstApp/GeoQuiz/app/.gitignore
 create mode 100644 01_FirstApp/GeoQuiz/app/build.gradle
 create mode 100644 01_FirstApp/GeoQuiz/app/proguard-rules.pro
 create mode 100644 01_FirstApp/GeoQuiz/app/src/androidTest/java/com/bignerdranch/android/geoquiz/ExampleInstrumentedTest.java
 create mode 100644 01_FirstApp/GeoQuiz/app/src/main/AndroidManifest.xml
 create mode 100644 01_FirstApp/GeoQuiz/app/src/main/java/com/bignerdranch/android/geoquiz/QuizActivity.java
 create mode 100644 01_FirstApp/GeoQuiz/app/src/main/res/layout/activity_quiz.xml
 create mode 100644 01_FirstApp/GeoQuiz/app/src/main/res/mipmap-hdpi/ic_launcher.png
 create mode 100644 01_FirstApp/GeoQuiz/app/src/main/res/mipmap-mdpi/ic_launcher.png
 create mode 100644 01_FirstApp/GeoQuiz/app/src/main/res/mipmap-xhdpi/ic_launcher.png
 create mode 100644 01_FirstApp/GeoQuiz/app/src/main/res/mipmap-xxhdpi/ic_launcher.png
 create mode 100644 01_FirstApp/GeoQuiz/app/src/main/res/mipmap-xxxhdpi/ic_launcher.png
 create mode 100644 01_FirstApp/GeoQuiz/app/src/main/res/values-w820dp/dimens.xml
 create mode 100644 01_FirstApp/GeoQuiz/app/src/main/res/values/colors.xml
 create mode 100644 01_FirstApp/GeoQuiz/app/src/main/res/values/dimens.xml
 create mode 100644 01_FirstApp/GeoQuiz/app/src/main/res/values/strings.xml
 create mode 100644 01_FirstApp/GeoQuiz/app/src/main/res/values/styles.xml
 create mode 100644 01_FirstApp/GeoQuiz/app/src/test/java/com/bignerdranch/android/geoquiz/ExampleUnitTest.java
 create mode 100644 01_FirstApp/GeoQuiz/build.gradle
 create mode 100644 01_FirstApp/GeoQuiz/gradle.properties
 create mode 100644 01_FirstApp/GeoQuiz/gradle/wrapper/gradle-wrapper.jar
 create mode 100644 01_FirstApp/GeoQuiz/gradle/wrapper/gradle-wrapper.properties
 create mode 100644 01_FirstApp/GeoQuiz/gradlew
 create mode 100644 01_FirstApp/GeoQuiz/gradlew.bat
 create mode 100644 01_FirstApp/GeoQuiz/settings.gradle
 create mode 100644 01_FirstApp/LICENSE.txt

HONOR@MosesMin-HonorMagicbook16pro2021 MINGW64 /e/DownloadFiles/BaiduNetdiskDownload/AndroidStudy/00 PDF/AndroidProgramming3eMm (master)
$ git remote add origin https://github.com/mosesmindev/AndroidProgramming3eMm.git

HONOR@MosesMin-HonorMagicbook16pro2021 MINGW64 /e/DownloadFiles/BaiduNetdiskDownload/AndroidStudy/00 PDF/AndroidProgramming3eMm (master)
$ git push -u origin master
fatal: unable to access 'https://github.com/mosesmindev/AndroidProgramming3eMm.git/': OpenSSL SSL_read: Connection was reset, errno 10054

HONOR@MosesMin-HonorMagicbook16pro2021 MINGW64 /e/DownloadFiles/BaiduNetdiskDownload/AndroidStudy/00 PDF/AndroidProgramming3eMm (master)
$ git push -u origin master
Enumerating objects: 72, done.
Counting objects: 100% (72/72), done.
Delta compression using up to 16 threads
Compressing objects: 100% (44/44), done.
Writing objects: 100% (72/72), 90.81 KiB | 12.97 MiB/s, done.
Total 72 (delta 0), reused 0 (delta 0), pack-reused 0
To https://github.com/mosesmindev/AndroidProgramming3eMm.git
 * [new branch]      master -> master
branch 'master' set up to track 'origin/master'.

HONOR@MosesMin-HonorMagicbook16pro2021 MINGW64 /e/DownloadFiles/BaiduNetdiskDownload/AndroidStudy/00 PDF/AndroidProgramming3eMm (master)
$

參考:
1、郭霖大牛的《Android第一行程式碼 第二版》
2、將本地專案上傳到github,git操作詳細指導,不看後悔深度好文!

相關文章