一.安裝GIT和配置GIT
1.安裝GIT
apt-get install git
2.配置GIT
##配置用戶信息
git config --global user.name "John Doe"
git config --global user.email johndoe@example.com
##文本編輯器
git config --global core.editor emacs
##差異分析工具
git config --global merge.tool vimdiff
##查看配置信息
git config --list
##獲取幫助
git help config
二、創建GIT倉庫和遠程倉庫的使用
1.在工作目錄中初始化新倉庫
##切換到工作目錄
mkdir test
cd test
git init
##初始化后可以看到這些文件
ls ./.git/
branches config description HEAD hooks info objects refs
2.從現有倉庫克隆出來
git clone git://192.168.1.1/var/www/test.git
3.克隆到本地
git clone /var/www/test test_new
4.遠程倉庫的克隆
git clone root@192.168.1.1:/var/www/test
5.查看當前的遠程庫
git remote -v
6.添加遠程倉庫和推送
##添加遠程倉庫分支
git remote add test root@192.168.1.1:/var/www/test
##從遠程倉庫抓取數據
git fetch test
##推送數據到遠程倉庫
git push origin master
##查看遠程倉庫信息
git remote show origin
7.遠程倉庫的刪除和重命名
##重命名
git remote rename test test_new
##刪除
git remote rm paul
三、GIT全局配置
1.配置當前用戶名和郵箱
git config --global user.name "linzhenjie"
git config --global user.email linzhenjie@live.com
2.設置別名
git config --global alias.ci commit
git config --global alias.st status
3.其他配置
##顏色顯示
git config --global color.ui true
##編輯器
git config --global core.editor vim
##獨立忽略文件
git config --global core.excludesfile /home/linzhenjie/.gitignore
四、GIT中相關命令
1.檢查當前文件狀態
git status
2.往暫存庫中添加新的文件
git add test.php
3.提交更新
##提交更新
git commit -m "add test file for my test"
##添加並提交更新
git commit -a -m 'added new benchmarks'
##執行一次空白提交
git commit --allow-empty -m "who does commit?"
4.比較差異
##暫存庫與版本庫比較
git diff
##本地庫與暫存庫比較
git diff HEAD
##暫存庫與版本庫比較
git diff --cached
git diff --staged
5.修改最后一次提交
git commit -m 'initial commit'
git add test.php
git commit --amend
6. 查看提交歷史
##查看所有日志
git log
##查看所有日志(包含重置的日志)
git reflog show master
7.重置/回退暫存區和版本庫
##重置/回退版本庫
git reset --soft
##重置/回退版本庫、暫存庫
git reset
##重置/回退版本庫、暫存區、工作區
git reset --hard
8.清理工作區
##查看不在暫存區的工作區文件
git clean -nd
##清理工作區多余文件
git clean –fd
9.刪除暫存區和版本庫
##刪除暫存庫和版本庫的文件
git rm test.php
##刪除版本庫的文件
$ git rm --cached test.php
10.移動文件
git mv test.php test_new.php
11.進度的存儲和恢復
##保存當前進度
git stash save
##查看當前進度列表
git stash list
##彈出恢復工作區進度
git stash pop
##彈出恢復工作區和暫存區進度
git stash pop --index
##應用工作區進度
git stash apply
##刪除一個進度
git stash drop
##刪除所有存儲進度
git stash clear
##存儲分支進度
git stash branch
五、忽略文件語法
.gitignore
*.a ##忽略以.a為節結尾的文件
!lib.a ##不會忽略lib.a的文件或目錄
/DIR ##忽略當前目錄下文件(不包括子目錄)
DIR/ ##忽略當前目錄下所有文件
DIR/*.txt ##忽略DIR下的txt文件(不包括子目錄)