Basic Git Commands

Set up Git config path

首先設定好自己的名字與信箱

1
2
$ git config --global user.name "Your name"
$ git config --global user.email "Your email"

確認輸入是否成功

1
2
$ git config --global user.name
$ git config --global user.email

查看 git config 設定

1
$ git config --list

Importing project

Creat a project file

建立專案資料夾

1
$ mkdir project

移動至 project 資料夾

1
$ cd project

Creat a repository

建立本地端初始化 git repository

1
$ git init

Clone GitHub repository

從 GitHub 下載數據庫 remote repo 至目前工作區

1
$ git clone SSH/HTTPS

Listing the contents of file information

列出 project file 的檔案和資料夾 -l: 列出詳細資料 -a: 列出隱藏資料夾

1
$ ls -la

Git add staging area

檢查目前本地端所有檔案的狀態

1
$ git status

將更新檔案建立索引 版本控制

1
2
$ git add .                 // project下的所有檔案
$ git add helloworld.txt // 指定project下的單一檔案

Git commit to local repository

將建立索引的更新檔案 commit 到 local repo

1
$ git commit -m "summary"

Git push to GitHub

加入遠端數據庫 git remote

1
$ git remote add origin url

將 local repo commit 發佈到 GitHub git push

1
$ git push -u origin branch

View project history

查看之前的 commit 記錄 版本資訊

q 退出

1
2
$ git log       // 作者、時間、commit 內容
$ git reflog // commit 內容

查看目前檔案與上次 commit 的差異

1
$ git diff commitID

Set up SSH key on your machine

在 terminal 執行 SSH 時發生如下錯誤 :

1
2
3
4
5
Permissiondenied (publickey).
fatal:Could not read from remote repository.

Pleasemake sure you have the correct access rights
and the repository exists.

Creat SSH key

過程一直按 enter 即可,最後便會生成你的 ssh key

1
$ ssh-keygen -t rsa -C "Your email"

Set up SSH key

1
2
3
4
$ ssh -v git@github.com
$ ssh-agent -s
$ eval `ssh-agent -s`
$ ssh-add ~/.ssh/id_rsa

將產生的 id_rsa.pub 複製至 github setting -> SSH and GPG keys -> new SSH key

Authenticate SSH key

在 GitHub 上設定完成後,在 terminal 驗證 ssh key

1
$ ssh -T git@github.com

驗證成功

1
Hi username! You’ve successfully authenticated, but GitHub does not provide shell access.