Git的基本配置以及操作
默认你已经安装好了Git,这里使用的是Git-2.30.1-64版本
一、Git本地库的初始化git init
- 命令:git init
安装好Git后,我们可以在任意的磁盘界面或者主界面,点击鼠标右键看到git的快捷键,这里选择Git Bash Here ,打开命令行界面,此时我们即可使用linux命令了哦。Git命令跟linux命令是兼容的。
我们进入到我们Git专用的目录,你可以自己指定哦.创建weChat,名称自行指定,意义:这里假设weChat是我们的项目,我们使用Git来进行版本控制
输入命令:git init
Initialized empty Git repository in D:/Git/GitSpace/weChat/.git/
//这里指,初始化仓库实在目录下的.git目录下。
由于以点开头的目录都是隐藏的,我们可以使用 ll -lA进行查看
ll -lA
total 4
drwxr-xr-x 1 1 197121 0 Mar 14 18:38 .git/
//可以看到 .git 文件夹创建了
此时可以查看 .git 文件夹:
输入命令:ll .git/
显示结果:
total 7
-rw-r--r-- 1 1 197121 23 Mar 14 18:38 HEAD
-rw-r--r-- 1 1 197121 130 Mar 14 18:38 config //配置文件所在文件夹
-rw-r--r-- 1 1 197121 73 Mar 14 18:38 description
drwxr-xr-x 1 1 197121 0 Mar 14 18:38 hooks/
drwxr-xr-x 1 1 197121 0 Mar 14 18:38 info/
drwxr-xr-x 1 1 197121 0 Mar 14 18:38 objects/
drwxr-xr-x 1 1 197121 0 Mar 14 18:38 refs/
注意: .git 目录中存放的是本地库相关的子目录和文件,不要删除,也不要胡乱随意的修改
- 设置签名
形式:
用户名:motcs<自己设定>
Email地址:motcs@qq.com[自己设定,可以与用户名一致,也可以不一致]
作用:区分不同开发人员的身份
这里设置的签名和登录远程库(托管中心)的账号、密码没有任何关系
命令:
项目级别/仓库级别:仅在当前本地库范围内有效
git config user.name motcs
git config user.email motcs@qq.com
系统用户级别:登录当前操作系统的用户范围
git config --global user.name motcs
git config --global user.email nsyy@qq.com
系统优先级:
就近原则:项目级别优先于系统用户级别,二者都有时,采用项目级别的签名。
若只有系统用户级别,则以项目用户级别签名为准。
二者必须存在一个级别的签名,不允许不存在。
我们配置好了信息,怎么确定我们保存的信息,在weChat目录下输入以下命令:cat .git/config
$ cat .git/config
[core]
repositoryformatversion = 0
filemode = false
bare = false
logallrefupdates = true
symlinks = false
ignorecase = true
[user]
name = motcs
email = motcs@qq.com
//可以看到 user 中记录了我们的签名信息
同一步骤,我们再配置下系统级别用户信息。(可以复制上方的语句然后根据自己的进行修改。)
此时我们可以进入到我们系统的家目录:cd ~
输入:ls -lA
查询结果可以看到有这一条记录:
-rw-r--r-- 1 1 197121 119 Mar 14 18:57 .gitconfig
//前边可以不管,只看最后 .gitconfig 就是我们要寻找的了
cat .gitconfig
[user]
name = motcs
email = motcsqq.com
返回本地库目录:
$ git status
On branch master
No commits yet
//没有提交的信息
nothing to commit (create/copy files and use "git add" to track)
//没有什么可以提交的也就是缓存区没有数据
我们可以任意创建一个文本或者是其他文件: vim test.txt 并输入内容,内容随意,这是测试文件。
输入命令: git status
$ git status
On branch master
No commits yet
Untracked files://发现了未追踪的文件
(use "git add <file>..." to include in what will be committed) //可以使用命令放进缓存区
test.txt
nothing added to commit but untracked files present (use "git add" to track)
//没有放入到缓存区数据,但是未追踪的文件存在,使用 git add去追踪他
$ 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 status
On branch master
No commits yet
Changes to be committed:
(use "git rm --cached <file>..." to unstage)
new file: test.txt
如果我们已经放入到缓存区的数据发现不对,或者是又有新添加的数据,我们可以使刚刚提交的回复到之前的状态:
$ git rm --cached test.txt
rm 'test.txt'
$ git status
On branch master
No commits yet
Untracked files:
(use "git add <file>..." to include in what will be committed)
test.txt
nothing added to commit but untracked files present (use "git add" to track)
好了,回复到我们已经放入到缓存区的步骤:git add test.txt
从缓存区提交到本地库: git commit test.txt
# Please enter the commit message for your changes. Lines starting
# with '#' will be ignored, and an empty message aborts the commit.
#
# On branch master
#
# Initial commit
#
# Changes to be committed:
# new file: test.txt
#
在这里主要是提交的一个信息,也就是描述,这次你干了什么事。
输入 :set nu 显示行号,进入到vim编辑器
然后自行操作了。
$ git commit test.txt
warning: LF will be replaced by CRLF in good.txt.
The file will have its original line endings in your working directory
[master (root-commit) 54f1e5c] 消息,这是第一次提交,做的事情是新建一个 test.txt文件
1 file changed, 2 insertions(+)
create mode 100644 test.txt
此时可以查看我们提交文件的内容:
$ cat test.txt
sout("huaning");[自定义内容]
在查看git状态吧
$ git status
On branch master
nothing to commit, working tree clean
我们可以去更新之前的test.txt文件,然后再查看git状态
$ vim test.txt
$ git status
On branch master
Changes not staged for commit:
(use "git add <file>..." to update what will be committed)
(use "git restore <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 -a,即你可以先放入到缓存区,或者是直接提交
整合git常用命令:
-
状态查看 : git status
-
添加操作: git add [文件名] //将文件的“修改/新建" 添加到缓存区
-
提交操作: git commit -m “commit massage[ 备注信息]” [文件名] 。也可以不加 -m,直接git commit [文件名] 然后 进入vim添加注释
-
查看历史记录操作:
-
git log 多屏显示控制方式,空格下翻页,b上翻页,q退出
-
当历史记录较多时,再全部显示不方便:$ git log --pretty=oneline 每一条记录单行显示,显示hash值和注释
-
也可以$ git log --oneline 每一条记录单行显示,显示部分hash值和注释
-
$ git reflog
90bc685 (HEAD -> master) HEAD@{0}: commit: 这是第二次提交
54f1e5c HEAD@{1}: commit (initial): 消息,这是第一次提交,做的事情是新建一个 test.txt文件HEAD@{1}表示回到这个版本需要1步。举一反三,其他见明知意。
-
-
版本的前进后退
-
基于索引值操作【推荐使用】
$ git reset --hard 54f1e5c //54f1e5c 使用git reflog 查看的索引值 HEAD is now at 54f1e5c 消息,这是第一次提交,做的事情是新建一个 test.txt文件 //可以前进和后退
-
使用^符号:git reset --hard HEAD^ //一个^代表退1步.只能后退,不能前进
-
使用~符号:git reset --hard HEAD~2 // ~后的数字,代表后退几步.只能后退,不能前进
-
-
其他的具体操作可以自行查看帮助文档或者百度哦!
不行一步,不践一行。