Git基础:基础命令练习
平台选择
由于大部分开发者日常使用的是Windows
平台进行代码的开发,因此本文采用Windows
平台作为演示。需要注意的是:如果遇见莫名其妙的问题,请确保目录名(包括父目录)不包含中文
安装Git
Linux
Debian
或者Ubuntu
系统,通过sudo apt-get install git
即可完成安装。
老一点的版本可能需要将命令改为sudo apt-get install git-core
不在官方文档当中的Linux
版本,可以通过编译源码安装,步骤如下:
- 从Git官网下载源码,地址:mirrors.edge.kernel.org/pub/softwar…
- 解压,并进入文件夹对应的文件夹:
cd git-x.xx.xx
- 依次输入
./config
、make
、sudo make install
等待编译安装完成即可
Git安装官方文档:git-scm.com/download/li…
Windows
直接从官网下载安装程序即可:Git - Downloads (git-scm.com)
敲击键盘Win+R
输入cmd
并且回车,打开命令行窗口,在其中输入git version
看到如下信息即说明安装成功。
C:\>git version
git version 2.28.0.windows.1
安装之后
安装完成后,还需要设置自己的用户名和邮箱地址,在命令行输入:
$ git config --global user.name "jiafu"
$ git config --global user.email "jiafu@example.com"
因为Git是分布式的版本控制系统,因此在多人协同合作的时候,知道每个人的身份就很重要了。在向远程仓库push
的时候,会带上个人的身份信息。
基础命令
创建仓库
创建一个空的仓库非常简单,创建一个空的文件夹,然后进入文件夹,输入git init
即可。
E:\>mkdir repository
E:\>cd repository
E:\repository>git init
Initialized empty Git repository in E:/repository/.git/
看到最后输出信息,即说明已成功创建仓库。
新增文件
我们在repository
中新建一个文本文件example.txt
,内容如下
This is the original content.
在命令行中输入git status
,可以看到工作区中文件的变化:
E:\repository>git status
On branch master
No commits yet
Untracked files:
(use "git add <file>..." to include in what will be committed)
example.txt
nothing added to commit but untracked files present (use "git add" to track)
输出结果告诉我们,现在工作区中有一个文件example.txt
未被跟踪,也就是未加入到暂存区的意思。
我们通过git add
命令将工作区的文件添加到暂存区中:
E:\repository>git add example.txt
执行结束后没有任务提示,没有提示就说明没有报错信息,那就是好消息。
再次输入git status
查看仓库状态:
E:\repository>git status
On branch master
No commits yet
Changes to be committed:
(use "git rm --cached <file>..." to unstage)
new file: example.txt
可以看到暂存区中新增了一个文件,需要被提交。意为我们之前在工作区中新增的文件,已经成功添加到暂存区当中。
现在我们可以通过git commit
命令,将暂存区中的内容提交到版本库当中,形成一个稳定的版本。
E:\repository>git commit -m "write the first line"
[master 52c28ee] write the first line
1 file changed, 1 insertion(+)
create mode 100644 example.txt
通过-m
参数来指定本次commit
的描述信息。
总结,通过git add
和git commit
命令将文件从工作区添加到暂存区,并最终保存至版本库。
修改文件
紧接上一节,我们在完成一次commit
之后,查看一下仓库状态:
E:\repository>git status
On branch master
nothing to commit, working tree clean
可以看到,我们当前处于master
分支,没有任何文件发生变化。
我们在example.txt
里新增一行,完整内容如下:
This is the original content.
Add a new line.
再次查看仓库状态:
E:\repository>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: example.txt
no changes added to commit (use "git add" and/or "git commit -a")
这次提示,有一个被修改的文件example.txt
,需要被add
到暂存区当中。
通过git add
和git commit
将修改后的文件添加到版本库当中。
E:\repository>git add example.txt
E:\repository>git status
On branch master
Changes to be committed:
(use "git restore --staged <file>..." to unstage)
modified: example.txt
E:\repository>git commit -m "add a new line"
[master cd1653b] add a new line
1 file changed, 2 insertions(+), 1 deletion(-)
可以看到新增文件和修改文件的步骤几乎是一样的,那他们之间有什么区别呢?
我们回顾一下两个的操作步骤会发现,无论是新增文件还是修改文件,在通过git status
查看仓库状态的时候,都会显示工作区与暂存区的文件不一致,而git add
命令是将新增或修改后的文件添加到暂存区,而git commit
命令才是真正将暂存区的内容提交到版本库,形成一次完整的版本内容。也就是说,我们可以多次修改文件,多次通过git add
命令将其添加进暂存区,最后只需要一次git commit
命令就行了,而不用每次都执行git add
和git commit
两条命令。
版本回退
在经过了两次commit
命令之后,应该形成了两次版本,我们可以通过git log
命令来查看历史提交信息:
E:\repository>git log
commit cd1653b833037aa0adfdb16d4b176e07731991a3 (HEAD -> master)
Author: jiafu <jiafu@example.com>
Date: Sat May 8 13:43:34 2021 +0800
add a new line
commit eb0e9e3f7fd6e06c2224cad22bf73b8b7f79f1ca
Author: jiafu <jiafu@example.com>
Date: Sat May 8 13:36:05 2021 +0800
write the first line
可以看到两次提交的版本号、作者信息、提交时间和提交描述信息。
我们可以通过增加--oneline
参数,来简化提交日志内容:
E:\repository>git log --oneline
cd1653b (HEAD -> master) add a new line
eb0e9e3 write the first line
假设我们发现新增的那一行有问题,不想要最新的这一次commit
,想要将版本回退到之前的一个版本,可以通过git reset
命令来进行版本的回退。
E:\repository>git reset --hard HEAD^
More? ^
HEAD is now at eb0e9e3 write the first line
查看example.txt
中的内容,发现已经回退到之前的版本了
This is the original content.
查看提交记录:
E:\repository>git log
commit eb0e9e3f7fd6e06c2224cad22bf73b8b7f79f1ca (HEAD -> master)
Author: chenjiafu <442398967@qq.com>
Date: Sat May 8 13:36:05 2021 +0800
write the first line
也只能看到第一次的提交了,说明版本已经被成功回退了。
如果回退之后,发现第二次提交的内容还是有点用的,后悔了,有没有后悔药可以吃呢?
有的!只要命令行窗口没有关闭,我们向上查看之前的git log
命令输出的第二次提交的内容,拿到提交的版本号之后,可以通过git reset
命令再次重置到第二次提交的版本。
E:\repository>git reset --hard cd1653b
HEAD is now at cd1653b add a new line
E:\repository>git log
commit cd1653b833037aa0adfdb16d4b176e07731991a3 (HEAD -> master)
Author: chenjiafu <442398967@qq.com>
Date: Sat May 8 13:43:34 2021 +0800
add a new line
commit eb0e9e3f7fd6e06c2224cad22bf73b8b7f79f1ca
Author: chenjiafu <442398967@qq.com>
Date: Sat May 8 13:36:05 2021 +0800
write the first line
输入版本号的时候,只需要输入前几位即可,git
会自动定位到具体的提交版本。
如果恰好把命令行窗口关闭了,没有之前的提交版本号了,还能再次后悔吗?
能!git
提供了一个命令git reflog
用来记录每一次命令:
E:\repository>git reflog
cd1653b (HEAD -> master) HEAD@{0}: reset: moving to cd1653b
eb0e9e3 HEAD@{1}: reset: moving to HEAD^
e4511b1 HEAD@{2}: commit: new line
eb0e9e3 HEAD@{3}: commit (initial): write the first line
关于
git reset --hard HEAD^
的解释:
- HEAD 表示当前版本
- HEAD^ 上一个版本
- HEAD^^ 上上一个版本
- HEAD^^^ 上上上一个版本
- 以此类推...
可以使用 ~数字表示
- HEAD~0 表示当前版本
- HEAD~1 上一个版本
- HEAD^2 上上一个版本
- HEAD^3 上上上一个版本
- 以此类推...
删除文件
我们先添加一个文件newFile.txt
到Git
当中:
E:\repository>git add newFile.txt
E:\repository>git commit -m "new file"
[master 032eb51] new file
1 file changed, 0 insertions(+), 0 deletions(-)
create mode 100644 newFile.txt
按照一般的习惯,我们通常直接在文件管理器中将文件删除了。
现在,我们假设已经删除了文件。也就是工作区中已经没有newFile.txt
了。
查看仓库信息:
E:\repository>git status
On branch master
Changes not staged for commit:
(use "git add/rm <file>..." to update what will be committed)
(use "git restore <file>..." to discard changes in working directory)
deleted: newFile.txt
no changes added to commit (use "git add" and/or "git commit -a")
可以看到,由于工作区和暂存区中的文件不对应,Git
会提示我们有一个文件在工作区中已经删除了,但在暂存区中仍然存在。
出现这种场景的时候,有两种情况:一种是我们要从版本库中删除文件;另外一种是我们误删了,需要恢复文件。
恢复文件
恢复文件很简单,因为暂存区和版本库里都还有呢,我们只需要将暂存区或者版本库中的文件恢复到工作区即可。
用git checkout
命令可以从暂存区和版本库(若暂存区没有)中恢复文件到工作区中:
E:\repository>git checkout -- newFile.txt
Updated 1 path from the index
删除文件
使用git rm
命令可以从暂存区中将已索引的文件删除:
E:\repository>git rm newFile.txt
rm 'newFile.txt'
E:\repository>git commit -m "delete new file"
[master 1cb2227] delete new file
1 file changed, 0 insertions(+), 0 deletions(-)
delete mode 100644 newFile.txt
E:\repository>git log --oneline
1cb2227 (HEAD -> master) delete new file
032eb51 new file
cd1653b add a new line
eb0e9e3 write the first line
这样,便从版本库中完全删除了newFile.txt
文件。事后若还想找回,通过版本回退即可。