歡迎來到Linux教程網
Linux教程網
Linux教程網
Linux教程網
Linux教程網 >> Linux基礎 >> 關於Linux >> Git的一些細節

Git的一些細節

日期:2017/3/3 16:12:14   编辑:關於Linux

一、對於任何一個文件,在Git 內都只有三種狀態:已提交(committed),已修改(modified)和已暫存(staged)。已提交表示該文件已經被安全地保存在本地數據庫中了;已修改表示修改了某個文件,但還沒有提交保存;已暫存表示把已修改的文件放在下次提交時要保存的清單中。

二、Git工作變量的設置

1、/etc/gitconfig 文件:系統中對所有用戶都普遍適用的配置。若使用git config 時用--system 選項,讀寫的就是這個文件。

2、~/.gitconfig 文件:用戶目錄下的配置文件只適用於該用戶。若使用git config 時用--global 選項,讀寫的就是這個文件。

3、當前項目的git 目錄中的配置文件(也就是工作目錄中的.git/config 文件):這裡的配置僅僅針對當前項目有效。每一個級別的配置都會覆蓋上層的相同配置,所以.git/config 裡的配置會覆蓋/etc/gitconfig 中的同名變量。

#用戶信息
git config --global user.name "John Doe"
git config --global user.email [email protected]
#默認使用的文本編輯器
git config --global core.editor vim
#差異分析工具
git config --global merge.tool vimdiff

三、提交,只有在Changes to be committed下的文件才會被提交。

git status
# On branch master
# Your branch is ahead of 'origin/master' by 3 commits.
#
# Changes to be committed:
#   (use "git reset HEAD <file>..." to unstage)
#
#   modified:   a.txt
#
# Changes not staged for commit:
#   (use "git add <file>..." to update what will be committed)
#   (use "git checkout -- <file>..." to discard changes in working directory)
#
#   modified:   a.txt

跳過暫存區使用:git commit -a -m 'reason...'

四、git diff

git diff:此命令比較的是工作目錄中當前文件和暫存區域快照之間的差異,也就是修改之後還沒有暫存起來的變化內容。

若要看已經暫存起來的文件和上次提交時的快照之間的差異,可以用git diff --cached

五、移除文件

git rm :刪除文件,同時刪除跟蹤

git rm --cached filename :刪除跟蹤,但保留本地文件。

六、察看提交記錄

git log -p:察看細節差異

git log -2:記錄條數

git log --stat:顯示簡要的增改行數統計

選項說明
-(n) 僅顯示最近的n 條提交
--since, --after 僅顯示指定時間之後的提交。
--until, --before 僅顯示指定時間之前的提交。
--author 僅顯示指定作者相關的提交。
--committer 僅顯示指定提交者相關的提交。

git log --pretty=format:"%h - %an, %ar : %s" 設置展示格式

%H 提交對象(commit)的完整哈希字串
%h 提交對象的簡短哈希字串
%T 樹對象(tree)的完整哈希字串
%t 樹對象的簡短哈希字串
%P 父對象(parent)的完整哈希字串
%p 父對象的簡短哈希字串
%an 作者(author)的名字
%ae 作者的電子郵件地址
%ad 作者修訂日期(可以用-date= 選項定制格式)
%ar 作者修訂日期,按多久以前的方式顯示
%cn 提交者(committer)的名字
%ce 提交者的電子郵件地址
%cd 提交日期
%cr 提交日期,按多久以前的方式顯示
%s 提交說明

七、回滾

1、git reset HEAD filename : 撤銷暫存區的文件

2、git checkout -- filename : 撤銷本地修改

八、遠程倉庫

察看有哪些遠程倉庫: git remote -v

添加遠程倉庫:git remote add pb git://github.com/paulboone/ticgit.git

從遠程倉庫抓取數據:git fetch pb

如果設置了某個分支用於跟蹤某個遠端倉庫的分支,可以使用git pull 命令自動抓取數據下來,然後將遠端分支自動合並到本地倉庫中當前分支.

推送數據到遠程倉庫:git push origin master

查看遠程倉庫信息

$ git remote show origin
* remote origin
URL: [email protected]:defunkt/github.git
#運行git pull 時將自動合並哪些分支
Remote branch merged with 'git pull' while on branch issues
issues
#運行git pull 時將自動合並哪些分支
Remote branch merged with 'git pull' while on branch master
master
#有哪些遠端分支還沒有同步到本地
New remote branches (next fetch will store in remotes/origin)
caching
#已同步到本地的遠端分支在遠端服務器上已被刪除
Stale tracking branches (use 'git remote prune')
libwalker
walker2
Tracked remote branches
acl
apiv2
dashboard2
issues
master
postgres
#運行git push 時缺省推送的分支是什麼
Local branch pushed with 'git push'
master:master

九、打標簽 輕量級的(lightweight)和含附注的(annotated)

git tag

git tag -l 'v1.4.2.*' 列出特定標簽

含附注的標簽 git tag -a v0.8.0 -m 'my tag v0.8.0'

察看標簽:git show v0.8.0

補打標簽:git tag -a v1.2 9fceb02(校驗數)

分享標簽:git push origin --tags/v0.8.0

十、使用技巧

1、自動完成

下載Git 的源

代碼,進入contrib/completion 目錄,會看到一個git-completion.bash 文件。將此文件復制到你自己的用戶主目錄中(譯注:按照下面的示例,還應改名加上點:cp gitcompletion.

bash ~/.git-completion.bash),並把下面一行內容添加到你的.bashrc 文件中:

source ~/.git-completion.bash

在輸入Git 命令的時候可以敲兩次跳格鍵(Tab),就會看到列出所有匹配的可用命令建議:

$ git co<tab><tab>

commit config

2、Git 命令別名

$ git config --global alias.co checkout
$ git config --global alias.br branch
$ git config --global alias.ci commit
$ git config --global alias.st status

作者:51cto博客 phper-每天一點點

 

Copyright © Linux教程網 All Rights Reserved