Git使用心得(一)

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/ron03129596/article/details/54835758

目录

Git使用心得(一)

本文旨在记录自己学习Git的一些体验,同时也分享给大家,使用的为Windows系统:


Windows下安装Git

请同学们自行去Git官网下载安装,安装成功后在CMD命令窗口输入git --version回车后会看到对应的版本信息,
如下:

git安装后版本信息查看

接下来,我们在开始菜单中找到“Git”->“Git Bash”,打开后跳出类似命令窗口的东西,后续我们命令执行工具
都在此东东下面完成

Git命令运行工具

安装完成后还需要最后一步设置,在命令行输入如下命令:
$ git config –global user.name “Your Name”
$ git config –global user.email “[email protected]

因为Git是分布式版本控制系统,所以每个机器都必须自报家门:你的名字和Email地址
注意git config命令的--global参数,这个参数是全局的意思表示你这台机器上的所有Git仓库都会使用这个配置,当然也可
以对某个仓库指定不同的用户名和Email地址  

到这里Git的安装就讲完了,接下来我们来使用Git来做文件版本控制


Git版本库

版本库又名版本仓库,英文名Repository,和SVN等其他版本控制工具一样,就是一个目录,这个目录里面的所有文件
都可以被Git管理起来,每个文件的修改删除Git都可以跟踪,以便在将来还原

创建一个版本库非常简单,简历一个空文件目录,然后在该目录下使用git init命令初始化将该目录变成Git可识别的仓库即可

thinkpad@thinkpad-PC MINGW64 ~
$ mkdir learngit

thinkpad@thinkpad-PC MINGW64 ~
$ cd learngit/

thinkpad@thinkpad-PC MINGW64 ~/learngit
$ pwd
/c/Users/thinkpad/learngit

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

现在Git仓库已经建好了,我们会发现在我们建立的文件夹下面出现了一个隐藏的.git的文件夹,没错这就是仓库初始化后出现的Git配置信息,没事可不要乱动哟。

若你没有看到.git的文件夹也没事,因为该文件夹是隐藏的,使用ls -ah命令即可看见

thinkpad@thinkpad-PC MINGW64 ~/learngit (master)
$ ls -ah
./  ../  .git/

所有的版本控制系统都只能跟踪文本文件的改动,二进制文件无法跟踪其内容的变化,另外Microsoft的Word格式为二进制,故版本控制系统无法进行跟踪其改动。

另使用Microsoft记事本同学的一定要注意:千万不要使用Windows自带的记事本编辑任何文件,原因是Microsoft记
事本团队在每个记事本内容的最前面加入了0xefbbbf(十六进制)的字符,所以你会遇到一些不可思议的问题,比如网页前面多
了个?等等

Git文件操作-1

下面我们在已经初始化好的learngit仓库下创建a.txt文件,内容如下

Git is a version control system.
Git is free software.
  • 使用git add命令将文件添加到仓库
thinkpad@thinkpad-PC MINGW64 ~/learngit/testFiles (master)
$ git add a.txt
warning: LF will be replaced by CRLF in testFiles/a.txt.
The file will have its original line endings in your working directory.
注:出现如上警告原因为windows下的换行符为CRLF,而linux下的换行符为LF,在windows下使用git bash提
交将会出现如下情况,后续如经常出现不用刻意去管
  • 使用git commit命令将文件提交到仓库
thinkpad@thinkpad-PC MINGW64 ~/learngit/testFiles (master)
$ git commit -m "第一次使用提交命令备注"
[master (root-commit) 4f03f9e] 第一次使用提交命令备注
warning: LF will be replaced by CRLF in testFiles/a.txt.
The file will have its original line endings in your working directory.
 1 file changed, 2 insertions(+)
 create mode 100644 testFiles/a.txt
注:git commit -m "本次提交备注",-m 所带参数为提交说明

现在我们运行git status命令来查看下当前版本库的状态

thinkpad@thinkpad-PC MINGW64 ~/learngit/testFiles (master)
$ git status
On branch master
nothing to commit, working tree clean
注:该提示告诉我们没有需要提交的修改,而且工作目录是干净的

git add可以多次添加文件,而git commit一次可以提交很多文件,所以可以多次add不同的文件

thinkpad@thinkpad-PC MINGW64 ~/learngit/testFiles (master)
$ git add file1.txt
$ git add file2.txt file3.txt
$ git commit -m "commit 3 files."

小结

初始化一个Git仓库,使用git init命令。
添加文件至Git仓库,分两步:
- 1、使用git add 添加文件,可反复使用
- 2、使用git commit 提交,完成

Git文件操作-2

我们已经成功地添加了一个a.txt文件至仓库了,现在我们继续工作,修改a.txt内容成如下:
Git is a distributed version control system.
Git is free software

现在运行git status命令继续查看结果

thinkpad@thinkpad-PC MINGW64 ~/learngit/testFiles (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:   a.txt

no changes added to commit (use "git add" and/or "git commit -a")
注:上面的命令告诉我们.文件已经被修改过了,但还没有准备提交的修改。

我们有些时候想查看修改了那些内容,那么就可以执行git diff命令,如下:

thinkpad@thinkpad-PC MINGW64 ~/learngit/testFiles (master)
$ git diff
diff --git a/testFiles/a.txt b/testFiles/a.txt
index 46d49bf..9247db6 100644
--- a/testFiles/a.txt
+++ b/testFiles/a.txt
@@ -1,2 +1,2 @@
-Git is a version control system.
+Git is a distributed version control system.
 Git is free software.
warning: LF will be replaced by CRLF in testFiles/a.txt.
The file will have its original line endings in your working directory.
注:上述代码-号部分为老的内容,+号部分为新的内容,没有符号为老的内容

知道了修改的具体内容后我们就可以安心的提交代码了,那么提交修改和提交新增文件同样的是两步,第一步是git add,第二步为git commit ,下面我们看下执行第一步后使用git status命令查看版本库状态

thinkpad@thinkpad-PC MINGW64 ~/learngit/testFiles (master)
$ git status
warning: LF will be replaced by CRLF in testFiles/a.txt.
The file will have its original line endings in your working directory.
On branch master
Changes to be committed:
  (use "git reset HEAD <file>..." to unstage)

        modified:   a.txt
注:上述说明将要被提交的修改包括a.txt文件,下一步就可以放心的执行第二步进行提交了

下面执行第二步提交操作

thinkpad@thinkpad-PC MINGW64 ~/learngit/testFiles (master)
$ git commit -m "修改a文件内容后再次commit提交"
[master warning: LF will be replaced by CRLF in testFiles/a.txt.
The file will have its original line endings in your working directory.
88093bf] 修改a文件内容后再次commit提交
warning: LF will be replaced by CRLF in testFiles/a.txt.
The file will have its original line endings in your working directory.
 1 file changed, 1 insertion(+), 1 deletion(-)
注:以上命令就是提交后的输出信息

我们再次执行git status命令来查看当前版本库的状态

thinkpad@thinkpad-PC MINGW64 ~/learngit/testFiles (master)
$ git status
On branch master
nothing to commit, working tree clean
注:以上输出指当前工版本库没有需要提交的文件,工作目录是干净的

在开发中我们经常提交修改文件,某一天你忘了你上次提交的内容是什么,那么日志查看命令就显得尤为重要了,命令及输出如下:

thinkpad@thinkpad-PC MINGW64 ~/learngit/testFiles (master)
$ git log
commit 92ba36d082e2623e748b6c5f88c15681e4c5710b
Author: renjj <renjj@zcxny.cn>
Date:   Wed Feb 1 14:47:00 2017 +0800

    第三次修改提交备注

commit 88093bf8dbddc28ccd045f0c32cdf37ac238d819
Author: renjj <renjj@zcxny.cn>
Date:   Wed Feb 1 14:40:16 2017 +0800

    修改a文件内容后再次commit提交

commit 4f03f9ee4018a309d9b3196a1780f6a950098f6f
Author: renjj <renjj@zcxny.cn>
Date:   Tue Jan 31 19:50:56 2017 +0800

    第一次使用提交命令备注
注:如果觉得输出太多眼花缭乱,可以加上 --pretty=oneline参数,其输出如下:
thinkpad@thinkpad-PC MINGW64 ~/learngit/testFiles (master)
$ git log --pretty=oneline
92ba36d082e2623e748b6c5f88c15681e4c5710b 第三次修改提交备注
88093bf8dbddc28ccd045f0c32cdf37ac238d819 修改a文件内容后再次commit提交
4f03f9ee4018a309d9b3196a1780f6a950098f6f 第一次使用提交命令备注
注:一大串字符是指commitid,因Git为分布式版本控制原因故不能使用SVN中的1,2,3之类的数字,而是以SHA1算法算出的数字
以16进制表示,每提交一个版本,Git则会讲它们自动串成一条线

小结

  • 1、要随时掌握工作区的状态,使用git status命令
  • 2、如果git status告诉你有文件修改过,可以使用git diff查看修改内容
  • 3、若想查看提交过的记录则可以使用git log查看日志记录

现在你已经学会了提交修改到版本库,现在再练习一次,修改a.txt内容如下,然后尝试提交:
Git is a distributed version control system.
Git is free software distributed under teh GPL.

Git文件操作-3

下面我们回退到上一个版本,使用git reset命令,上一个版本是HEAD^,上上个版本为HEAD^^,上100个版本为HEAD~100

thinkpad@thinkpad-PC MINGW64 ~/learngit/testFiles (master)
$ git reset --hard HEAD^
HEAD is now at 88093bf 修改a文件内容后再次commit提交
注:--hard有何作用暂时不做说明,先加上,后面再讲解,现在查看内容,你会发现果然回退到上一个版本了
thinkpad@thinkpad-PC MINGW64 ~/learngit/testFiles (master)
$ cat a.txt
Git is a distributed version control system.
Git is free software.

接下来我们继续使用git log信息查看版本库提交日志

thinkpad@thinkpad-PC MINGW64 ~/learngit/testFiles (master)
$ git log
commit 88093bf8dbddc28ccd045f0c32cdf37ac238d819
Author: renjj <renjj@zcxny.cn>
Date:   Wed Feb 1 14:40:16 2017 +0800

    修改a文件内容后再次commit提交

commit 4f03f9ee4018a309d9b3196a1780f6a950098f6f
Author: renjj <renjj@zcxny.cn>
Date:   Tue Jan 31 19:50:56 2017 +0800

    第一次使用提交命令备注

果然,已经找不到最新的那个版本日志信息了,但这时候你又想再回到最新的版本,怎么办?办法还是有的,只要上个命令行窗口还没有被关掉,你就可以顺着往上找到那个最新的commit id,于是就可以指定回退到未来的某个版本。

thinkpad@thinkpad-PC MINGW64 ~/learngit/testFiles (master)
$ git reset --hard 92ba36d082e(此处只写一部分即可,Git会自动去匹配)
HEAD is now at 92ba36d 第三次修改提交备注
然后查看内容和日志发现又回来了,是不是很神奇呢?

现在你关掉电脑休息了会,发现我又想回到另外一个版本,这时候找不到commit id该怎么弄呢?Git提供了一个命令git reflog用来记录你每一次命令:

thinkpad@thinkpad-PC MINGW64 ~/learngit/testFiles (master)
$ git reflog
92ba36d HEAD@{0}: reset: moving to 92ba36d082e
88093bf HEAD@{1}: reset: moving to HEAD^
92ba36d HEAD@{2}: commit: 第三次修改提交备注
88093bf HEAD@{3}: commit: 修改a文件内容后再次commit提交
4f03f9e HEAD@{4}: commit (initial): 第一次使用提交命令备注
注:最前面则是commit id,拿到commit id我们就能够随意回退到指定版本了

小结

  • 1、HEAD指向的版本就是当前版本,因此,Git可以让我们任意退进,使用命令git reset –hard commit_id 即可
  • 2、回退前使用git log可以查看历史,以便确认要回退哪个版本
  • 3、要返回到未来可以使用git reflog查看命令历史,以便确定要回退的的版本

本人也在学习阶段,如写得有误的地方,请朋友们指出及时改正

猜你喜欢

转载自blog.csdn.net/ron03129596/article/details/54835758