Git之`Hello, World`

前提條件

  1. 你必須在你的操做系統安裝git,若是沒有的話,那請自行google:-)
  2. 懂得命令行的基本使用

那麼,下面咱們開始git的hello world之旅吧! html

新建git repository

$ mkdir ~/public_html
$ cd ~/public_html
$ echo 'My website is alive!' > index.html
$ git init
Initialized empty Git repository in /Users/longkai/public_html/.git/

執行後生成的隱藏.git/目錄就是git維護的你的這個repository的整個版本庫 git

你的pulic_html/被稱爲工做目錄 github

向git repository中添加文件

使用git add (file|files|dirs)向repository中添加文件 web

一次能夠添加一個或者多個文件,也能夠添加一個目錄 shell

$ git add index.html

命令執行成功後,git知道了index.html將要保留在repository中,可是git只是把index.html暫存了起來,做爲在下一次提交以前的一個臨時動做。 vim

git將add和commit劃分爲兩個過程是爲了保持repository的穩定性,深思熟慮後的提交不至於repository太亂:-) bash

接下來執行git status來查看工做目錄和版本庫之間的狀態 編輯器

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

運行的結果表示新文件index.html文件將在下一次提交後添加到repository中 ui

git repository除了文件內容的變化外,git還會記錄每次提交的元數據,包括提交log和做者 google

$ git commit -m "Initial contents of public_html"
[master (root-commit) 0d3625d] Initial contents of public_html
 1 file changed, 1 insertion(+)
 create mode 100644 index.html

這裏簡單的使用了一句話的log,實際中你可使用你本身喜歡的文本編輯器記錄更詳細的log,好比vim,若是默認不是vim,那麼在bash中export GIT_EDITOR=vim

接下來咱們再來看看工做目錄和reposiroty之間的狀態

$ git status
# On branch master
nothing to commit, working directory clean

看,clean working directory,這意味着工做目錄如今沒有修改,添加,刪除的文件或內容啦!

配置提交的做者

剛纔的提交我沒有指名做者,由於我已經配置了默認的做者:-)

$ git config --global user.name "longkai"
$ git config --global user.email "im.longkai@gmail.com"

建立一個新的提交

$ cd ~/public_html
$ vim index.html
# edit file
$ cat index.html
<html>
	<body>
		welcome to my site!
    </body>
</html>

接下來直接提交

$ git commit index.html
# open vim to add log
$ git status
# On branch master
nothing to commit, working directory clean

也許你會以爲奇怪,爲何不是先add再commit呢?若是熟悉git後發現其實不是這樣的,由於這個文件在提交以前已經加入到repository了,git已經認識她啦!

如今,咱們已經有了2個提交

查看個人提交

一旦你有了提交後你即可以查閱你的提交

命令git log將整個提交列表倒序打印出來

$ git log
commit fcf1a7f60148aabf9c52a8a8869f9dde50fdf95e
Author: longkai <im.longkai@gmail.com>
Date:   Thu Nov 28 23:55:02 2013 +0800

    convert to html

commit 0d3625d1b212debc3e683928c791cc7cf9e6181d
Author: longkai <im.longkai@gmail.com>
Date:   Thu Nov 28 23:40:06 2013 +0800

    Initial contents of public_html

以上只是查看了提交的ID(如fcf1a7f60148aabf9c52a8a8869f9dde50fdf95e),做者,日期和log,想要進一步查看內容的變動,使用git show命令

$ git show fcf1a7f60148aabf9c52a8a8869f9dde50fdf95e
commit fcf1a7f60148aabf9c52a8a8869f9dde50fdf95e
Author: longkai <im.longkai@gmail.com>
Date:   Thu Nov 28 23:55:02 2013 +0800

    convert to html

diff --git a/index.html b/index.html
index 34217e9..8a9dac1 100644
--- a/index.html
+++ b/index.html
@@ -1 +1,5 @@
-My website is alive!
+<html>
+       <body>
+               welcome to my size!
+       </body>
+</html>

不帶commit id則表示最近的一次提交。很酷,是吧!

另外,命令git show-branch則提供了更簡潔的展示

$ git show-branch --more=7
[master] convert to html
[master^] Initial contents of public_html

因爲咱們只有2次提交,因此只顯示了2條,不帶任何參數則表示最近的一次提交

查看提交間的差別

想要查看任意兩個提交之間的差別?用git diff commit_id1 commit_id2

$ git diff fcf1a7 0d3625
diff --git a/index.html b/index.html
index 8a9dac1..34217e9 100644
--- a/index.html
+++ b/index.html
@@ -1,5 +1 @@
-<html>
-       <body>
-               welcome to my size!
-       </body>
-</html>
+My website is alive!

能夠看到,commit_id能夠是開頭的幾個字母的簡寫~

在repository中刪除和重命名文件

這個和普通的命很像,只是加上了git命令前綴

假設你的repository中有一個test.txt文件不想要他了

$ git rm test.txt 
rm 'test.txt'
$ git status
# On branch master
# Changes to be committed:
#   (use "git reset HEAD <file>..." to unstage)
#
#       deleted:    test.txt
#
$ git commit -m "remove test.txt file"
[master daca0eb] remove test.txt file
 1 file changed, 1 deletion(-)
 delete mode 100644 test.txt

能夠看到,執行git rm test.txt命令後,test.txt文件被刪除到了暫存區,下一次提交就將被刪除,這個和add很是像

假設你有一個foo.txt的文件,想要重命名爲bar.txt

$ git mv foo.txt bar.txt
$ git status
# On branch master
# Changes to be committed:
#   (use "git reset HEAD <file>..." to unstage)
#
#       renamed:    foo.txt -> bar.txt
#
$ git commit -m "rename foo.txt to bar.txt"
[master 2e1ca23] rename foo.txt to bar.txt
 1 file changed, 0 insertions(+), 0

OK,就是這麼簡單!

複製你的repository

使用git clone命令能將一個完整的版本庫複製,包裹全部的提交,變動,徹底同樣。

若是是從web上clone,那麼還會有一些額外的信息複製下來,方便追蹤與增長提交等,這裏就先不涉及啦。

$ cd ~
$ git clone public_html/ public_html2
Cloning into 'public_html2'...
done.
Checking connectivity... done


配置文件

git的配置文件是繼承式的,下面分別從優先級高到低說明

.git/config
    repository相關的配置,可使用--file選項對其進行操做(默認操做也是如此)
~/.gitconfig
    用戶相關的配置,可使用--global選項對其進行操做
/etc/gitconfig
    系統相關的配置,可使用--system選項對其進行配置(若是你有足夠的權限),另外,該文件可能根據操做系統的不一樣放在不一樣的目錄

可使用git config -l命令查看當前適用的配置

$ git config -l
user.name=longkai
user.email=im.longkai@gmail.com
color.diff=auto
color.ui=true
alias.lg=log --color --graph --pretty=format:'%Cred%h%Creset -%C(yellow)%d%Creset %s %Cgreen(%cr) %C(bold blue)<%an>%Creset' --abbrev-commit
alias.st=status
alias.br=branch
alias.ci=commit
core.repositoryformatversion=0
core.filemode=true
core.bare=false
core.logallrefupdates=true
core.ignorecase=true
core.precomposeunicode=false

固然,你也能夠直接查看.git/config文件的內容

$ cat .git/config
[core]
        repositoryformatversion = 0
        filemode = true
        bare = false
        logallrefupdates = true
        ignorecase = true
        precomposeunicode = false

發現怎麼沒有以前show的那麼長呢?其實它是繼承了用戶配置的,使用git config -l --global查看便可!

使用git config --unset命令刪除配置,固然,你也可使用文本編輯器直接編輯配置文件!

$ git config --unset --global user.mail


配置別名

命令太長?別擔憂,正如shell提供的別名同樣,你也能夠給命令添加別名呢!

$ git status
# On branch master
nothing to commit, working directory clean
$ git config alias.s "status"
$ git s
# On branch master
nothing to commit, working directory clean

能夠看到,咱們使用git s完成了git status的命令,另外,咱們使用的是項目相關的配置哦~

說明

本筆記參考自[version control with git 2nd],感謝原做者!

github連接:https://github.com/longkai/longkai/blob/master/notes/git/get-started.md

相關文章
相關標籤/搜索