分布式版本控制系统-Git

1、下载安装,并进行初始设置

Git的官网:https://git-scm.com/downloadsgit

下载安装完成后打开,以下图界面:命令行

再进行用户和邮件的设置:3d

$ git config --global user.name "jin"
$ git config --global user.email "akipa11@163.com"

--global参数,表示你这台机器上全部的Git仓库都会使用这个配置。rest

2、Git的版本库

版本库又名仓库,英文名repository。版本库的全部文件均可以被Git管理起来,每一个文件的修改、删除,Git都能跟踪,以便任什么时候刻均可以追踪历史,或者在未来某个时刻能够“还原”。日志

建立版本库:code

$ mkdir learn_git

Jin@Jin-PC MINGW64 ~
$ cd learn_git

Jin@Jin-PC MINGW64 ~/learn_git
$ pwd
/c/Users/Jin/learn_git

使用命令git init对版本库进行初始化:blog

Jin@Jin-PC MINGW64 ~/learn_git
$ git init
Initialized empty Git repository in C:/Users/Jin/learn_git/.git/

这时候查看目录,会发现当前目录下多了一个.git的目录,这个目录是Git来跟踪管理版本库的,如无必要,不要对这个目录进行修改。ip

Jin@Jin-PC MINGW64 ~/learn_git (master)
$ ls -al
total 12
drwxr-xr-x 1 Jin 197121 0 四月 19 19:50 ./
drwxr-xr-x 1 Jin 197121 0 四月 19 19:49 ../
drwxr-xr-x 1 Jin 197121 0 四月 19 19:50 .git/

3、Git的经常使用操做:

下面咱们新建一个文件test.txt,内容以下:get

Hello World!
Git is a version control system.
I'm learning Git.

git add命令把文件test.txt添加到仓库中。it

Jin@Jin-PC MINGW64 ~/learn_git (master)
$ git add test.txt
warning: LF will be replaced by CRLF in test.txt.
The file will have its original line endings in your working directory.

命令git commit则是把文件提交到仓库中。

Jin@Jin-PC MINGW64 ~/learn_git (master)
$ git commit -m "wrote a test file"
[master (root-commit) ecd1e29] wrote a test file
 1 file changed, 3 insertions(+)
 create mode 100644 test.txt

参数-m后面输入的是本次提交的说明,能够输入任意内容,固然最好是有意义的,这样你就能从历史记录里方便地找到改动记录。

咱们把test.txt文件的内容进行修改,内容以下:

Jin@Jin-PC MINGW64 ~/learn_git (master)
$ cat test.txt
Git is a version control system.
I'm learning Git.

运行git status命令:

Jin@Jin-PC MINGW64 ~/learn_git (master)
$ 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:   test.txt

no changes added to commit (use "git add" and/or "git commit -a")

git status命令可让咱们时刻掌握仓库当前的状态,能够看到,文件test.txt已经被修改了,但尚未提交修改。

git diff能够查看具体修改了什么内容

Jin@Jin-PC MINGW64 ~/learn_git (master)
$ git diff test.txt
warning: LF will be replaced by CRLF in test.txt.
The file will have its original line endings in your working directory.
diff --git a/test.txt b/test.txt
index 049ab45..da0261d 100644
--- a/test.txt
+++ b/test.txt
@@ -1,3 +1,2 @@
-Hello World!
 Git is a version control system.
 I'm learning Git.

知道了文件做了哪些修改,此时就能够放心把文件提交到仓库了:

Jin@Jin-PC MINGW64 ~/learn_git (master)
$ git add test.txt

此时再用git status命令查看,能够看到,要被提交的修改包括文件test.txt:

Jin@Jin-PC MINGW64 ~/learn_git (master)
$ git status
On branch master
Changes to be committed:
  (use "git reset HEAD <file>..." to unstage)

        modified:   test.txt

提交到仓库:

Jin@Jin-PC MINGW64 ~/learn_git (master)
$ git commit -m "del first line"
[master 88cb81a] del first line
 1 file changed, 1 deletion(-)

再用git status查看状态,能够看到,当前没有须要提交的修改,并且,工做目录是干净(working tree clean)的。:

Jin@Jin-PC MINGW64 ~/learn_git (master)
$ git status
On branch master
nothing to commit, working tree clean

git log能够查看时间从最近到最远的提交日志

Jin@Jin-PC MINGW64 ~/learn_git (master)
$ git log
commit 88cb81a0e46bb1ffae280fd38a26e0112edbdf82 (HEAD -> master)
Author: jin <akipa11@163.com>
Date:   Fri Apr 19 20:16:15 2019 +0800

    del first line

commit ecd1e29aee2a2ee04ffbdf5e4b000e90cc96c83f
Author: jin <akipa11@163.com>
Date:   Fri Apr 19 20:01:31 2019 +0800

    wrote a test file

若是输出信息太多的话,能够加上--pretty=oneline:

Jin@Jin-PC MINGW64 ~/learn_git (master)
$ git log --pretty=oneline
88cb81a0e46bb1ffae280fd38a26e0112edbdf82 (HEAD -> master) del first line
ecd1e29aee2a2ee04ffbdf5e4b000e90cc96c83f wrote a test file

其中,前面的字符为commit id(版本号),最后面的是提交的说明。

若是咱们想把文件退回到初始的版本,那么,应该怎么作呢?

首先,Git必须知道当前版本是哪一个版本,在Git中,用HEAD表示当前版本,也就是最新的提交88cb8...,上一个版本就是HEAD^,上上一个版本就是HEAD^^,固然往上100个版本写100个^比较容易数不过来,因此写成HEAD~100。

版本回退使用命令git reset:

$ git reset --hard HEAD^
HEAD is now at ecd1e29 wrote a test file

Jin@Jin-PC MINGW64 ~/learn_git (master)
$ cat test.txt
Hello World!
Git is a version control system.
I'm learning Git.

能够看到,test.txt文件已经退回到最初的版本了。

可是,咱们用git log查看的话,会发现,以前最新的版本del first line居然没有了!

$ git log
commit ecd1e29aee2a2ee04ffbdf5e4b000e90cc96c83f (HEAD -> master)
Author: jin <akipa11@163.com>
Date:   Fri Apr 19 20:01:31 2019 +0800

    wrote a test file

这时候,若是咱们想退回最新版本就作不到了,若是命令行还没关的话,能够上翻查找到:

$ git log
commit 88cb81a0e46bb1ffae280fd38a26e0112edbdf82 (HEAD -> master)
Author: jin <akipa11@163.com>
Date:   Fri Apr 19 20:16:15 2019 +0800

    del first line

commit ecd1e29aee2a2ee04ffbdf5e4b000e90cc96c83f
Author: jin <akipa11@163.com>
Date:   Fri Apr 19 20:01:31 2019 +0800

    wrote a test file

再用命令git rest退回:

$ git reset --hard 88cb81a0e
HEAD is now at 88cb81a del first line

Jin@Jin-PC MINGW64 ~/learn_git (master)
$ cat test.txt
Git is a version control system.
I'm learning Git.

能够发现,又回到了最新的修改版本。但若是操做了太屡次数的话,以前的可能就被覆盖了,那么,就再也回不到这个版本了。

幸亏,git提供了一个命令git reflog用来记录每一次的命令:

$ git reflog
88cb81a (HEAD -> master) HEAD@{0}: reset: moving to 88cb81a0e
ecd1e29 HEAD@{1}: reset: moving to HEAD^
88cb81a (HEAD -> master) HEAD@{2}: commit: del first line
ecd1e29 HEAD@{3}: commit (initial): wrote a test file

能够看到,使用git reflog能够退回到任何一个版本。

使用git reset --hard命令改变了HEAD指向某个版本后,还能够用git reflog来查看全部版本。

4、Git的撤销修改:

一、还没git add以前:

(1)、查看内容,直接修改文件内容;

(2)、用git status 命令查看状态:

$ 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:   test.txt

no changes added to commit (use "git add" and/or "git commit -a")

能够看到,咱们能够用命令git checkout -- <file>来进行撤销修改:

Jin@Jin-PC MINGW64 ~/learn_git (master)
$ git checkout -- test.txt

二、git add后,还没git commit以前:

$ cat test.txt
Hello World!
Git is d distributed version control system.
Git is free software distributed under the GPL.
Git is a version control system.
I'm learning Git.
ohhhhhh

Jin@Jin-PC MINGW64 ~/learn_git (master)
$ git add test.txt

Jin@Jin-PC MINGW64 ~/learn_git (master)
$ git status
On branch master
Changes to be committed:
  (use "git reset HEAD <file>..." to unstage)

        modified:   test.txt


Jin@Jin-PC MINGW64 ~/learn_git (master)
$ git reset HEAD test.txt
Unstaged changes after reset:
M       test.txt

Jin@Jin-PC MINGW64 ~/learn_git (master)
$ git checkout -- test.txt

Jin@Jin-PC MINGW64 ~/learn_git (master)
$ cat test.txt
Hello World!
Git is d distributed version control system.
Git is free software distributed under the GPL.
Git is a version control system.
I'm learning Git.

Jin@Jin-PC MINGW64 ~/learn_git (master)
$ git status
On branch master
nothing to commit, working tree clean

能够看到,若是咱们已经用命令git add添加文件后,能够前后用命令git reset HEAD <file>和git checkout -- <file>来进行撤销修改。

三、git add 和git commit以后:

能够用命令git reflog查看版本历史,并退回以前版本,前提是没有推送到远程仓库。

5、删除文件

在本地删除一个文件后,用git status查看状态:

$ rm test.txt

Jin@Jin-PC MINGW64 ~/learn_git (master)
$ git status
On branch master
Changes not staged for commit:
  (use "git add/rm <file>..." to update what will be committed)
  (use "git checkout -- <file>..." to discard changes in working directory)

        deleted:    test.txt

no changes added to commit (use "git add" and/or "git commit -a")

能够看到,若是是删错了文件,还能够用命令git checkout -- <file>把误删的文件恢复。

$ ls
LICENSE

Jin@Jin-PC MINGW64 ~/learn_git (master)
$ git checkout -- test.txt

Jin@Jin-PC MINGW64 ~/learn_git (master)
$ ls
LICENSE  test.txt

而若是是想把版本库的文件也同时删除,能够用命令git rm 删除并用git commit提交。

$ git rm test.txt
rm 'test.txt'

Jin@Jin-PC MINGW64 ~/learn_git (master)
$ git commit -m "del test.txt"
[master 9b508c2] del test.txt
 1 file changed, 5 deletions(-)
 delete mode 100644 test.txt

Jin@Jin-PC MINGW64 ~/learn_git (master)
$ git status
On branch master
nothing to commit, working tree clean

Jin@Jin-PC MINGW64 ~/learn_git (master)
$ ls
LICENSE
相关文章
相关标签/搜索