[Git] Basic Command

  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
# 設定個人資訊 (這些資訊會顯示在commit log上)
git config --global username "your name"
git config --global email "your E-mail"

#------------------------------[init]-----------------------------------
# 切換到自己的專案目錄
cd my_project

# 管理初始化
git init


#------------------------------[add]----------------------------------
# 新增檔案
git add (file name)

# 將當前目錄下的所有檔案及目錄加入管理
git add .

# 將指定子目錄下的指定檔案加入管理
git add dir_name/file_name

# 將指定子目錄下的所有檔案加入管理
git add dir_name/.

# 新增修改過的檔案 (兩者皆可)
git add -u
git add modifyfile


#-------------------------------[commit]----------------------------------
# commit並添加註解
git commit -m "commit message"

# 自動add修改過的檔案(不包含未納管的檔案)並commit (會開啟editor,需要寫commit massage)
git commit -a

# 同上,差異在於commit massage直接寫在-m後面,並不會開啟editor
git commit -a -m "commit message"

# -v 可以看到檔案哪些內容有被更改
git commit -a -v


#-------------------------------[branch]----------------------------------
# 列出現有branch
git branch

# 列出所有branch
git branch -a

# 新增branch
git branch (branch name)

# 從master新增branch
git branch test-branch master

# 刪除branch
git branch -d (branch name)

# 強制刪除branch
git branch -D (branch name)

# 重新命名branch (當前branch為old branch時可忽略參數old branch name)
git branch -m (old branch name) (new branch name)

# 切換至指定branch
git checkout (branch name)

# 將指定的branch與當前branch合併
git merge (branch name)

#-------------[push、pull、clone、remote(以girhub為例)]-------------
# 提交變更至git server
git push [email protected]:(user name)/(project name).git

# 提交指定branch的變更
git push [email protected]:(user name)/(project name).git (branch name)

# 刪除git server上的branch(下列兩種方式皆可)
git push [email protected]:(user name)/(project name) .git :(branch name)
git push origin --delete (branch name)

# 檢查git server有無較新的變更,並拉下來更新本機的檔案(未指定remote+branch name則對目前branch進行作業)
git pull (remote) (branch name)

# 將該專案資料夾從git server拉下來
git clone [email protected]:(user name)/(project name).git

# 新增遠端名稱
git remote (remote name) [email protected]:(user name)/(project name).git

# 顯示現有remote清單
git remote -v

# 將project上傳到指定的remote
git push (remote name)

# 將project裡指定branch的上傳到指定的remote
git push (remote name) (branch name)
example:
git remote origin [email protected]:athlon20147/myproject.git //remote name : origin
新增好remote後,以下兩行功能相等
git push [email protected]:athlon20147/myproject.git
git push origin

-------------------------------[other]----------------------------------
# 檢視變更紀錄
git log

# 還原已經commit刪除的檔案
git checkout <deleteing_commit>^ -- <file_path>
ex: git checkout b05128e3ebd4f38c317f066d679aee21c7d3af65^ -- config/ldap-sample.php
Licensed under CC BY-NC-SA 3.0 TW
comments powered by Disqus