git使用詳解

git使用詳解php

一:本文介紹git的使用方法html

1.1:安裝即獲取幫助:git

1.1.1:安裝:github

yum install git -y

1.1.2:獲取幫助:web

git –help

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

git config –global user.name zhangjie

git config –global user.email 1669121886@qq.com

[root@saltmaster ~]# git config –global –list #獲取全局設置
user.name=zhangjie
user.email=1669121886@qq.com服務器

git config –global –help #使用幫助

1.1.4:初始化一個空的git倉庫ide

#git init #當前目錄初始化一個空的git倉庫測試

Initialized empty Git repository in /root/web/.git/
1.1.5:建立一個文件並提交:fetch

echo 「git test」 > readme.txt

git status

On branch master

#

Initial commit

#

Untracked files:

(use "git add <file>..." to include in what will be committed)

#

readme.txt

nothing added to commit but untracked files present (use "git add" to track)

git add readme.txt #添加到暫存區

git status #查看工做區的當前狀態

On branch master

#

Initial commit

#

Changes to be committed:

(use "git rm --cached <file>..." to unstage)

#

new file: readme.txt

git commit -m 「text git」 #-m是提交說明

[master (root-commit) e90e767] text git #主分支以root提交
1 file changed, 1 insertion(+) #一個文件更改
create mode 100644 readme.txt #權限644

git log #查看操做日誌

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

echo 「123」 > index.html

git diff index.html

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的版本回退:

git reset –hard HEAD^ #HEAD爲當前版本,後面加一個^是當前版本的上一個版本,加兩個^^回退帶上上一個版本,加三個^^^是上上上一個版本,以此類推

HEAD is now at 04910a7 index.html
1.1.8:回退到指定的版本:

git reflog #獲取每次提交的ID,能夠使用–hard根據提交的ID進行版本回退

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

git reset –hard 193d182

HEAD is now at 193d182 xx

cat index.html

123
1.1.9:建立並切換到一個分支:

git branch #查看當前的分支

git branch dev

git checkout dev #切換到dev分支

Switched to branch 'dev'

echo "dev" > index.html #建立一個你文件

git add index.html #添加到分支暫存區

git commit -m "dev comm" #提交到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

  • master
    [root@saltmaster ~/web]# echo "masetr" > index.html #寫一個master的文件
    [root@saltmaster ~/web]# git add index.html #添加到本地暫存區
    [root@saltmaster ~/web]# git commit -m "master" #提交到本地服務器
    [master cdebc62] master
    1 file changed, 1 insertion(+), 1 deletion(-)
    [root@saltmaster ~/web]# git checkout dev #切換到dev分支
    Switched to branch 'dev'
    [root@saltmaster ~/web]# echo "dev dev dev dev" > index.php #建立一個文件
    [root@saltmaster ~/web]# git add index.php
    [root@saltmaster ~/web]# git commit -m "dev"
    [dev debcd1c] dev
    1 file changed, 1 insertion(+)
    create mode 100644 index.php
    [root@saltmaster ~/web]# git checkout master #切換到mster分支,合併要在master端進行
    [root@saltmaster ~/web]# git merge dev #合併dev分支
    Merge made by the 'recursive' strategy.
    index.php | 5 ++++-
    1 file changed, 4 insertions(+), 1 deletion(-)
    [root@saltmaster ~/web]# cat index.php #合併後的內容
    dev1
    dev1
    dev1
    dev1
    1.1.11:推送分支到服務器:

[root@saltmaster ~/web]# git branch #查看當前所在的分支
dev

  • master
    [root@saltmaster ~/web]# git tag v1.2 #打一個標籤
    [root@saltmaster ~/web]# git tag #查看當前全部的標籤
    v1.2

    git push origin 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

  • [new branch] dev -> dev
    1.1.12:打標籤,在版本發佈不頻繁的時候每次發佈就打一個tag,生成環境直接checkout這個tag便可,若是發佈頻繁就使用版本號:

[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

  • [new tag] v1.2 -> v1.2
    1.1.15:從git服務器獲取指定版本號的代碼到本地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

相關文章
相關標籤/搜索