Git學習筆記(一)
1、Git的介紹及安裝
Git:全宇宙最牛的分布式版本控制軟件,Git是目前世界上最先進的分布式版本控制系統
#CentOS7下git的安裝 [[email protected] ~]# yum -y install git #設置git賬號信息 [[email protected] ~]# git config --global user.name "molewan" [[email protected] ~]# git config --global user.email "[email protected]" a)因為Git是分布式版本控制系統,所以,每個機器都必須報家:你的名字和Email地址。你也許 會擔,如果有故意冒充別怎麽辦?這個不必擔,先我們相信家都是善良 知的群眾,其次,真的有冒充的也是有辦法可查的。 b)註意git config命令的--global參數,了這個參數,表你這臺機器上所有的Git倉庫都會使 這個配置,當然也可以對某個倉庫指定不同的戶名和Email地址。
2、查看git的配置:
[[email protected] ~]# git config --list user.name=molewan [email protected]
3、為git配置顏色
[[email protected] ~]# git config --global color.ui true [[email protected] ~]# git config --list user.name=molewan [email protected] color.ui=true
4、創建一個本地的git庫
[[email protected]
5、將文件添加到版本庫:
將一個文件放到Git倉庫只需要兩步:
a)git add告訴git,將文本添加到倉庫
b)用git commit告訴git,把文件提交到倉庫
[[email protected] molewan]# vim readme.txt You have new mail in /var/spool/mail/root [[email protected] molewan]# cat readme.txt hehe [[email protected] molewan]# 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) 添加一個新文件,readme.txt [[email protected] molewan]# git add readme.txt [[email protected] molewan]# git status # On branch master # # Initial commit # # Changes to be committed: # (use "git rm --cached <file>..." to unstage) # #new file: readme.txt # [[email protected] molewan]# git commit -m "the first commity" [master (root-commit) 24a5b30] the first commity 1 file changed, 1 insertion(+) create mode 100644 readme.txt You have new mail in /var/spool/mail/root 說明,commit -m 後面的內容只是針對本次提交的一個描述 [[email protected] molewan]# git status # On branch master nothing to commit, working directory clean [[email protected] molewan]# vim deply.sh You have new mail in /var/spool/mail/root [[email protected] molewan]# git status # On branch master # Untracked files: # (use "git add <file>..." to include in what will be committed) # #deply.sh nothing added to commit but untracked files present (use "git add" to track) [[email protected] molewan]# git add deply.sh [[email protected] molewan]# git commit -m "2th commit" [master f30d737] 2th commit 1 file changed, 2 insertions(+) create mode 100644 deply.sh [[email protected] molewan]# git status # On branch master nothing to commit, working directory clean [[email protected] molewan]# ls -l total 8 -rw-r--r--. 1 root root 22 Nov 26 17:15 deply.sh -rw-r--r--. 1 root root 5 Nov 26 17:07 readme.txt
6、 使用git log進行查看
[[email protected] molewan]# git log commit f30d737d24b2c04f8ce70a8c88b0071bdcc069c8 Author: molewan <[email protected]> Date: Sat Nov 26 17:16:27 2016 -0500 2th commit commit 24a5b3094f55e815ad46561f4b741c23bc7ad371 Author: molewan <[email protected]> Date: Sat Nov 26 17:13:37 2016 -0500 the first commity
7、git diff進行對比
[[email protected] molewan]# vim readme.txt # 添加一行文字,hehe [[email protected] molewan]# cat readme.txt hehe hehe [[email protected] molewan]# git status # On branch master # Changes not staged for commit: # (use "git add <file>..." to update what will be committed) # (use "git checkout -- <file>..." to discard changes in working directory) # #modified: readme.txt # no changes added to commit (use "git add" and/or "git commit -a")
[[email protected] molewan]# git diff readme.txt diff --git a/readme.txt b/readme.txt index 91ca0fa..197cac2 100644 --- a/readme.txt +++ b/readme.txt @@ -1 +1,2 @@ hehe +hehe [[email protected] molewan]# git add readme.txt [[email protected] molewan]# git commit -m "add 2hehe" [master c33cc4f] add 2hehe 1 file changed, 1 insertion(+) [[email protected] molewan]# git log commit c33cc4f3c6a1dc6741bce035de920f5a17c82b4b Author: molewan <[email protected]> Date: Sat Nov 26 17:22:05 2016 -0500 add 2hehe commit f30d737d24b2c04f8ce70a8c88b0071bdcc069c8 Author: molewan <[email protected]> Date: Sat Nov 26 17:16:27 2016 -0500 2th commit commit 24a5b3094f55e815ad46561f4b741c23bc7ad371 :...skipping... commit c33cc4f3c6a1dc6741bce035de920f5a17c82b4b Author: molewan <[email protected]> Date: Sat Nov 26 17:22:05 2016 -0500 add 2hehe commit f30d737d24b2c04f8ce70a8c88b0071bdcc069c8 Author: molewan <[email protected]> Date: Sat Nov 26 17:16:27 2016 -0500 2th commit commit 24a5b3094f55e815ad46561f4b741c23bc7ad371 Author: molewan <[email protected]> Date: Sat Nov 26 17:13:37 2016 -0500 the first commity
小結:
a)所有的版本控制系統,其實只能跟蹤本件的改動,如TXT件,網頁,所有的程序代碼等等,Git也不 例外。 b)版本控制系統 可以告訴你每次的改動,如在第5加了個單詞“Linux”刪了個單詞“Windows” c)圖片視頻這些二進制文件,雖然也能由版本 控制系統管理,但沒法跟蹤件的變化,只能把二進制文 件每次改動串起來,也就是只知道圖從100KB改成了120KB,但到底改了啥,版本控制系統不知道,沒法 知道。 d)不幸的是,Microsoft的Word格式是進制格式,因此,版本控制系統是沒法跟蹤Word件的改動的, 我們舉的例只是為了演示,真正使版本控制系統,就要以純本式編寫件。
8、版本回退(回退到上一個版本)
[[email protected] molewan]# git reset --hard HEAD^ HEAD is now at f30d737 2th commit # 說明:HEAD^代表上一個版本,^^代表上上個版本 [[email protected] molewan]# cat readme.txt hehe [[email protected] molewan]# git log commit f30d737d24b2c04f8ce70a8c88b0071bdcc069c8 Author: molewan <[email protected]> Date: Sat Nov 26 17:16:27 2016 -0500 2th commit commit 24a5b3094f55e815ad46561f4b741c23bc7ad371 Author: molewan <[email protected]> Date: Sat Nov 26 17:13:37 2016 -0500 the first commity
9、回退到指定的某個版本
[[email protected] molewan]# git reflog f30d737 HEAD@{0}: reset: moving to HEAD^ c33cc4f HEAD@{1}: commit: add 2hehe f30d737 HEAD@{2}: commit: 2th commit 24a5b30 HEAD@{3}: commit (initial): the first commity [[email protected] molewan]# git reset --hard 24a5b30 HEAD is now at 24a5b30 the first commity [[email protected] molewan]# ls -l total 4 -rw-r--r--. 1 root root 5 Nov 26 17:25 readme.txt 說明:Git的版本回退速度常快,因為Git在內部有個指向當前版本的HEAD指針,當你回退版本 的時候,Git僅僅是把HEAD從指向“append GPL”
小結:
a)HEAD指向的版本就是當前版本,因此,Git允許我們在版本的歷史之間穿梭,使命令 git reset --hard commit_id。 b)整理、排版: numbbbbb 穿梭前,git log可以查看提交歷史,以便確定要回退到哪個版本。 c) 要重返未來,git reflog查看命令歷史,以便確定要回到未來的哪個版本
10、工作區、版本庫、暫存區:
工作區:就是你在電腦能看到的目錄,如我的某個文件夾
版本庫(Repository):作區有個隱藏目錄“.git”,這個不算作區,是Git的版本庫。
暫存區:Git的版本庫存了很多東,其中最重要的就是稱為stage(或者叫index)的暫存區,還有Git為我們動創建的第一個分支master,以及指向master的個指針叫HEAD。
[[email protected] git-data]# pwd /git-data#工作區 [[email protected] git-data]# ls -la total 40 drwxr-xr-x 3 root root 119 Jun 29 11:22 . dr-xr-xr-x. 19 root root 281 Jun 29 09:32 .. -rw-r--r-- 1 root root 10 Jun 29 11:20 dev.txt drwxr-xr-x 8 root root 201 Jun 29 11:21 .git#版本庫 -rw-r--r-- 1 root root 1045 Jun 29 11:20 .gitignore -rw-r--r-- 1 root root 8870 Jun 29 11:20 git.txt -rw-r--r-- 1 root root 11357 Jun 29 11:14 LICENSE -rw-r--r-- 1 root root 25 Jun 29 11:20 README.md -rw-r--r-- 1 root root 1774 Jun 29 11:20 談運維.txt [[email protected] molewan]# git status # On branch master nothing to commit, working directory clean 需要先add,才能commit 說明:要掌握工作區的狀態,使用git status命令,而用git diff可以查看修改內容
本文出自 “冰凍vs西瓜” 博客,請務必保留此出處http://molewan.blog.51cto.com/287340/1943143
Git學習筆記(一)