Git Pro讀書筆記(二)安裝和基礎操作 //Windows和Ubuntu

1.安裝


Windows版本下載地址:http://git-scm.com/download/win


Ubuntu版本,必要時加個sudo

# apt-get install git

2.初始配置

用戶名郵箱啥的,配置一次

git config --global user.name "Zhu Mingde"
git config --global user.email [email protected]

可以查看配置結果

git config --list


當然也可以查看指定的配置信息

PS C:\WINDOWS\system32> git config user.name
Zhu Mingde
PS C:\WINDOWS\system32> git config user.email
[email protected]


Ubuntu上效果


3.獲取幫助文檔

例如獲取config幫助的指令可以鍵入

git help config
或者

git config --help

4.現有目錄導入所有文件到Git

【Windows版】

轉到目錄下//注意,cd到目錄中有空格的時候需要用引號引起來……

cd "C:\Users\zmdsj\Documents\Visual Studio 2015\Projects\opencvtest"
然後初始化

git init
接着添加文件add以及提交commit

git add *.cpp
git commit -m 'initial project version'
效果如圖:(沒有LICENSE...)

現在已經有了一個倉庫。

【Ubuntu版】

操作一樣


5.克隆代碼倉

git clone  地址  (名字,可省略)

git clone https://github.com/libgit2/libgit2 Test_zmd

【Windows版】


【Ubuntu】



6.記錄每次更新到倉庫

查看狀態信息

git status

增添一個新的追蹤文件

git add README

查看狀態的縮寫

git status -s



忽略文件

創建一個ignore文件,忽略所有帶~的文件以及所有以oa結尾的文件

//修改.gitignore文件裏的內容就好

cat .gitignore
*.[oa]
*~
規範

所有空行或者以 # 開頭的行都會被 Git 忽略。

可以使用標準的 glob 模式匹配。

匹配模式可以以(/)開頭防止遞歸。

匹配模式可以以(/)結尾指定目錄。

要忽略指定模式以外的文件或目錄,可以在模式前加上驚歎號(!)取反。
舉個栗子

# no .a files
*.a

# but do track lib.a, even though you're ignoring .a files above
!lib.a

# only ignore the TODO file in the current directory, not subdir/TODO
/TODO

# ignore all files in the build/ directory
build/

# ignore doc/notes.txt, but not doc/server/arch.txt
doc/*.txt

# ignore all .pdf files in the doc/ directory
doc/**/*.pdf



更爲具體的見項目


查看修改//已暫存和未暫存的

git diff指令查看哪些更新還沒存起來……

git diff


查看已經存起來但是沒提交的

git diff --cached

或者git diff --staged



提交更新

git commit

git commit


跳過使用暫存區域//避免繁瑣的add

自動把所有已經跟蹤過的文件暫存起來一併提交,從而跳過 git add 步驟
git commit -a



移除文件

git rm XXXX
如果刪除之前修改過並放入緩存區的,需要加-f強制刪除……


從Git裏刪除但本地保留……

加管檢測--cached ....

git rm --cached README


git rm後面可以使用glob模式


移動文件、重命名

git mv file_from file_to

本質上相當於//mv操作,然後刪除,再添加,合起來就是git mv

 mv README.md README
 git rm README.md
 git add README


7.查看歷史


查看提交歷史

git log


-p只顯示差異部分  -2只顯示兩次

git log -p -2

更多詳細信息 : 地址

8.撤銷


撤銷操作

重新上傳

git commit --amend

取消暫存

git reset HEAD <file>... 
撤銷對文件的修改

 (use "git checkout -- <file>..." to discard changes in working directory)
例子:
$ git checkout -- CONTRIBUTING.md
$ git status
On branch master
Changes to be committed:
  (use "git reset HEAD <file>..." to unstage)

    renamed:    README.md -> README


9.遠程倉庫

查看
git remote -v
添加

git remote add pb https://github.com/paulboone/ticgit


從遠程倉庫抓取(例如剛剛添加的pb

git fetch [remote-name]


推送到倉庫

git push [remote-name] [branch-name]

查看遠程倉庫

git remote show [remote-name]


遠程倉庫的移除與重命名

git remote rename XX YY

10.打標籤

列出標籤
git tag

附註標籤
-a 輸入版本號  -m 名字

$ git tag -a v1.4 -m 'my version 1.4'
$ git tag
v0.1
v1.3
v1.4
顯示版本

git show v1.4

輕量標籤

$ git tag v1.4-lw
$ git tag
v0.1
v1.3
v1.4
v1.4-lw
v1.5

後期打標籤//後面補上校驗碼

git tag -a v1.2 9fceb02


共享標籤

//push到服務器,標籤不會弄上去

git push origin v1.5

批量

 git push origin --tags

檢出標籤

git checkout -b [branchname] [tagname]


這部分看得有點浮光掠影,要用的時候還是要看,地址


11. Git別名

好方便,相當於快捷鍵,233

設置方式

git config --global alias.ci commit



先吃晚飯去了,233



發佈了218 篇原創文章 · 獲贊 482 · 訪問量 109萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章