Git Notes
20 Apr 2019 |學Git常常會使用到cmd,故先列出常用cmd指令
常用cmd指令
- ls [-al]: 顯示檔案,a:顯示隱藏檔,l:顯示使用者使用權限、建檔日期及檔名等…
- mkdir: 建立目錄
- touch:建立檔案或是修改已存在檔案的修改時間, touch [file_Name]
- pwd:顯示目前目錄路徑
- cd: 轉換目錄
- clear:清除cmd畫面
- cp: 複製檔案, cp [file_Name] [file_Name2], 複製一份file_Name 並命名為 file_Name2
- mv: 更改檔名, mv [file_Name] [file_Name2], 將file_Name 名稱更改為 file_Name2
- rm: 移除檔案, rm [file_Name]
工作目錄、暫存區、儲存庫
再Git專案中分為三個區域:工作目錄(working directory)、暫存區(staging area)、儲存庫(repository)
- git add:將修改的檔案從工作目錄移置暫存區
- git commit:將暫存區的資料更新至儲存庫
Git指令
使用者全域設定
$ git config --global user.email "email帳號"
$ git config --global user.name "使用者名稱"
$ git config --list(查看目前設定)
使用者特定專案設定
$ git config --local user.email "LocalEmail"
$ git config --local user.name "LocalUserName"
檢查版本
$ git --version
git version 2.16.2
查看Git指令
$ git --help
usage: git [--version] [--help] [-C <path>] [-c name=value]...
抓取線上專案
$ git clone https://github.com/david8388/movieApp.git
$ git clone -b [branch_name] --single-branch [url] ## 抓取特定分支
更新本地進度
$ git pull
Already up to date.
讓Git開始對專案進行版控
$ git init
Initialized empty Git repository in /Users/wuxiancheng/SideProject/test/.git/
將變更檔案從working directory 移置 staging area
$ git add .
- git add -A、git add -all 也都可以將變更檔案移置staging area.
- git add . ,再Git版本1.x不包含刪除檔案
調整檔案名稱
$ git mv [old_filename] [new_filename]
刪除檔案
git rm [filename]
檢查工作目錄(working directory)目前狀態
$ git status
On branch master
Changes to be committed:
(use "git reset HEAD <file>..." to unstage)
renamed: newUser2.html -> renameUser.html
查看/新增目前分支
$ git branch [new_branch] # 新增分支
$ git branch
branch_1
* master
git checkout
$ git checkout [branch_name] # 切換到指定分支
$ git checkout -b [branch_name] # 切換到指定分支,若該分支不存在,則會自動建立並切換
$ git checkout [file_name] # 從repo抓取名為[file_name]的檔案,並復原檔案至working directory
$ git checkout . #將working directory所有變更檔案還原
git reset
Reset模式有三種
- mixed mode:預設參數,從commit拆出來的檔案會
留在working directory
- soft mode:從commit拆出來的檔案會
留在staging area
- hard mode:從commit拆出來的檔案會
直接移除
間單來說 git reset 類似前往某個commit紀錄
先查看目前commit 紀錄
$ git log --oneline
2119fb5 (HEAD -> master) add newFile.txt
8832137 (branch_with_b, branch_1) add newUser2.html
ff144bd RENAME
回復到某個特定commit
$ git reset 8832137 or $ git reset 2119fb5^ (這裡的意思是回復到2119fb5的前一個commit,也就是8832137)
$ git log --oneline
8832137 (HEAD -> master, branch_with_b, branch_1) add newUser2.html
ff144bd RENAME
4a340b7 remove welcome.html
發現最後一次的commit已經被拆除,預設為mixed,所有會留在working directory
回復完但又後悔想回到 2119fb5 這個commit紀錄
$ git reset --hard 2119fb5
$ git log --oneline
2119fb5 (HEAD -> master) add newFile.txt
8832137 (branch_with_b, branch_1) add newUser2.html
ff144bd RENAME
合併分支到master
$ git checkout master
$ git merge [branch_name]
衝突的分支合併
不同的分支中都修改了同一個檔案的同一部分此時就會出現錯誤訊息如下
$ git checkout master
$ git merge branch_with_b
Auto-merging newUser2.html
CONFLICT (content): Merge conflict in newUser2.html
Automatic merge failed; fix conflicts and then commit the result.
開啟衝突的檔案,Git會將衝突的內容標示出來
$ vi newUser2.html
HI, I'm new user.
<<<<<<< HEAD
It's branch_1;
=======
it's branch_with_b
>>>>>>> branch_with_b
後來決定要依branch的為主,將資料更改為
HI, I'm new user.
it's branch_with_b
接下來就將檔案新增到staging area並更新到repo
git add newUser2.html
git commit -m 'conflict fixed'
提交修改,將staging area的檔案移置repository
$ git commit -m "message"
顯示提交紀錄
$ git log
commit 68ec529fc792cbf2afcbe655e6e5f8ed91ac3c44 (HEAD -> master, origin/master)
Author: David <hsienwu830808@gmail.com>
Date: Sun Apr 21 09:44:18 2019 +0800
2019-04-20(Git Notes), to be continued
...
顯示簡略commit資訊
$ git log --oneline
68ec529 (HEAD -> master, origin/master) 2019-04-20(Git Notes), to be continued
1b9f785 update: 2019-04-13(Phabricator Workflow) git script
4013580 update: 2019-04-13(Phbricator-Workflow)
...
參考資料:
Comments