git使用詳解php
一:本文介紹git的使用方法html
1.1:安裝即獲取幫助:git
1.1.1:安裝:github
1.1.2:獲取幫助:web
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 merge 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
1.1.3:設置全局用戶名和郵件地址:vim
[root@saltmaster ~]# git config –global –list #獲取全局設置
user.name=zhangjie
user.email=1669121886@qq.com服務器
1.1.4:初始化一個空的git倉庫ide
#git init #當前目錄初始化一個空的git倉庫測試
Initialized empty Git repository in /root/web/.git/
1.1.5:建立一個文件並提交:fetch
#
#
#
nothing added to commit but untracked files present (use "git add" to track)
#
#
#
[master (root-commit) e90e767] text git #主分支以root提交
1 file changed, 1 insertion(+) #一個文件更改
create mode 100644 readme.txt #權限644
commit 04910a72bed1101423655dd9b5535d6447dfbea7
Author: zhangjie <1669121886@qq.com> #提交人和郵箱
Date: Wed Aug 17 16:51:08 2016 -0400 #提交日期
index.html #提價的文件
commit e90e7673c2bdfd6892f44e78eb47f9ceb6cba66a
Author: zhangjie <1669121886@qq.com>
Date: Wed Aug 17 16:45:03 2016 -0400
text git
diff --git a/index.html b/index.html
index 9015a7a..190a180 100644
--- a/index.html
+++ b/index.html
@@ -1 +1 @@
-index #刪除的行
+123 #添加的行
1.1.7:git的版本回退:
HEAD is now at 04910a7 index.html
1.1.8:回退到指定的版本:
04910a7 HEAD@{0}: reset: moving to HEAD^
193d182 HEAD@{1}: commit: xx
04910a7 HEAD@{2}: commit: index.html
e90e767 HEAD@{3}: commit (initial): text git
HEAD is now at 193d182 xx
123
1.1.9:建立並切換到一個分支:
git branch #查看當前的分支
Switched to branch 'dev'
[dev 8ebd5ff] dev comm
1 file changed, 1 insertion(+), 1 deletion(-)
[root@saltmaster ~/web]# ll #查看當前分支的文件,分支會集成master的數據
total 20
-rw-r--r-- 1 root root 4 Aug 17 22:39 index.html
-rw-r--r-- 1 root root 11357 Aug 17 21:59 LICENSE
-rw-r--r-- 1 root root 27 Aug 17 21:08 readme.txt
1.1.10:分支合併,首先切換到master分支:
[root@saltmaster ~/web]# git branch #當前是主分支
dev
[root@saltmaster ~/web]# git branch #查看當前所在的分支
dev
[root@saltmaster ~/web]# git push origin dev
Counting objects: 14, done.
Delta compression using up to 2 threads.
Compressing objects: 100% (9/9), done.
Writing objects: 100% (12/12), 1001 bytes | 0 bytes/s, done.
Total 12 (delta 4), reused 0 (delta 0)
remote: Resolving deltas: 100% (4/4), done.
To git@github.com:zhangshijle/demo.git
[root@saltmaster ~/web]# git tag v1.2 #打一個標籤
[root@saltmaster ~/web]# git tag #查看全部標籤
v1.2
[root@saltmaster ~/web]# vim readme.txt #代碼變動
[root@saltmaster ~/web]# git add * #添加全部
[root@saltmaster ~/web]# git commit -m "x" #提交信息
[master d0712e5] x
1 file changed, 3 insertions(+)
[root@saltmaster ~/web]# git tag v1.3 #提交以後打標籤
[root@saltmaster ~/web]# git tag #查看全部的標籤
v1.2
v1.3
1.1.13:查看tag提交的說明:
[root@saltmaster ~/web]# git show v1.3
commit d0712e5bc70068e998475f1337e42adefd52f4f0 #這就是版本號
Author: zhangjie <1669121886@qq.com>
Date: Wed Aug 17 23:17:49 2016 -0400
x
diff --git a/readme.txt b/readme.txt
index bd143a6..15abcd5 100644
--- a/readme.txt
+++ b/readme.txt
@@ -1,3 +1,6 @@
git test
git test
git test
+git test
+git test
+git test
1.1.14:推送指定版本的代碼 git push:
[root@saltmaster ~/web]# git push origin v1.2 #推送指定版本到git服務器
Counting objects: 11, done.
Delta compression using up to 2 threads.
Compressing objects: 100% (6/6), done.
Writing objects: 100% (7/7), 721 bytes | 0 bytes/s, done.
Total 7 (delta 3), reused 0 (delta 0)
remote: Resolving deltas: 100% (3/3), completed with 2 local objects.
To git@github.com:zhangshijle/demo.git
[root@saltmaster ~/web]# git checkout v1.3 #回滾到指定版本的代碼
M index.php
Previous HEAD position was 345dbd6... Merge branch 'dev'
HEAD is now at d0712e5... x
1.1.16:忽略文件:
[root@saltmaster ~/web]# cat .gitignore #忽略的文件定義在此文件內
1.1.17:命令總結:
git add index.html #添加到暫存區
git status #查看狀態
git commit -m "xx" 提交
git log #查看提交日誌
git reset --hard HEAD^ 回退到是上一個版本
git reflog 查看歷史提交信息
git reset --hard xxxxx #根據提交ID回退
1.2:什麼是暫存區和工做區?
工做區就是編輯文件的目錄區域,須要將工做區的修改好的文件add到咱存區才能提交到git服務器,在工做區有多個文件的時候能夠將一個或多個文件添加至暫存區提交到git服務器便可。
1.3:git checkout 從版本庫已經提交的內容從新拉取一個文件,用戶臨時測試本地git配置文件的內容,測試完成以後再從git服務器獲取此文件的場景,用法以下:
[root@saltmaster ~/web]# cat index.html #當前內容123123[root@saltmaster ~/web]# echo abc >> index.html #添加一些內容[root@saltmaster ~/web]# cat index.html #添加以後的內容,如今須要從新從git獲取此文件123123abc[root@saltmaster ~/web]# git checkout -- index.html #文件前面有兩個短橫線,表示從新從git服務器獲取此文件,即以git服務器中的文件內容爲準,若是直接提交的話就把git服務器的文件覆蓋了[root@saltmaster ~/web]# cat index.html 123123abc