《Git创建本地仓库、提交和查看文件》

版权声明:本文为博主原创文章,未经博主允许不得转载,博客地址:http://blog.csdn.net/mars_xiaolei。 https://blog.csdn.net/mars_xiaolei/article/details/83069802

按win键进入开始菜单,选择Git Bash,打开:

1、创建本地仓库 

$ mkdir /E/Git

2、进入仓库

$ cd /E/Git

此时,命令行的前一行会改变,由 ~/Git更改为/E/Git

//更改前的标题头
dell@DESKTOP-BJG7JVO MINGW64 ~/Git
//更改后的标题头
dell@DESKTOP-BJG7JVO MINGW64 /E/Git

3、显示当前路径

$ pwd

显示结果:

/E/Git

4、初始化空Git存储库

$ git init

显示结果:

Initialized empty Git repository in E:/Git/.git/

5、添加文件到Git存储库

(1)在Git存储库路径(/E/Git)下新建一个txt文本文档,注意必须是在Git存储库路径创建,不然Git找不到,可以自己试试,添加内容如下:

Git is a distributed version control system.
Git is free software distributed under the GPL.
Git has a mutable index called stage.
Git tracks changes of files.

(2)使用git add将文件添加到仓库

$ git add readme.txt

(3)使用git commit将文件提交到仓库

$ git commit -m "wrote a readme file"

因为我没有之前没有配置Git账户用于验证身份(identity),因此,需要先配置一下账户:

//弹出的配置账户提示信息
*** Please tell me who you are.

Run

  git config --global user.email "[email protected]"
  git config --global user.name "Your Name"

to set your account's default identity.
Omit --global to set the identity only in this repository.

fatal: unable to auto-detect email address (got 'dell@DESKTOP-BJG7JVO.(none)')

提示信息给出了配置账户的命令,需要设置邮箱和用户名:

git config --global user.email "[email protected]"
git config --global user.name "Your Name"

这个是配置全局账户,如果需要配置单独账户,需要使用以下命令:

git config user.email "[email protected]"
git config user.name "Your Name"

配置全局账户和配置单独账户具体分析,请看这篇博文:https://blog.csdn.net/coco_wonderful/article/details/51822143

配置完账户之后,再次执行步骤(3),如果之前已配置账户,那可能就不需要了,显示结果如下:

$ git commit -m "wrote a readme file"
[master (root-commit) 95b4579] wrote a readme file
 1 file changed, 4 insertions(+)
 create mode 100644 readme.txt

注:git commit命令,-m后面输入的是本次提交的说明,可以输入任意内容,当然最好是有意义的,这样你就能从历史记录里方便地找到改动记录。

commit还可以一次提交多个文件:

$ git add file1.txt
$ git add file2.txt
$ git add file3.txt
$ git commit -m "add 3 files."

6、查看Git存储库当前状态

(1)修改readme.txt文件,修改内容如下:

Git is a distributed version control system.
Git is free software.

(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:   readme.txt

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

这提示的意思是,文档已被修改过但是还没有提交(Changes not staged for commit)

7、使用git diff命令查看不同

$ git diff readme.txt

我的竟然没有显示,凉凉

8、使用git log命令查看历史记录

$ git log

显示结果:

commit f4b7b4328a11dcae2e38e4e361c6bb94ef80dd11 (HEAD -> master)
Author: xxxxxx  xxxxxxxxxxxxxxxxxxxxxx
Date:   Tue Oct 16 10:45:25 2018 +0800

    wrote a readme file

9、使用git reset命令回退到上一个版本

$ git reset --hard HEAD^

显示结果:

HEAD is now at 95b4579 wrote a readme file

10、使用cat命令查看文件

$ cat readme.txt

因为我回退了一次,所以内容变成了未修改的:

Git is a distributed version control system.
Git is free software distributed under the GPL.
Git has a mutable index called stage.
Git tracks changes of files.

11、使用git reflog查看使用过的命令

$ git reflog

显示结果:

95b4579 (HEAD -> master) HEAD@{0}: reset: moving to HEAD^
f4b7b43 HEAD@{1}: commit: wrote a readme file
95b4579 (HEAD -> master) HEAD@{2}: commit (initial): wrote a readme file

前面的数字是commit id。知道commit id可以回退上一次执行的命令,回退命令为git reset --hard <commit id>

参考博文1:https://www.cnblogs.com/lixiaolun/p/4360732.html

参考博文2:https://blog.csdn.net/coco_wonderful/article/details/51822143

猜你喜欢

转载自blog.csdn.net/mars_xiaolei/article/details/83069802