天天看點

GIT使用—建立一個版本庫

本文介紹如何使用GIT建立一個版本庫

一、GIT指令行

[root@localhost ~]# git
usage: git [--version] [--exec-path[=GIT_EXEC_PATH]] [--html-path]
           [-p|--paginate|--no-pager] [--no-replace-objects]
           [--bare] [--git-dir=GIT_DIR] [--work-tree=GIT_WORK_TREE]
           [--help] 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 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

See 'git help COMMAND' for more information on a specific command.
           

二、Git建立倉庫

(1)建立一個項目

[root@localhost ~]# mkdir public_html
[root@localhost ~]# cd public_html/
[root@localhost public_html]# echo 'My website is alive!' > index.html
[root@localhost public_html]# ls
index.html
           

(2)執行git init将目錄轉化為版本庫

[root@localhost public_html]# git init
Initialized empty Git repository in /root/public_html/.git/
[root@localhost public_html]# ls -a
.  ..  .git  index.html
           

在執行完成 git init 指令後,Git 倉庫會生成一個 .git 目錄,該目錄包含了資源的所有中繼資料,其他的項目目錄保持不變(不像 SVN 會在每個子目錄生成 .svn 目錄,Git 隻在倉庫的根目錄生成 .git 目錄)。

(3)将檔案添加到版本庫

[root@localhost public_html]# git add index.html 
           

如果目錄中有多個檔案,使用git add . 指令将目前目錄及子目錄的檔案都添加到版本庫中。

(4)檢視狀态

[root@localhost public_html]# git status
# On branch master
#
# Initial commit
#
# Changes to be committed:
#   (use "git rm --cached <file>..." to unstage)
#
#	new file:   index.html
#
           

(5)送出

[root@localhost public_html]# git commit -m "Initial contents of public_html" --author="tong <[email protected]>"
[master (root-commit) b4e2a14] Initial contents of public_html
 1 files changed, 1 insertions(+), 0 deletions(-)
 create mode 100644 index.html
           

(6)讓git在送出時打開編輯器,設定你的GIT_EDITOR環境變量

[root@localhost public_html]# export GIT_EDITOR=vim
[root@localhost public_html]# git status
# On branch master
nothing to commit (working directory clean)
           

(7)編輯檔案再次送出

[root@localhost public_html]# cat index.html 
<html>
<body>
My website is alive!
</body>
</html>
[root@localhost public_html]# git commit index.html  這時會進入編輯器
# Please enter the commit message for your changes. Lines starting
# with '#' will be ignored, and an empty message aborts the commit.
# Explicit paths specified without -i nor -o; assuming --only paths...
# On branch master
# Changes to be committed:
#   (use "git reset HEAD <file>..." to unstage)
#
#       modified:   index.html
#
This is new html!
儲存退出
[root@localhost public_html]# git commit index.html
[master 0a30716] This is new html!
 1 files changed, 4 insertions(+), 0 deletions(-)
           

(8)檢視送出

[root@localhost public_html]# git log
commit 0a3071601cc10777e271a952ead46cffba233e24
Author: tong <[email protected]>
Date:   Tue Feb 27 11:52:29 2018 +0800

    This is new html!

commit b4e2a14de84d29ea8890a2e19f039eb08bc2fc7d
Author: tong <[email protected]>
Date:   Tue Feb 27 11:45:23 2018 +0800

    Initial contents of public_html

[root@localhost public_html]# git show b4e2a14de84d29ea8890a2e19f039eb08bc2fc7d
commit b4e2a14de84d29ea8890a2e19f039eb08bc2fc7d
Author: tong <[email protected]>
Date:   Tue Feb 27 11:45:23 2018 +0800

    Initial contents of public_html

diff --git a/index.html b/index.html
new file mode 100644
index 0000000..34217e9
--- /dev/null
+++ b/index.html
@@ -0,0 +1 @@
+My website is alive!

[root@localhost public_html]# git show-branch --more=5
[master] This is new html!
[master^] Initial contents of public_html
           

(9)檢視送出差異

[root@localhost public_html]# git diff 0a3071601cc10777e271a952ead46cffba233e24 b4e2a14de84d29ea8890a2e19f039eb08bc2fc7d
diff --git a/index.html b/index.html
index 8638631..34217e9 100644
--- a/index.html
+++ b/index.html
@@ -1,5 +1 @@
-<html>
-<body>
 My website is alive!
-</body>
-</html>
           

(10)版本庫内檔案的删除和重命名

[root@localhost public_html]# ls
index.html  poem.html
[root@localhost public_html]# git rm poem.html 
rm 'poem.html'
[root@localhost public_html]# git commit -m "Remove a poem"
[master 19a473c] Remove a poem
 1 files changed, 0 insertions(+), 4 deletions(-)
 delete mode 100644 poem.html
           
[root@localhost public_html]# ls
bar.html  index.html
[root@localhost public_html]# mv bar.html foo.html
[root@localhost public_html]# git rm bar.html
rm 'bar.html'
[root@localhost public_html]# git add foo.html
[root@localhost public_html]# git commit -m "mv bar to foo"
[master aa431d9] mv bar to foo
 1 files changed, 0 insertions(+), 0 deletions(-)
 rename bar.html => foo.html (100%)
           

(11)建立版本庫的副版本

這就是不同開發者如何通過Git在相同的檔案上從事項目開發,并保持與其他版本庫同步。

[root@localhost ~]# git clone public_html my_website
Initialized empty Git repository in /root/my_website/.git/
[root@localhost ~]# ls -lsa public_html my_website
my_website:
total 20
4 drwxr-xr-x  3 root root 4096 Feb 27 13:08 .
4 dr-xr-x---. 8 root root 4096 Feb 27 13:08 ..
4 -rw-r--r--  1 root root   51 Feb 27 13:08 foo.html
4 drwxr-xr-x  8 root root 4096 Feb 27 13:08 .git
4 -rw-r--r--  1 root root   51 Feb 27 13:08 index.html

public_html:
total 20
4 drwxr-xr-x  3 root root 4096 Feb 27 13:05 .
4 dr-xr-x---. 8 root root 4096 Feb 27 13:08 ..
4 -rw-r--r--  1 root root   51 Feb 27 13:04 foo.html
4 drwxr-xr-x  8 root root 4096 Feb 27 13:06 .git
4 -rw-r--r--  1 root root   51 Feb 27 11:50 index.html
[root@localhost ~]# diff -r public_html my_website
Only in public_html/.git: COMMIT_EDITMSG
diff -r public_html/.git/config my_website/.git/config
5a6,11
> [remote "origin"]
> 	fetch = +refs/heads/*:refs/remotes/origin/*
> 	url = /root/public_html
> [branch "master"]
> 	remote = origin
> 	merge = refs/heads/master
Binary files public_html/.git/index and my_website/.git/index differ
diff -r public_html/.git/logs/HEAD my_website/.git/logs/HEAD
1,6c1
< 0000000000000000000000000000000000000000 b4e2a14de84d29ea8890a2e19f039eb08bc2fc7d tong <[email protected]> 1519703123 +0800 commit (initial): Initial contents of public_html
< b4e2a14de84d29ea8890a2e19f039eb08bc2fc7d 0a3071601cc10777e271a952ead46cffba233e24 tong <[email protected]> 1519703549 +0800 commit: This is new html!
< 0a3071601cc10777e271a952ead46cffba233e24 5be473e1ed6d04ed9e96b7fa3e9e2860607cbd31 tong <[email protected]> 1519703956 +0800 commit: add poem.html
< 5be473e1ed6d04ed9e96b7fa3e9e2860607cbd31 19a473c2e935efa59b8edea19d2d12be96987a97 tong <[email protected]> 1519704003 +0800 commit: Remove a poem
< 19a473c2e935efa59b8edea19d2d12be96987a97 1ed9a862e62bd5513021838cb435cf8170e2173d tong <[email protected]> 1519707877 +0800 commit: new bar
< 1ed9a862e62bd5513021838cb435cf8170e2173d aa431d938e85445f6c22c7389a37349f587d5b01 tong <[email protected]> 1519707981 +0800 commit: mv bar to foo
---
> 0000000000000000000000000000000000000000 aa431d938e85445f6c22c7389a37349f587d5b01 tong <[email protected]> 1519708133 +0800 clone: from /root/public_html
diff -r public_html/.git/logs/refs/heads/master my_website/.git/logs/refs/heads/master
1,6c1
< 0000000000000000000000000000000000000000 b4e2a14de84d29ea8890a2e19f039eb08bc2fc7d tong <[email protected]> 1519703123 +0800 commit (initial): Initial contents of public_html
< b4e2a14de84d29ea8890a2e19f039eb08bc2fc7d 0a3071601cc10777e271a952ead46cffba233e24 tong <[email protected]> 1519703549 +0800 commit: This is new html!
< 0a3071601cc10777e271a952ead46cffba233e24 5be473e1ed6d04ed9e96b7fa3e9e2860607cbd31 tong <[email protected]> 1519703956 +0800 commit: add poem.html
< 5be473e1ed6d04ed9e96b7fa3e9e2860607cbd31 19a473c2e935efa59b8edea19d2d12be96987a97 tong <[email protected]> 1519704003 +0800 commit: Remove a poem
< 19a473c2e935efa59b8edea19d2d12be96987a97 1ed9a862e62bd5513021838cb435cf8170e2173d tong <[email protected]> 1519707877 +0800 commit: new bar
< 1ed9a862e62bd5513021838cb435cf8170e2173d aa431d938e85445f6c22c7389a37349f587d5b01 tong <[email protected]> 1519707981 +0800 commit: mv bar to foo
---
> 0000000000000000000000000000000000000000 aa431d938e85445f6c22c7389a37349f587d5b01 tong <[email protected]> 1519708133 +0800 clone: from /root/public_html
Only in my_website/.git: packed-refs
Only in my_website/.git/refs: remotes