Git 使用 + 命令

设置

$ git config --global user.name "Your Name"
$ git config --global user.email "email@example.com"
name = Fristname Lastname
email = your_email@example.com

git config命令的–global参数,这个参数,表示你这台机器上所有的Git仓库都会使用这个配置


创建文件夹

$ mkdir test
$ cd test
$ pwd

分别为
创建一个叫test的文件夹(中文名Windows系统可能会出现各种莫名其妙的问题)
切换到当前文件夹中
显示当前路径


把这个目录作为Git可管理的仓库

$ git init

必须切换到当前目录


添加文件

编写一个test.txt文件,内容为 (放到test目录下)

ABCDEFD

添加文件 git add

$ git add test.txt

执行之后是没有反应就是成功了

然后commit

$ git commit -m "write a test"
[master (root-commit) e3f073c] write a test
 1 file changed, 1 insertion(+)
 create mode 100644 test.txt

-m后面输入的是本次提交的说明,可以输入任意内容
git commit命令执行成功后会告诉你,1个文件被改动(我们新添加的test.txt文件),插入了一行内容(test.txt有一行内容)。

这样就提交到仓库了。


查看当前状态和不同

修改test.txt内容为(建议使用notepad++)

ABCDEFG

查看当前状态 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 status命令可以让我们时刻掌握仓库当前的状态,上面的命令告诉我们,test.txt被修改过了,但还没有准备提交的修改。

查看不同 git diff

$ git diff
diff --git a/test.txt b/test.txt
index 5ee15cd..325c6c6 100644
--- a/test.txt
+++ b/test.txt
@@ -1 +1 @@
-ABCDEFD
\ No newline at end of file
+ABCDEFG
\ No newline at end of file
$ git diff test.txt
diff --git a/test.txt b/test.txt
index 5ee15cd..325c6c6 100644
--- a/test.txt
+++ b/test.txt
@@ -1 +1 @@
-ABCDEFD
\ No newline at end of file
+ABCDEFG
\ No newline at end of file

两种方法都可以
查看两次的不同,显示的格式是Unix通用的diff格式


提交文件

提交文件 git add

git add test.txt

再次查看当前状态 git status

$ git status
On branch master
Changes to be committed:
  (use "git reset HEAD <file>..." to unstage)

        modified:   test.txt

意思为将要被提交的修改包括test.txt
可以放心地提交了

$ git commit -m "add G"
[master 9859fc8] add G
 1 file changed, 1 insertion(+), 1 deletion(-)

然后查看当前状态

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

Git告诉我们当前没有需要提交的修改,而且,工作目录是干净(working tree clean)的。


还原版本

再次修改test.txt文件为

ABCDEFGH

尝试提交:

$ git add test.txt

$ git commit -m "add H"
[master 71c56fd] add H
 1 file changed, 1 insertion(+), 1 deletion(-)

现在在Git仓库里有许多版本,可以用 git log 查看:

$ git log
commit 71c56fdced4403930c9238334ab11c48e40c39c6 (HEAD -> master)
Author: somliy <1187525390@qq.com>
Date:   Fri Jan 5 19:47:03 2018 +0800

    add H

commit 9859fc8aeec24ce0115af6e016d0b4b1807fd840
Author: somliy <1187525390@qq.com>
Date:   Fri Jan 5 19:40:51 2018 +0800

    add G

commit e3f073c77ed6fbcfc2fb03f05215ecf40a076e39
Author: somliy <1187525390@qq.com>
Date:   Fri Jan 5 19:20:44 2018 +0800

    write a test

有创建,添加G,添加H,几个历史版本

如果嫌输出信息太多,看得眼花缭乱的,可以试试加上–pretty=oneline参数

$ git log --pretty=oneline
71c56fdced4403930c9238334ab11c48e40c39c6 (HEAD -> master) add H
9859fc8aeec24ce0115af6e016d0b4b1807fd840 add G
e3f073c77ed6fbcfc2fb03f05215ecf40a076e39 write a test

前面一大堆字符串是commit id(16进制的版本号)

现在我们退回上一个版本

ABCDEFG

使用 git reset

$ git reset --hard HEAD^
HEAD is now at 9859fc8 add G

–hard参数有啥意义?这个后面再讲,现在你先放心使用。
HEAD相当于一个指针,上一个版本就是HEAD^,上上一个版本就是HEAD^^,当然往上100个版本写100个^比较容易数不过来,所以写成HEAD~100。

先看一下内容:

$ cat test.txt
ABCDEFG

已经没有H了,现在可以用 git log 看以前的版本,已经不是3个了

$ git log --pretty=oneline
9859fc8aeec24ce0115af6e016d0b4b1807fd840 (HEAD -> master) add G
e3f073c77ed6fbcfc2fb03f05215ecf40a076e39 write a test

也可以使用commit id退回制定版本:

$ git reset --hard e3f073c
HEAD is now at e3f073c write a test

$ cat test.txt
ABCDEFD

当你用$ git reset –hard HEAD^回退到add G版本时,再想恢复到add H,就必须找到add H的commit id。Git提供了一个命令git reflog用来记录你的每一次命令:

$ git reflog
e3f073c (HEAD -> master) HEAD@{0}: reset: moving to e3f073c
9859fc8 HEAD@{1}: reset: moving to 9859
9859fc8 HEAD@{2}: reset: moving to HEAD^
71c56fd HEAD@{3}: commit: add H
9859fc8 HEAD@{4}: commit: add G
e3f073c (HEAD -> master) HEAD@{5}: commit (initial): write a test

版本库

工作区有一个隐藏目录.git,这个不算工作区,而是Git的版本库。

Git的版本库里存了很多东西,其中最重要的就是称为stage(或者叫index)的暂存区,还有Git为我们自动创建的第一个分支master,以及指向master的一个指针叫HEAD。

这里写图片描述

我们把文件往Git版本库里添加的时候,是分两步执行的:

第一步是用git add把文件添加进去,实际上就是把文件修改添加到暂存区;

第二步是用git commit提交更改,实际上就是把暂存区的所有内容提交到当前分支。

因为我们创建Git版本库时,Git自动为我们创建了唯一一个master分支,所以,现在,git commit就是往master分支上提交更改。


多次改动分别添加,一次提交或者多次提交

比如我们再两次修改文件内容

ABCDEFD
E
$ git add test.txt
$ git status
On branch master
Changes to be committed:
  (use "git reset HEAD <file>..." to unstage)

        modified:   test.txt


ABCDEFD
EF
$ git commit -m "add F"
[master 07605be] add F
 1 file changed, 2 insertions(+), 1 deletion(-)
$ 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 add -> 第二次修改 -> git commit

你看,我们前面讲了,Git管理的是修改,当你用git add命令后,在工作区的第一次修改被放入暂存区,准备提交,但是,在工作区的第二次修改并没有放入暂存区,所以,git commit只负责把暂存区的修改提交了,也就是第一次的修改被提交了,第二次的修改不会被提交。


撤销修改

$ git checkout -- test.txt

命令git checkout – test.txt意思就是,把readme.txt文件在工作区的修改全部撤销,这里有两种情况:

一种是readme.txt自修改后还没有被放到暂存区,现在,撤销修改就回到和版本库一模一样的状态;

一种是readme.txt已经添加到暂存区后,又作了修改,现在,撤销修改就回到添加到暂存区后的状态。

总之,就是让这个文件回到最近一次git commit或git add时的状态。


删除文件

$ rm test.txt

这个时候,Git知道你删除了文件,因此,工作区和版本库就不一致了
现在你有两个选择,
一是确实要从版本库中删除该文件,那就用命令git rm删掉,并且git commit:

$ git rm test.txt
rm 'test.txt'
$ git commit -m "remove test.txt"
[master d17efd8] remove test.txt
 1 file changed, 1 deletion(-)
 delete mode 100644 test.txt

另一种情况是删错了,因为版本库里还有呢,所以可以很轻松地把误删的文件恢复到最新版本:

$ git checkout -- test.txt
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值