簡介
- Git是一款免費、開源的分散式版本控制系統,用於敏捷高效地處理任何或小或大的專案
- Git是一個開源的分散式版本控制系統,可以有效、高速的處理從很小到非常大的專案版本管
- Git 是 Linus Torvalds 為了幫助管理 Linux核心開發而開發的一個開放原始碼的版本控制軟體
git常用命令
常用命令列表
git help
usage: git [--version] [--help] [-C <path>] [-c name=value]
[--exec-path[=<path>]] [--html-path] [--man-path] [--info-path]
[-p|--paginate|--no-pager] [--no-replace-objects] [--bare]
[--git-dir=<path>] [--work-tree=<path>] [--namespace=<name>]
<command> [<args>]
The most commonly used git commands are:
add Add file contents to the index
bisect Find by binary search the change that introduced a bug
branch List, create, or delete branches
checkout Checkout a branch or paths to the working tree
clone Clone a repository into a new directory
commit Record changes to the repository
diff Show changes between commits, commit and working tree, etc
fetch Download objects and refs from another repository
grep Print lines matching a pattern
init Create an empty Git repository or reinitialize an existing one
log Show commit logs
merge Join two or more development histories together
mv Move or rename a file, a directory, or a symlink
pull Fetch from and integrate with another repository or a local branch
push Update remote refs along with associated objects
rebase Forward-port local commits to the updated upstream head
reset Reset current HEAD to the specified state
rm Remove files from the working tree and from the index
show Show various types of objects
status Show the working tree status
tag Create, list, delete or verify a tag object signed with GPG
複製程式碼
以上是通過githelp命令得出的所有常用的git操作符
命令詳解
add
- git add . 提交新檔案(new)和被修改(modified)檔案,不包括被刪除(deleted)檔案
- git add -A 提交所有變化
- git add -u 提交被修改(modified)和被刪除(deleted)檔案,不包括新檔案(new)
- git add -p 提交分散式,每次提交會提示是否提交
git add .
git status
# Changes to be committed:
# new file: add-me
# modified: change-me
# Changed but not updated:
# deleted: delete-me
git reset
git add -u
git status
# Changes to be committed:
# modified: change-me
# deleted: delete-me
# Untracked files:
# add-me
git reset
git add -A
git status
# Changes to be committed:
# new file: add-me
# modified: change-me
# deleted: delete-me
複製程式碼
bisect
Git-bisect 用於通過二分查詢來查尋版本的錯誤,一般用於大工程 詳解
設定前後兩個版本,一個為good, 一個為bad, 使用二分查詢中間的版本,進行編譯,看是否出現問題,如果沒有,在該版本與之前設定的bad之間再進行二分;如果有錯誤,則在該版本與之前設定的good之間進行二分,分別設定了兩個版本,一個good, 一個bad, 只要有一個good和一個bad設定完成,就出輸出可能的中間版本,你可以將該版本checkout, 編譯看是否有bug
$ git bisect start
$ git bisect bad # Current version is bad
$ git bisect good v2.6.13-rc2 # v2.6.13-rc2 was the last version
# tested that was good
複製程式碼
branch
Git-branch 用於檢視該工程存在的分支以及對分支進行操作的命令符
- git branch 檢視本地分支
- git branch -a 檢視遠端分支與本地分支,會先列出本地分支
- git branch -r 檢視遠端分支
- git branch -d [branchName] 刪除某個本地分支
- git branch -d -r [branchName] 刪除某個遠端分支
- git branch -m oldbranch newbranch 重新命名本地的某個分支
- git branch -v 檢視各個分支最後一次提交
- git branch –-merged 檢視哪些分支合併入當前分支
- git branch –-no-merged 檢視哪些分支未合併入當前分支
$ git branch
dev
lyxy-snapshot-1.0.0
master
release
wcxy-snapshot-1.0.0
* zhbx-snapshot-1.0.0
$ git branch -a
dev
lyxy-snapshot-1.0.0
master
release
wcxy-snapshot-1.0.0
* zhbx-snapshot-1.0.0
remotes/origin/HEAD -> origin/master
remotes/origin/cyfw-snapshot-1.0.0
remotes/origin/dev
remotes/origin/lyxy-snapshot-1.0.0
remotes/origin/master
remotes/origin/release
$ git branch -r
origin/HEAD -> origin/master
origin/cyfw-snapshot-1.0.0
origin/dev
origin/lyxy-snapshot-1.0.0
origin/master
origin/release
$ git branch -v
dev 41400c0 [ahead 1] <x>修改BUG.
lyxy-snapshot-1.0.0 c60ee6b Merge branch 'release'
master 8385d99 [behind 18] 修改介面中引數沒有加上company_id的SQL,去掉專案中多餘檔案
release 54cac55 [behind 9] +(個人中心學習路徑完成與未完成數量的統計)
wcxy-snapshot-1.0.0 afefba1 [ahead 1] <+>中傳網路學院的定製化修改
* zhbx-snapshot-1.0.0 a6f5cfd +(使用者模組:課件完成百分比配置)
複製程式碼
checkout
git checkout 最為常用的兩種情形是建立分支和切換分支
- git checkout [branch] 直接切換到指定分支,分支本身已存在的情況
- git checkout -b branch-name origin/branch-name 從遠端分支上拉下分支並切換到該分支
- git checkout -b branch-name TagName 以某標籤點為基礎建立新分支
- git checkout -b branchName 建立新分支,並切換到該分支。等於一次執行兩條命令。建立新分支:git branch branchName,切換到新分支:git checkout branchName
- git checkout . 放棄當前修改回退到當前分支的最後一次提交
//基於TAG v8.4.0 開出新分支並切換到該分支
$ git checkout -b tzcpa-snapshot-1.0.0 v8.4.0
Switched to a new branch 'tzcpa-snapshot-1.0.0'
複製程式碼
clone
git clone 用於克隆程式碼伺服器上的程式碼到本地
- git clone xxx.git 最原始的命令直接克隆程式碼伺服器上最新的master分支程式碼
- git clone -b [new_branch_name] xxx.git clone時建立新的分支替代預設Origin HEAD(master)
- git clone xxx.git "directName" clone到指定目錄
//克隆時候出現以下程式碼表示clone成功
$ git clone ssh://xxxx/2b/Android/mooc-Android-client
Cloning into 'mooc-Android-client'...
remote: Counting objects: 3516, done
remote: Finding sources: 100% (3516/3516)
remote: Total 3516 (delta 1041), reused 3514 (delta 1041)
Receiving objects: 100% (3516/3516), 29.12 MiB | 1.12 MiB/s, done.
Resolving deltas: 100% (1041/1041), done.
Checking connectivity... done.
Checking out files: 100% (3161/3161), done.
複製程式碼
commit
git commit 提交工作空間的修改內容到本地倉庫中.需與git add聯合使用
- git commit 提交暫存區的程式碼到本地倉庫,回車後輸入註釋然後再回車,用於註釋多餘一行的提交
- git commit -m 'xxx' 提交暫存區的程式碼到本地倉庫,m後面用引號,裡面填寫註釋
diff
git diff 比較當前版本與對應的遠端分支的區別
- git diff 此命令比較的是工作目錄(Working tree)和暫存區域快照(index)之間的差異 也就是修改之後還沒有暫存起來的變化內容。
- git diff --stat 顯示簡要的比較結果,只會顯示出具體的增減行數,不會具體到程式碼行
- git diff [branchName] 比較當前修改與另一個指定分支間的改動
- git diff [branchName] -- [filePath] 比較當前分支的某個檔案與指定分支的這個檔案的改動
- git diff [branchName] [branchName] 比較兩個分支間的改動,可用head^ head代替前一個版本和當前版本
- git diff [SHA1] [SHA2] 比較兩個提交點間的改動,sha表示提交的changeId
- git diff [branchName]...[branchName] 比較兩個分支所有的改動
-- ps:所有的git diff模式都可以通過輸入大寫字母Q退出
//列舉出從tzcpa-snapshot-1.0.0到mater分支所有改動的簡要模式
$ git diff tzcpa-snapshot-1.0.0...master --stat
filters/dev/system-config.properties | 32 +-
.../com/zxy/mobile/base/constant/Constants.java | 3 +
.../mobile/base/constant/SystemConfigConstant.java | 5 +-
.../mobile/base/service/impl/BaseServiceImpl.java | 11 +
.../com/zxy/mobile/foundation/util/StringUtil.java | 6 +-
.../mobile/listener/SaveLoginRecordListener.java | 36 +-
.../service/ask/service/impl/AskServiceImpl.java | 15 +-
.../service/impl/CommunityServiceImpl.java | 8 +-
.../community/service/impl/TrendsServiceImpl.java | 16 +-
.../course/service/impl/CourseServiceImpl.java | 4 +-
//列舉出從tzcpa-snapshot-1.0.0到mater分支所有改動,就會列舉出詳細的區別,+表示新增內容,-表示刪減的內容
$ git diff tzcpa-snapshot-1.0.0...master
diff --git a/filters/dev/system-config.properties b/filters/dev/system-config.properties
index 3333d2a..0d4efeb 100644s/CommonUtils.java | 117 +-
--- a/filters/dev/system-config.properties.java | 2 +-
+++ b/filters/dev/system-config.propertiesss | 6364 ++++++++++++++++++++
@@ -1,37 +1,44 @@share/css/style.css | 140 +-
-jdbc.url=jdbc:mysql://10.162.61.11:3306/zxystudentdb_yy
+#db
+jdbc.url=jdbc:mysql://121.41.24.23:3306/zxystudentdb
jdbc.user=zxystudentadmin
jdbc.password=dreamtech
複製程式碼
fetch
git fetch 取回branch在伺服器上的最新狀態
- git fetch 取回所有分支的最新程式碼到本地
- git fetch [branchName] 取回某個分支的最新程式碼到本地
- git fetch -p 取回程式碼伺服器上的最新分支庫
$ git fetch -p
remote: Counting objects: 265, done
remote: Finding sources: 100% (131/131)
remote: Total 131 (delta 52), reused 129 (delta 52)
Receiving objects: 100% (131/131), 125.86 KiB | 0 bytes/s, done.
Resolving deltas: 100% (52/52), completed with 34 local objects.
From ssh://xxxxx:29418/2b/xxxx-xxx-new
9daaa54..67731b6 ansp-snapshot-1.0.0 -> origin/ansp-snapshot-1.0.0
00cc0ce..73179e5 dev -> origin/dev
7873606..11ebdbc master -> origin/master
7873606..9d7fd98 release -> origin/release
複製程式碼
grep
git grep git自帶的檢索器,可以方便查詢
- git grep xxxx 查詢出所有帶xxxx的檔案以及該字元在這個檔案的位置
- git grep -n xxxx 查詢出所有帶xxxx的檔案以及該字元在這個檔案的具體位置,精確到行數
- git grep -c xxxx 統計這個字元在各個檔案出現的次數
- git grep --name-only xxx 查詢出所有帶xxxx字元的檔案的名稱
- git grep xxxx [TagName] 在某個標記點查詢帶XXXX字元的檔案
- git grep -e 'xxxx' --and -e 'hhh' 查詢出一行既包含xxx又包含hhh的檔案
- git grep --all-match -e 'xxxx' -e 'hhh' 查詢出一行包含xxx或者包含hhh的檔案
//查詢userInfo
$ git grep userInfo
src/main/java/com/zxy/mobile/service/user/service/impl/UserServiceImpl.java: User userInfo = User.dao.findFirs
//顯示行數
$ git grep -n userInfo
src/main/java/com/zxy/mobile/service/user/service/impl/UserServiceImpl.java:456: User userInfo = User.daos
//統計出現的字數
$ git grep -c userInfo
src/main/java/com/zxy/mobile/service/user/service/impl/UserServiceImpl.java:28
//顯示出現該字元的檔名
$ git grep --name-only userInfo
src/main/java/com/zxy/mobile/service/user/service/impl/UserServiceImpl.java
//基於標籤版本8.4.0進行搜尋
$ git grep userInfo v8.4.0
v8.4.0:src/main/java/com/zxy/mobile/service/user/service/impl/UserServiceImpl.java: User userInfo = User.dao
//關聯查詢,因為沒有符合條件的檔案
$ git grep -e log --and -e hello
src/main/webapp/share/index.html: console.log('hello')
//或查詢
$ git grep --all-match -e hello -e log
src/main/webapp/share/index.html: src/main/webapp/share/index.html: <div class="weui_dialog">
src/main/webapp/share/index.html:
<div class="weui_dialog_hd">
src/main/webapp/share/index.html:
<strong class="weui_dialog_title">溫馨提示</strosrc/main/webapp/share/index.html:
src/main/webapp/share/index.html: <div class="weui_dialog_ft">
src/main/webapp/share/index.html: <a href="javascript:void(0);"
src/main/webapp/share/index.html: console.log("data:" + data);
src/main/webapp/share/index.html: src/main/webapp/share/index.html: console.log('hello')
複製程式碼
blame
git blame 可以列印出git的提交記錄,精確到行,戲稱'緝凶神器'
- git blame [fileName]