這7個常用Git命令或概念你都知道嗎?
本文轉載自公眾號“讀芯術”(ID:AI_Discovery)。
1. 以“; ”(分號)分隔的多個git命令
以分號分隔可使用多個Git命令:
- $ git --version; git branch // separated by semi-colon
2. git別名命令
命令別名能提高可重用性。
將gitremote -v(用于檢查遠程源的命令)別名為show-origin:
- $ git config --global alias.show-origin remote -v
- $ git show-origin
將git log --graph--pretty=oneline (打印提交日志的命令)別名為show-graph:
3. 名為‘ .git’的隱藏文件夾
隱藏的.git文件夾包含提交、分支和文件的歷史記錄。因此,需要復制.git 文件夾并復制整個應用程序及git歷史記錄(提交歷史等)。要復制隱藏的文件夾,需要運行-r 選項:
- $ cp -r <originalFolder><destinationFolder>
要查看隱藏的文件夾,需要運行ls-a 而不是ls:
- $ls -a
4. 在‘.gitignore’中指定文件夾和文件的多種方法
.gitignore配置文件包含你不想在git系統中管理的文件和文件夾。在.gitignore中指定文件夾和文件的方法有很多種。首先,.gitignore中的#用于注釋(類似于Python中的#注釋)。
(1) 簡單文件名
- # exclude dbinfo.php file in git system
- dbinfo.php
(2) 模式匹配——“globbing”使用星號(*)
globbing是大多數Unix shell使用的通配符技術:
- # exclude obj files
- *.obj
(3) 指定不想排除的文件或文件夾
可以指定不想排除的文件或文件夾。當與團隊合作并希望重新聲明該文件或文件夾不應從git系統中排除時,這是很有用的做法:
- # do not exclude the following configuration file
- !config.php
(4) 文件相對路徑
- # exclude the file in the current directory
- /readme.txt# exclude all files in /pub/ directory
- /pub/# exclude all txt files whose parent is docdoc/**/*.txt
5. 空白信息選項
有時想在沒有信息的情況下進行提交,而信息卻又是提交的必要條件,可以使用--allow-empty-message選項:
- $ git commit --allow-empty-mesage -m "" --- no commit message
6. 分支層次結構
當分支中帶有斜杠(/)時,該分支將存儲為目錄層次結構:
- Branch name
- --> v1.5/feature-1
- --> v1.5/fix-1--> v1.6/feature-2
- --> v1.6/feature-3
- --> v1.6/fix-1

7. 顯示日志
顯示git日志有多種方法:
(1) pretty=online選項
- $ git log --pretty=onelineOR$ git log --oneline
(2) 圖表選項
- $ git log --online --graph
(3) 數字選項
- $ git log --online --graph -5 // SHOW only 5 most recent commits
Git使用愉快!