GitHub 配置及簡單使用

一. 初始化

1. 初始化 git 目錄

終端中進入到項目文件夾,然後輸入以下命令。

git init

命令執行後,文件夾中會多一個.git 文件夾


2. 設置本機關聯 GItHub 的用戶名和郵箱

git config --global user.name "<username>"
git config --global user.email "<email>"

示例:

git config --global user.name "Christy"
git config --global user.email "[email protected]"


3. 將項目文件夾中的文件都添加入staging area

git add .


3.1 查看哪些文件在staging area 中

如果你未來不想哪些文件或文件類型出現在staging area中,可以將他們添加到 .gitignore 文件中。

git status


4. commit staging area中的變化

git commit -m "<message>"

示例:

git commit -m "Initial commit"


4.1 查看commit 信息。

git log


5. 在GitHub 官方網站中創建項目相關目錄

在 Repositories 標籤下點擊 New 按鈕創建:


6. 添加 GitHub 作爲你的主要 branch 的 origin

git remote add origin [email protected]:<username>/<directory name>

此處[email protected]:<username>/<directory name>是你上一步創建的目錄的鏈接地址。直接複製粘貼即可。


7. 將本地目錄 push 到GitHub

git push origin master
此處會要求你輸入 GitHub 用戶名密碼。Push 成功後你在 GitHub 網站相應目錄下就能看到本地項目目錄中的內容了。

二. 簡單的 branch

1. 創建一個 branch

git checkout -b <branchname>


1.1 查看有哪些 branch

git branch

2. 在新建的 branch 下修改一些文件,然後添加到 staging area 並且 commit

git add *

git commit -m "<message>"

3. 回到 master branch

git checkout master


4. merge 第2步在新 branch 下做的修改到 master branch

git merge <branchname>

5. 刪除新的 branch

此時 master branch 中已經有新 branch 下的修改。可以刪除新的 branch, 當然也可以留着。如果要刪除的話,命令行如下:

git branch -d <branchname>

6. 將變化 push 到GitHub

git push






發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章