Git官方提供的快速入門教程:https://try.github.io/levels/1/challenges/1git
特色:Git極其強大的分支管理;分佈式版本github
集中式版本控制系統,版本庫是集中存放在中央服務器的,而幹活的時候,用的都是本身的電腦,因此要先從中央服務器取得最新的版本,而後開始幹活,幹完活了,再把本身的活推送給中央服務器。中央服務器就比如是一個圖書館,你要改一本書,必須先從圖書館借出來,而後回到家本身改,改完了,再放回圖書館。集中式版本控制系統最大的毛病就是必須聯網才能工做。bash
使用Git服務器
首先,選擇一個合適的地方,建立一個空目錄,經過git init
命令把這個目錄變成Git能夠管理的倉庫app
zhangqulideMacBook-Air:~ zhangquli$ mkdir git_test zhangqulideMacBook-Air:~ zhangquli$ cd git_test zhangqulideMacBook-Air:git_test zhangquli$ git init Initialized empty Git repository in /Users/zhangquli/git_test/.git/
2。把文件添加到版本庫分佈式
全部的版本控制系統,其實只能跟蹤文本文件的改動,好比TXT文件,網頁,全部的程序代碼等等,Git也不例外。版本控制系統能夠告訴你每次的改動,好比在第5行加了一個單詞「Linux」,在第8行刪了一個單詞「Windows」。而圖片、視頻這些二進制文件,雖然也能由版本控制系統管理,但無法跟蹤文件的變化,只能把二進制文件每次改動串起來,也就是隻知道圖片從100KB改爲了120KB,但到底改了啥,版本控制系統不知道,也無法知道。ide
把一個文件放到Git倉庫只須要兩步。spa
第一步,用命令git add
告訴Git,把文件添加到倉庫版本控制
第二步,用命令git commit
告訴Git,把文件提交到倉庫:code
zhangqulideMacBook-Air:git_test zhangquli$ git add a.txt zhangqulideMacBook-Air:git_test zhangquli$ git commit a.txt [master (root-commit) 0c9192c] add a file a.txt #可使用git commit -m "add a file a.txt「 添加描述
3.修改文件
修改文件後,運行git status
命令看看結果:
zhangqulideMacBook-Air:git_test zhangquli$ vi a.txt
zhangqulideMacBook-Air:git_test zhangquli$ 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: a.txt
#
no changes added to commit (use "git add" and/or "git commit -a")#a.txt被修改過了,但尚未準備提交的修改
看看具體修改了什麼內容,須要用git diff
這個命令看看
zhangqulideMacBook-Air:git_test zhangquli$ git diff a.txt
diff --git a/a.txt b/a.txt
index de891d0..14078f4 100644
--- a/a.txt
+++ b/a.txt
@@ -1 +1,2 @@
heiheiehi
+changed
知道了對readme.txt做了什麼修改後,再把它提交到倉庫就放心多了,提交修改和提交新文件是同樣的兩步 git add git commit
版本回退
版本控制系統確定有某個命令能夠告訴咱們歷史記錄,在Git中,咱們用git log
命令查看:
zhangqulideMacBook-Air:git_test zhangquli$ git log commit 8ed1c1a1d5666a98d2464787177f61e2a23126a6 #版本號 Author: zqlmmd <zhangquli@zhangqulideMacBook-Air.local> Date: Sun May 29 23:46:24 2016 +0800 add a line #commit的描述 commit 0c9192cfde234fc37b351f959cc791ebb1c3f68a Author: zqlmmd <zhangquli@zhangqulideMacBook-Air.local> Date: Sun May 29 23:29:35 2016 +0800 add a file a.txt
咱們要把當前版本回退到上一個版本,首先要知道當前版本是哪一個版本,在Git中,用HEAD
表示當前版本,,上一個版本就是HEAD^
,上上一個版本就是HEAD^^
,固然往上100個版本寫100個^
比較容易數不過來,因此寫成HEAD~100
。而後使用git reset
命令 --hard參數
zhangqulideMacBook-Air:git_test zhangquli$ git reset --hard HEAD^ HEAD is now at 0c9192c add a file a.txt zhangqulideMacBook-Air:git_test zhangquli$ git log commit 0c9192cfde234fc37b351f959cc791ebb1c3f68a Author: zqlmmd <zhangquli@zhangqulideMacBook-Air.local> Date: Sun May 29 23:29:35 2016 +0800 add a file a.txt
再回去
$ git reset --hard 3628164
HEAD is now at 3628164 append GPL