一、git install


Ubuntu

sudo apt-get install git

Centos

yum install git  

Mac

brew install git

二、git initialization configure


git config --global user.name "Your Name"

git config --global user.email "email@example.com"
注意git config命令的--global参数,用了这个参数,表示你这台机器上所有的Git仓库都会使用这个配置,当然也可以对某个仓库指定不同的用户名和Email地址

三、git create repository

  • git版本库,也叫仓库,英文为repository,可以理解为一个目录,目录里的文件都可以被git管理起来,每个文件的修改、删除Git都能跟踪,以便任何时候都能够追踪历史,或者将来在某个时刻可以还原。
1、创建一个目录

$ mkdir gitrepo

$ cd gitrepo

$ pwd

/Users/antony/gitrepo

注意:目录不要包含中文

2、通过 git init 命令把这个目录变成Git可以管理的仓库

$ git init

Initialized empty Git repository in /Users/antony/gitrepo/.git/

瞬间Git就把仓库建好了,而且告诉你是一个空的仓库(empty Git repository),细心的读者可以发现当前目录下多了一个.git的目录,这个目录是Git来跟踪管理版本库的,没事千万不要手动修改这个目录里面的文件,不然改乱了,就把Git仓库给破坏了。

四、提交一个文件到仓库

1、创建一个文件

$ vim readme.txt

This is a test file

Git is a opensource app

此文件一定要放在刚才创建的仓库下,即gitrepo

2、用命令git add将文件添加到仓库

$ git add readme.txt
3、用命令git commit告诉Git,把文件提交到git仓库

$ git commit -m "up a readme file"

[master (root-commit) ba4d5a3] up a readme file

 1 file changed, 2 insertions(+)

 create mode 100644 readme.txt

-m 添加描述,一般用来告诉此次提交的说明

为什么添加文件需要add,commit两步呢? 因为commit一次可以提交多个文件,所以你可以多次add不同文件,比如


$ git add file1

$ git add file2 file3

$ git commit -m "file2 file3"

五、Git Status、diff(状态、对比)

1、Status

修改文件


$ vim readme.txt #在最后一行新添加一行

New line

查看状态


$ 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")

git status 时刻查看文件的状态,通过返回信息告诉我们文件被修改了

2、diff(查看具体修改内容)

$ git diff readme.txt

diff --git a/readme.txt b/readme.txt

index 194380c..e8e4b8b 100644

--- a/readme.txt

+++ b/readme.txt

@@ -1,2 +1,3 @@

 This is a test file

 Git is a opensource app

+New line

(END)
3、查看add及commit后的状态

git add后的状态


$ git add readme.txt

$ git status

On branch master

Changes to be committed:

  (use "git reset HEAD <file>..." to unstage)

    modified:   readme.txt

git commit后的状态


$ git commit -m "add new line"

[master a782d05] add new line

 1 file changed, 1 insertion(+)

$ git status

On branch master

nothing to commit, working tree clean

六、版本回退

先修改一次文件


$ vim readme.txt

New line 2

提交文件


$ git add readme.txt

$ git commit -m "New line 2"

[master def33d7] New line 2

 1 file changed, 1 insertion(+)
1、版本提交历史记录git log

$ git log

commit def33d73a1ded1b673f2b994bddedce9f8409a33

Author: ZAntony <zantony.top@gmail.com>

Date:   Sun Dec 11 22:36:53 2016 +0800

    New line 2

commit a782d05f438ca21a2d2336e97256bf9d7e35c058

Author: ZAntony <zantony.top@gmail.com>

Date:   Sun Dec 11 22:19:43 2016 +0800

    add new line

commit ba4d5a316c0e915e768d0a793999ff9f5f41447b

Author: ZAntony <zantony.top@gmail.com>

Date:   Sun Dec 11 22:00:13 2016 +0800

    up a readme file

git 最近显示的是New line 2,上一次是add new line,第一次是up a readme file

可以通过--pretty=oneline参数控制输出内容的数量


$ git log --pretty=oneline

def33d73a1ded1b673f2b994bddedce9f8409a33 New line 2

a782d05f438ca21a2d2336e97256bf9d7e35c058 add new line

ba4d5a316c0e915e768d0a793999ff9f5f41447b up a readme file

前面的一串数字是16进制的commit id

2、git reset 版本回退

首先,Git必须知道当前版本是哪个版本,在Git中,用HEAD表示当前版本,也就是最新的提交def33d........9a33(注意我的提交ID和你的肯定不一样),上一个版本就是HEAD^,上上一个版本就是HEAD^^,当然往上100个版本写100个^比较容易数不过来,所以写成HEAD~100


$ git reset --hard HEAD^

HEAD is now at a782d05 add new line

This is a test file

Git is a opensource app

New line

# 此时版本已经回退
3、当返回到上一个版本后,突然想返回到为返回前的版本,即New line 2

查看日志,发现并未找到回退前的版本的commit id


$ git log

commit a782d05f438ca21a2d2336e97256bf9d7e35c058

Author: ZAntony <zantony.top@gmail.com>

Date:   Sun Dec 11 22:19:43 2016 +0800

    add new line

commit ba4d5a316c0e915e768d0a793999ff9f5f41447b

Author: ZAntony <zantony.top@gmail.com>

Date:   Sun Dec 11 22:00:13 2016 +0800

    up a readme file
当通过git reset --hard HEAD^恢复到add new line时,在想恢复到之前的New line 2,就必须找到他的commit id,可以通过查看之前的命令历史来查找,也可以通过git reflog来查看commit id

git reflog


$ git reflog # 通过命令来查看commit id,id是不完整的,但是可以使用,只要保证id唯一性。

a782d05 HEAD@{0}: reset: moving to HEAD^

def33d7 HEAD@{1}: commit: New line 2

a782d05 HEAD@{2}: commit: add new line

ba4d5a3 HEAD@{3}: commit (initial): up a readme file

恢复版本


$ git reset --hard def33d7

HEAD is now at def33d7 New line 2

$ cat readme.txt

This is a test file

Git is a opensource app

New line

New line 2









本文转自 赵大鑫 51CTO博客,原文链接:http://blog.51cto.com/xinzong/2043493,如需转载请自行联系原作者
Logo

瓜分20万奖金 获得内推名额 丰厚实物奖励 易参与易上手

更多推荐