###### tags: `Coding` # Git指令 ## 基本四步驟 1. git init 在 Workspace 創建 .git 目錄 2. git add 建立索引 3. git commit 至本地資料庫 (Local Repository) 4. git push 至 GitHub 資料庫 (Remote Repository) <img src = 'https://i.imgur.com/YO46vuV.png'> --- ## 1.創建Workspace 以及.git 目錄 **step 1** 在於目前工作區域建立一個git folder ``` $ git init ``` **step 2** 設定git config環境,用以讓本地端知道要連接到哪一個帳戶 ``` $ git config --global user.name "Max" #設定使用者名稱 $ git config --global user.email "user@gmail.com” #設定使用者信箱 ``` **step 3** 如果有需要從現有的git上下載repo至本地端則可使用下列 ``` case 1: $ git clone <repo URL> #未指定branch case 2: $ git clone <repo URL> -b <branch name> #指定特定branch case 3: $ git clone <repo URL> <folder name/path> #指定特定檔案 ``` --- ## 2.git add 建立索引 **step 1** 建立索引 ``` $ git add helloword.txt #指定單一檔案建立索引 $ git add . #工作區域內所有檔案建立索引 ``` --- ## 3.git commit 至本地資料庫 **step 1** 將已經建立索引的資料commit到本地端資料庫(Local Repository) ``` $ git commit -m "add helloword.txt" ``` --- ## 4.git push 至 GitHub **Step 1** 將檔案remote到github ``` $ git remote add origin <remote 網址> $ git remote set-url origin // for reset url for remote ``` **step 2** 將檔案push至GitHub上面 ``` $ git push #若git目錄內沒有其他分支,可直接使用push $ git push --all origin #上傳所有分支到git上面 $ git push origin master #上傳指定分支到git上面 ``` ``` 直接使用$ git push -u origin master,可將master push到github上,其意思為 $ git push -u <remote name> <branch name>,可拆解為: $ git push origin master #將master push到origin $ git checkout master #移動到master branch $ git branch -u origin/master #查看origin/master分支 ``` --- ## Git操作紀錄查看相關指令 **查看 Git config 設定** ``` $ git config --list ``` **查看目前WorkSpace的commit紀錄** ``` $ git log # 列出詳細的作者、時間、commit 內容 $ git reflog # 列出 commit 內容 ``` **查看目前WS內所有檔案狀況** ``` $ git status ``` **查看目前 Remote 連線位置** ``` $ git remote -v ``` **版本切換** ``` $ git checkout <版本號> <省略 or 特定文件名稱> ``` ----- ## 分支相關指令 **分支建立** ``` $ git branch <Name> ``` **切換分支** ``` $ git checkout <Name> $ git checkout -b <Name> # 同時建立且切換分支 ``` **查看目前本地所有分支** ``` $ git branch # 查看本地所有分支 $ git branch -a # 查看所有分支(含遠端分支) ``` **刪除分支** ``` $ git branch -d <name> ``` **合併分支** ``` $ git checkout master # 先切換到主分支 $ git merge <副分支> # fast-forward $ git merge <副分支> --no-ff # no-fast-forward $ git merge --abort # 恢復 Merge 之前 ``` --- ## git ignore ``` touch .gitignore //創建ignore檔案 ``` 編輯檔案內容: ``` # 檔案名稱 .gitignore # 忽略 secret.yml 檔案 secret.yml # 忽略 config 目錄下的 database.yml 檔案 config/database.yml # 忽略所有 db 目錄下附檔名是 .sqlite3 的檔案 /db/*.sqlite3 # 忽略所有附檔名是 .tmp 的檔案 *.tmp # 當然你要忽略自己也可以,只是通常不會這麼做 # .gitignore ``` 忽視ignore: ``` $ git add -f 檔案名稱 ``` 清除忽略的檔案: ``` $ git clean -fX //-f指強制刪除 ``` ## Git版本更新後注意事項 事項1 :  linux上的git版本更新後,無法直接使用帳密登入,需要得到token,解決方法其一(較保險)的方法為,使用token登入,在使用者的setting下會有一個Develop setting,內部的Personal access token 可以生成token,並將其輸入在原本的密碼處。 事項2:  如果遇到Git fatal: protocol 'https' is not supported,則代表複製的網址應該有問題,可能是前面有隱藏符號,因此叫好解法為直接用手打,而不要直接使用複製貼上。 事項3:  想刪除local的.git檔案,則使用:
×
Sign in
Email
Password
Forgot password
or
By clicking below, you agree to our
terms of service
.
Sign in via Facebook
Sign in via Twitter
Sign in via GitHub
Sign in via Dropbox
Sign in with Wallet
Wallet (
)
Connect another wallet
New to HackMD?
Sign up