一、安装git
-
Linux下安装
sudo apt-get install git //即可
-
windows下安装
直接到官网下载镜像 传送门下载安装完第一步,配置用户名和email
git config --global user.name "solicucu" git config --global user.email "2748847613@qq.com"
注意git config命令的–global参数,用了这个参数,表示你这台机器上所有的Git仓库都会使用这个配置,当然也可以对某个仓库指定不同的用户名和Email地址。
一些小意外:
如果你设置错了用户名或者想改修改,要怎么办呢,小编是从新在git bash 上面从新配置就行了
可以通过命令 git config --list 来查看配置结果
warning: user.name has multiple values
error: cannot overwrite multiple values with a single value,如果出现了这种错误,可以用一下命令修改:
git config --global --replace-all user.name “solicucu”
二、创建版本库和添加文件
-
随便找一个方便一点的位置,新建一个文件夹
linux下可以用指令mkdir respository git init //初始化版本库
-
新建一个文本
假定有一个文本test1.txt,可以通过vi指令编辑 -
提交文本
git add test1.txt git add test2.txt test3.txt //可以提交多个文件 git commit -m "explaintion" //-m 带的参数是提交修改的描述
commit成功之后会有提示,但是git add 如果成功是没有任何消息提示的
三、git常用的指令
1、git status //检查当前工作目录是否有变动
当你准备提交新的文件到版本库的时候,可能忘了已经改动了哪里,那么就可以用这个指令来查看是否有修改
例如我修改了test2.txt ,但是还没有提交,执行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: test2.txt //指明了被修改的文件
no changes added to commit (use "git add" and/or "git commit -a")
//可以看到会提醒有文件被修改了,但是还没有被提交
2、git diff //查看修改的地方
$ git diff test2.txt
diff --git a/test2.txt b/test2.txt
index df97e8b..0ccba71 100644
--- a/test2.txt
+++ b/test2.txt
@@ -1 +1,2 @@
hello chacha!
+hello jun ! //这里看到修改的地方
warning: LF will be replaced by CRLF in test2.txt.
The file will have its original line endings in your working directory.
//按照前面的提交之后,再检查一下当前工作目录发现是干净的
$ git status
On branch master
nothing to commit, working directory clean
3、git log //查看版本库所有的提交版本
由于某次修改,把文件改坏了,那么想测回到上一个版本库,那怎么办呢,所以能够回退版本就很有必要了
$ git log
commit c29613dca2e61ec61146397fcf4fe58ea056cecd
Author: solicucu <2748847613@qq.com>
Date: Fri Feb 15 17:10:26 2019 +0800
modify test2.txt
commit f012faf6d76d716a6b866bcf180c43cd4b90c90a
Author: solicucu <2748847613@qq.com>
Date: Fri Feb 15 16:53:25 2019 +0800
add test2.txt
commit 4fa05850dcbea2adb6a89952d75674a82b57d0bf
Author: solicucu <2748847613@qq.com>
Date: Fri Feb 15 16:51:15 2019 +0800
add two file just now
如果想看简单一点的,可以用下面的指令
$ git log --pretty=oneline
c29613dca2e61ec61146397fcf4fe58ea056cecd modify test2.txt
f012faf6d76d716a6b866bcf180c43cd4b90c90a add test2.txt
4fa05850dcbea2adb6a89952d75674a82b57d0bf add two file just now
可以看到到目前为止有三个版本,有具体的版本号id,作者,以及提交日期,提交说明。这个是按最新到最旧的顺序排列的
4、git reset --hard head^ // 回退到上一个版本,两个^就是上上个版本,100个就用head~100
//回退之前
$ git reset --hard head
HEAD is now at c29613d modify test2.txt
little jun@DESKTOP-9I2JAHT MINGW64 /d/repository/repository (master)
$ cat test2.txt
hello chacha!
hello jun !
//回退之后
little jun@DESKTOP-9I2JAHT MINGW64 /d/repository/repository (master)
$ git reset --hard head^
HEAD is now at f012faf add test2.txt
little jun@DESKTOP-9I2JAHT MINGW64 /d/repository/repository (master)
$ cat test2.txt
hello chacha!
//如果我突然又后悔回退版本了,那怎么也可以通过id号更新到最新的版本,注意id号只要前面一部分差不多就可以了
$ git reset --hard c29613dc
HEAD is now at c29613d modify test2.txt
git reset 其他参数的一些介绍
-q, --quiet be quiet, only report errors
--mixed reset HEAD and index
--soft reset only HEAD
--hard reset HEAD, index and working tree
--merge reset HEAD, index and working tree
--keep reset HEAD but keep local changes
-p, --patch select hunks interactively
-N, --intent-to-add record only the fact that removed paths will be added later
5、git reflog //查看所有更改过的版本号的id
$ git reflog
c29613d HEAD@{
0}: reset: moving to c29613dc
f012faf HEAD@{
1}: reset: moving to head^
c29613d HEAD@{
2}: commit: modify test2.txt
f012faf HEAD@{
3}: commit: add test2.txt
4fa0585 HEAD@{
4}: commit (initial): add two file just now
如此一来,那再也不怕,回退版本后,又不记得新版本号的id带来的麻烦了
注意一下,–hard 回退版本后,工作目录的所有相关文件,都会回退到修改为跟版本库一样
工作区和暂存区的理解
在我们的版本库文件夹里面有一个.git 文件夹,这个就是我们的版本库
Git的版本库里存了很多东西,其中最重要的就是称为stage(或者叫index)的暂存区,还有Git为我们自动创建的第一个分支master,以及指向master的一个指针叫HEAD。
而工作区就是.git 之外的所有文件夹或者文件
我们每次git add 的文件就是放到暂存区里面,一旦git commit了之后,就会把当前暂存区所有的内容提交到当前分支
如果当前的工作区有文件还没有添加到暂存区,用git status指令可以发现 有 untracked files,提示你git add
$ git status
On branch master
Untracked files:
(use "git add <file>..." to include in what will be committed)
test3