【Git】2. 基本操作 【命令大全】

Git 基本操作

创建 Git 本地仓库 – git init

要提前说的是,仓库是进⾏版本控制的⼀个⽂件⽬录。我们要想对⽂件进⾏版本控制,就必须先创建⼀个仓库出来。
创建⼀个 Git 本地仓库对应的命令为 git init ,注意命令要在⽂件⽬录下执⾏,例如:

hx@LAPTOP-H2EI4I6A:~/code$ git init
hint: Using 'master' as the name for the initial branch. This default branch name
hint: is subject to change. To configure the initial branch name to use in all
hint: of your new repositories, which will suppress this warning, call:
hint: 
hint:   git config --global init.defaultBranch <name>
hint: 
hint: Names commonly chosen instead of 'master' are 'main', 'trunk' and
hint: 'development'. The just-created branch can be renamed via this command:
hint: 
hint:   git branch -m <name>
Initialized empty Git repository in /home/hx/code/.git/
hx@LAPTOP-H2EI4I6A:~/code$ ll
total 12
drwxrwxr-x 3 hx hx 4096 Jul 19 09:20 ./
drwxr-x--- 9 hx hx 4096 Jul 19 09:19 ../
drwxrwxr-x 7 hx hx 4096 Jul 19 09:20 .git/

我们发现,当前⽬录下多了⼀个 .git 的隐藏⽂件,
.git ⽬录是 Git 来跟踪管理仓库的,不要⼿动修改这个⽬录⾥⾯的⽂件,不然改乱了,就把 Git 仓库给破坏了。切记!!!

hx@LAPTOP-H2EI4I6A:~/code$ tree .git
.git
├── HEAD
├── branches
├── config
├── description
├── hooks
│   ├── applypatch-msg.sample
│   ├── commit-msg.sample
│   ├── fsmonitor-watchman.sample
│   ├── post-update.sample
│   ├── pre-applypatch.sample
│   ├── pre-commit.sample
│   ├── pre-merge-commit.sample
│   ├── pre-push.sample
│   ├── pre-rebase.sample
│   ├── pre-receive.sample
│   ├── prepare-commit-msg.sample
│   ├── push-to-checkout.sample
│   └── update.sample
├── info
│   └── exclude
├── objects
│   ├── info
│   └── pack
└── refs
    ├── heads
    └── tags

9 directories, 17 files

配置 Git – git config

当安装 Git 后⾸先要做的事情是设置你的用户名称 和 e-mail 地址,这是非常重要的。配置命令为:

git config [--global] user.name "Your Name" 
git config [--global] user.email "email@example.com" 
# 把 Your Name 改成你的昵称
# 把 email@example.com 改成邮箱的格式,只要格式正确即可

其中 --global 是⼀个可选项。如果使用了该选项,表示这台机器上所有的 Git 仓库都会使用这个配置。
如果你希望在不同仓库中使用不同的 name 或 e-mail ,可以不要 --global 选项,但要注意的是,执行命令时必须要在仓库里。

查看git配置 – git config -l

查看配置命令为:

git config -l

删除git配置 – git config --unset […]

删除对应的配置命令为:

git config [--global] --unset user.name
git config [--global] --unset user.email

认识工作区、暂存区、版本库

⼯作区:是在电脑上你要写代码或⽂件的⽬录。
• 暂存区:英⽂叫 stage 或 index。⼀般存放在 .git ⽬录下的 index ⽂件(.git/index)中,我们把暂存区有时也叫作索引(index)。
• 版本库:⼜名仓库,英⽂名 repository 。⼯作区有⼀个隐藏⽬录 .git ,它不算⼯作区,⽽是 Git 的版本库。这个版本库⾥⾯的所有⽂件都可以被 Git 管理起来,每个⽂件的修改、删除,Git都能跟踪,以便任何时刻都可以追踪历史,或者在将来某个时刻可以“还原”。

下⾯这个图展⽰了⼯作区、暂存区和版本库之间的关系:
在这里插入图片描述
• 图中左侧为⼯作区,右侧为版本库。Git 的版本库⾥存了很多东西,其中最重要的就是暂存区。
• 在创建 Git 版本库时,Git 会为我们⾃动创建⼀个唯⼀的 master 分⽀,以及指向 master 的⼀个指针叫 HEAD。(分⽀和HEAD的概念后⾯再说)
• 当对⼯作区修改(或新增)的⽂件执⾏ git add 命令时,暂存区⽬录树的⽂件索引会被更新。
• 当执⾏提交操作 git commit 时,master 分⽀会做相应的更新,可以简单理解为暂存区的⽬录树才会被真正写到版本库中。

由上述描述我们便能得知:通过新建或粘贴进⽬录的⽂件,并不能称之为向仓库中新增⽂件,⽽只是在⼯作区新增了⽂件。必须要通过使⽤ git add 和 git commit 命令才能将⽂件添加到仓库中进⾏管理!!!

添加⽂件–场景一

添加文件到暂存区 – git add […]

在包含 .git 的⽬录下新建⼀个 ReadMe ⽂件,我们可以使⽤ git add 命令可以将⽂件添加到暂存区:
• 添加⼀个或多个⽂件到暂存区: git add [file1] [file2] …
• 添加指定⽬录到暂存区,包括⼦⽬录: git add [dir]
• 添加当前⽬录下的所有⽂件改动到暂存区: git add .

暂存区提交到本地仓库 – git commit

再使⽤ git commit 命令将暂存区内容添加到本地仓库中:
• 提交暂存区全部内容到本地仓库中: git commit -m “message”
• 提交暂存区的指定⽂件到仓库区: git commit [file1] [file2] … -m “message”
注意 git commit 后⾯的 -m 选项,要跟上描述本次提交的 message,由⽤⼾⾃⼰完成。
这部分内容绝对不能省略,并要好好描述,是⽤来记录你的提交细节,是给自己或者开发团队看的。
例如:

hx@LAPTOP-H2EI4I6A:~/code/practice/2024/test_7_27$ echo "hello git !" > test.txt
hx@LAPTOP-H2EI4I6A:~/code/practice/2024/test_7_27$ cat test.txt 
hello git !
hx@LAPTOP-H2EI4I6A:~/code/practice/2024/test_7_27$ git add test.txt 
hx@LAPTOP-H2EI4I6A:~/code/practice/2024/test_7_27$ git commit -m "认识git"
[master d27aedf] 认识git
 1 file changed, 1 insertion(+)
 create mode 100644 2024/test_7_27/test.txt

git commit 命令执⾏成功后会告诉我们,1个⽂件被改动(就是我们新添加的ReadMe⽂件),插⼊了两⾏内容(ReadMe有两⾏内容)。
我们还可以多次 add 不同的⽂件,⽽只 commit ⼀次便可以提交所有⽂件,是因为需要提交的⽂件是通通被 add 到暂存区中,然后⼀次性 commit 暂存区的所有修改。
如:

hyb@139-159-150-152:~/gitcode$ touch file1 file2 file3
hyb@139-159-150-152:~/gitcode$ git add file1
hyb@139-159-150-152:~/gitcode$ git add file2
hyb@139-159-150-152:~/gitcode$ git add file3
hyb@139-159-150-152:~/gitcode$ git commit -m "add 3 files"
[master 23807c5] add 3 files
 3 files changed, 0 insertions(+), 0 deletions(-)
 create mode 100644 file1
 create mode 100644 file2
 create mode 100644 file3

截⾄⽬前为⽌,我们已经更够将代码直接提交⾄本地仓库了。

查看提交历史 – git log

我们可以使⽤ git log 命令,来查看下历史提交记录:
在这里插入图片描述
该命令显⽰从最近到最远的提交⽇志,并且可以看到我们 commit 时的⽇志消息。
如果嫌输出信息太多,看得眼花缭乱的,可以试试加上 --pretty=oneline 参数:

hx@LAPTOP-H2EI4I6A:~/code/practice/2024/test_7_27$ git log --pretty=oneline
d27aedf8bf238c06e2db9222130faa68cda3ee2e (HEAD -> master) 认识git
b813e3c08fe0e7ef130e333ce6c2c715a7441727 (origin/master, origin/HEAD) 刷题coding
bea9e3a8719b9f2c1103330fa589d1c0ec853cfd 每日两题
5c15193c9cc7fc43f989d5059d94294db970df01 每日练习
aa3b5aaf78883a40b7ada7ffa2db067aae3d830b 刷题
efa90541e3550e729066bd6bc8a7ac46acf86fcf 刷题ing
9cf5580b9c3ffa7b6be19a915ce2b904d2941153 每日练习
bb1fba365c541fa05dee483d6d4be2b5811d30f6 每日练习
f88fe2221f9660b1020af7ade2ae58477dd12076 list迭代器的实现
5730ebf6cbf6984f4e4da8961fc36fd2ddc6a7e6 每日练习
7c1ec5fea9cc9684f720198874c09f2e9223ea71 每日练习
ff9d404903cd6e06c06446f8d29653e12ee59531 每日练习
a268631c8c9fd4ae21779b471c9667217a3abb06 每日练习
4979d8e02b216750b0e7afbd7a128dd82643ab1c 每日练习
a6ce07c0032ca5d7d59cf1cb1b5d67f07465404c 每日练习
f9ccddc9d882943886e51beba99d6d13146e28b9 每日练习

需要说明的是,我们看到的⼀⼤串类似 23807c5…56eed6 的是每次提交的 commit id (版本号),Git 的 commit id 不是1,2,3……递增的数字,⽽是⼀个 SHA1 计算出来的⼀个⾮常⼤的数字,⽤⼗六进制表⽰。

查看 .git ⽂件 – tree .git

先来看看我们的 .git 的⽬录结构:
在这里插入图片描述
objects 为 Git 的对象库,⾥⾯包含了创建的各种版本库对象及内容。
当执⾏ git add 命令时,暂存区的⽬录树被更新,同时⼯作区修改(或新增)的⽂件内容被写⼊到对象库中的⼀个新的对象中,就位于 “.git/objects” ⽬录下,让我们来看看这些对象有何⽤处:
在这里插入图片描述
查找 object 时要将 commit id 分成2部分,其前2位是⽂件夹名称,后38位是⽂件名称。
找到这个⽂件之后,⼀般不能直接看到⾥⾯是什么,该类⽂件是经过 sha (安全哈希算法)加密过的⽂件。

查看版本库对象的内容 – git cat-file

好在我们可以使⽤ git cat-file 命令来查看版本库对象的内容:

hx@LAPTOP-H2EI4I6A:~/code/practice$ git cat-file -p d27aedf8bf238c06e2db9222130faa68cda3ee2e
tree 5e5044405154927d5cb8f64c99abe8031d3f4bb1
parent b813e3c08fe0e7ef130e333ce6c2c715a7441727
author hacha <hx20020807@163.com> 1722057908 +0800
committer hacha <hx20020807@163.com> 1722057908 +0800

认识git
# 这就是我们最近⼀次的提交!

其中,还有⼀⾏ tree 830a8c9feefbdc098bbae2cdc25e5034ce1920d7 ,我们使⽤同样的⽅法,看看结果:

hyb@139-159-150-152:~/gitcode$ git cat-file -p 830a8c9feefbdc098bbae2cdc25e5034c
100644 blob 9c9e1f0f6bff3015df71a0963004476f5e6cfd54 ReadMe
100644 blob e69de29bb2d1d6434b8b29ae775ad8c2e48c5391 file1
100644 blob e69de29bb2d1d6434b8b29ae775ad8c2e48c5391 file2
100644 blob e69de29bb2d1d6434b8b29ae775ad8c2e48c5391 file3

在看 ReadMe 对应的 9c9e1f0f6bff3015df71a0963004476f5e6cfd54 :

hyb@139-159-150-152:~/gitcode$ git cat-file -p 9c9e1f0f6bff3015df71a0963004476f5
hello bit
hello bit
# 这是我们对ReadMe做的修改!!被git记录了下来!!

总结⼀下,在本地的 git 仓库中,有⼏个⽂件或者⽬录很特殊
• index: 暂存区, git add 后会更新该内容。
• HEAD: 默认指向 master 分⽀的⼀个指针。
• refs/heads/master: ⽂件⾥保存当前 master 分⽀的最新 commit id 。
• objects: 包含了创建的各种版本库对象及内容,可以简单理解为放了 git 维护的所有修改。

后⾯的学习过程中,最好能将常⻅的 git 操作与 .git ⽬录当中的结构内容变化对应起来,这样有利于我们理解git 细节流程。

添加⽂件–场景⼆

学习到这⾥,我们已经清楚了如何向仓库中添加⽂件,并且对于⼯作区、暂存区、版本库也有了⼀定的认识。那么我们再展⽰⼀种添加⽂件的场景,能加深对⼯作区、暂存区、版本库的理解,⽰例如下:

hx@LAPTOP-H2EI4I6A:~/code/practice$ touch file1 #1. 新增file4⽂件
hx@LAPTOP-H2EI4I6A:~/code/practice$ git add file1 #2. 将file4添加到暂存区
hx@LAPTOP-H2EI4I6A:~/code/practice$ touch file2 #3. 新增file5⽂件
hx@LAPTOP-H2EI4I6A:~/code/practice$ git commit -m "add file" #4. 提交修改
[master 6f61a49] add file
 1 file changed, 0 insertions(+), 0 deletions(-)
 create mode 100644 file1

提交后发现打印了 1 file changed, 0 insertions(+), 0 deletions(-) ,意思是只有⼀个⽂件改变了,这时我们提出了疑问,不是新增了两个⽂件吗?
再来回忆下, git add 是将⽂件添加到暂存区, git commit 是将暂存区的内容添加到本地仓库中。
由于我们并没有使⽤ git add file5 ,file5 就不在暂存区中维护,所以我们 commit 的时候其实只是把已经在暂存区的 file4 提交了,⽽遗漏了⼯作区的 file5。
如何提交 file5 呢?很简单,再次
add , commit 即可

修改⽂件

Git ⽐其他版本控制系统设计得优秀,因为 Git 跟踪并管理的是修改,⽽⾮⽂件。
什么是修改?⽐如你新增了⼀⾏,这就是⼀个修改,删除了⼀⾏,也是⼀个修改,更改了某些字符,也是⼀个修改,删了⼀些⼜加了⼀些,也是⼀个修改,甚⾄创建⼀个新⽂件,也算⼀个修改。

让我们将test.txt ⽂件进⾏⼀次修改:

hx@LAPTOP-H2EI4I6A:~/code/practice/2024/test_7_27$ cat test.txt 
hello git !
hello world

此时,仓库中的 ReadMe 和我们⼯作区的 ReadMe 是不同的,如何查看当前仓库的状态呢?

查看当前仓库状态 – git status

git status 命令⽤于查看在你上次提交之后是否有对⽂件进⾏再次修改。

hx@LAPTOP-H2EI4I6A:~/code/practice/2024/test_7_27$ git status 
On branch master
Your branch is ahead of 'origin/master' by 2 commits.
  (use "git push" to publish your local commits)

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)
        modified:   test.txt
        deleted:    ../../file1

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

上⾯的结果告诉我们,test.txt 被修改过了,但还没有完成添加与提交。
⽬前,我们只知道⽂件被修改了,如果能知道具体哪些地⽅被修改了,就更好了。有同学会说,我刚
改的我知道呀!可是,你还记得你三天前写了什么代码吗?或者没写?

hx@LAPTOP-H2EI4I6A:~/code/practice/2024/test_7_27$ git diff test.txt
diff --git a/2024/test_7_27/test.txt b/2024/test_7_27/test.txt
index e0d2b27..3288e73 100644
--- a/2024/test_7_27/test.txt
+++ b/2024/test_7_27/test.txt
@@ -1 +1,2 @@
 hello git !
+hello world

显⽰暂存区和⼯作区⽂件的差异 – git diff

git diff [file] 命令⽤来显⽰暂存区和⼯作区⽂件的差异,显⽰的格式正是Unix通⽤的diff格
式。也可以使⽤ git diff HEAD – [file] 命令来查看版本库和⼯作区⽂件的区别。
知道了对 ReadMe 做了什么修改后,再把它提交到本地仓库就放⼼多了。

hx@LAPTOP-H2EI4I6A:~/code/practice/2024/test_7_27$ git status 
On branch master
Your branch is ahead of 'origin/master' by 2 commits.
  (use "git push" to publish your local commits)

Changes to be committed:
  (use "git restore --staged <file>..." to unstage)
        modified:   test.txt

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:    ../../file1

git add 之后,就没有看到上⾯ no changes added to commit (use “git add”
and/or “git commit -a”) 的消息了。接下来让我们继续 git commit 即可:

hx@LAPTOP-H2EI4I6A:~/code/practice/2024/test_7_27$ git commit -m "修改test.txt文件"
[master fedbf53] 修改test.txt文件
 1 file changed, 1 insertion(+)
hx@LAPTOP-H2EI4I6A:~/code/practice/2024/test_7_27$ git status
On branch master
Your branch is ahead of 'origin/master' by 3 commits.
  (use "git push" to publish your local commits)

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:    ../../file1

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

版本回退

之前我们也提到过,Git 能够管理⽂件的历史版本,这也是版本控制器重要的能⼒。
如果有⼀天你发现之前前的⼯作做的出现了很⼤的问题,需要在某个特定的历史版本重新开始,这个时候,就需要版本回退的功能了。

版本回退 – git reset […]

执⾏ git reset 命令⽤于回退版本,可以指定退回某⼀次提交的版本。要解释⼀下“回退”本质是要将版本库中的内容进⾏回退,⼯作区或暂存区是否回退由命令参数决定:
git reset 命令语法格式为: git reset [--soft | --mixed | --hard] [HEAD]
• --mixed 为默认选项,使⽤时可以不⽤带该参数。该参数将暂存区的内容退回为指定提交版本内
容,⼯作区⽂件保持不变。
• --soft 参数对于⼯作区和暂存区的内容都不变,只是将版本库回退到某个指定版本。
• --hard 参数将暂存区与⼯作区都退回到指定版本。切记⼯作区有未提交的代码时不要⽤这个命令,因为⼯作区会回滚,你没有提交的代码就再也找不回了,所以使⽤该参数前⼀定要慎重。
• HEAD 说明:
◦ 可直接写成 commit id,表⽰指定退回的版本
◦ HEAD 表⽰当前版本
◦ HEAD^ 上⼀个版本
◦ HEAD^^ 上上⼀个版本
◦ 以此类推…
• 可以使⽤ 〜数字表⽰:
◦ HEAD~0 表⽰当前版本
◦ HEAD~1 上⼀个版本
◦ HEAD^2 上上⼀个版本
◦ 以此类推…
为了便于表述,⽅便测试回退功能,我们先做⼀些准备⼯作:更新3个版本的 ReadMe,并分别进⾏3
次提交,如下所⽰:

hx@LAPTOP-H2EI4I6A:~/code/practice/2024/test_7_27$ vim test.txt 
hx@LAPTOP-H2EI4I6A:~/code/practice/2024/test_7_27$ git add test.txt 
hx@LAPTOP-H2EI4I6A:~/code/practice/2024/test_7_27$ git commit -m "新增版本1"
[master 9e261f9] 新增版本1
 1 file changed, 5 insertions(+)
hx@LAPTOP-H2EI4I6A:~/code/practice/2024/test_7_27$ vim test.txt 
hx@LAPTOP-H2EI4I6A:~/code/practice/2024/test_7_27$ git add test.txt 
hx@LAPTOP-H2EI4I6A:~/code/practice/2024/test_7_27$ git commit -m "新增版本2"
[master e6840d4] 新增版本2
 1 file changed, 1 insertion(+)
hx@LAPTOP-H2EI4I6A:~/code/practice/2024/test_7_27$ vim test.txt 
hx@LAPTOP-H2EI4I6A:~/code/practice/2024/test_7_27$ git add test.txt 
hx@LAPTOP-H2EI4I6A:~/code/practice/2024/test_7_27$ git commit -m "新增版本3"
[master 9399e34] 新增版本3
 1 file changed, 1 insertion(+), 1 deletion(-)
hx@LAPTOP-H2EI4I6A:~/code/practice/2024/test_7_27$ cat test.txt 
hello git !
hello world



hello version1
hello version2
hello version3
hx@LAPTOP-H2EI4I6A:~/code/practice/2024/test_7_27$ git status
no changes added to commit (use "git add" and/or "git commit -a")
9399e34c53f94e562762b9bef364766d20f1f68c (HEAD -> master, origin/master, origin/HEAD) 新增版本3
e6840d4ec4721e02defaa5759adf7065a32c683b 新增版本2
9e261f9b885da40c8c3a8a546a235b28d80e5160 新增版本1

现在,如果我们在提交完 version3 后, 发现 version 3 编写错误,想回退到 version2,重新基于
version 2 开始编写。由于我们在这⾥希望的是将⼯作区的内容也回退到 version 2 版本,所以需
要⽤到 --hard 参数,⽰例如下:

hx@LAPTOP-H2EI4I6A:~/code/practice/2024/test_7_27$ git reset --hard e6840d4ec4721e02defaa5759adf7065a32c683b
HEAD is now at e6840d4 新增版本2
hx@LAPTOP-H2EI4I6A:~/code/practice/2024/test_7_27$ cat test.txt 
hello git !
hello world



hello version1
hello version2

我们惊奇的发现,此时 ReadMe ⽂件的内容,已经回退到 version2 了!,当前,我们再次⽤ git
log 查看⼀下提交⽇志,发现 HEAD 指向了version2,如下所⽰:

hx@LAPTOP-H2EI4I6A:~/code/practice/2024/test_7_27$ git log --pretty=oneline 
e6840d4ec4721e02defaa5759adf7065a32c683b (HEAD -> master) 新增版本2
9e261f9b885da40c8c3a8a546a235b28d80e5160 新增版本1

到这⾥⼀般回退功能就演⽰完了,但现在如果我后悔了,想再回到 version 3 怎么办?我们可以继续使
⽤ git reset 命令,回退到 version 3 版本,但我们必须要拿到 version 3 的 commit
id 去指定回退的版本。
但我们看到了 git log 并不能打印出 version 3 的 commit id ,运⽓好的话我们可以从终端
上去找找之前的记录,运⽓不好的话 commit id 已经被我们搞丢了=。=

记录本地的每⼀次命令 – git reflog

Git 还提供了⼀个 git reflog 命令能补救⼀下,该命令⽤来记录本地的每⼀次命令。

hx@LAPTOP-H2EI4I6A:~/code/practice/2024/test_7_27$ git reflog 
e6840d4 (HEAD -> master) HEAD@{0}: reset: moving to e6840d4ec4721e02defaa5759adf7065a32c683b
9399e34 (origin/master, origin/HEAD) HEAD@{1}: commit: 新增版本3
e6840d4 (HEAD -> master) HEAD@{2}: commit: 新增版本2
9e261f9 HEAD@{3}: commit: 新增版本1
fedbf53 HEAD@{4}: commit: 修改test.txt文件
6f61a49 HEAD@{5}: commit: add file
d27aedf HEAD@{6}: commit: 认识git
b813e3c HEAD@{7}: clone: from https://gitee.com/hacha/practice.git

这样,你就可以很⽅便的找到你的所有操作记录了,但 d95c13f 这个是啥东西?这个是 version
3 的 commit id 的部分。没错,Git 版本回退的时候,也可以使⽤部分 commit id 来代表⽬标版
本。⽰例如下:

# 回退到v3
hx@LAPTOP-H2EI4I6A:~/code/practice/2024/test_7_27$ git reset --hard 9399e34
HEAD is now at 9399e34 新增版本3

# 查看⼯作区
hx@LAPTOP-H2EI4I6A:~/code/practice/2024/test_7_27$ cat test.txt 
hello git !
hello world



hello version1
hello version2
hello version3

# 查看log
hx@LAPTOP-H2EI4I6A:~/code/practice/2024/test_7_27$ git log --pretty=oneline 
9399e34c53f94e562762b9bef364766d20f1f68c (HEAD -> master, origin/master, origin/HEAD) 新增版本3
e6840d4ec4721e02defaa5759adf7065a32c683b 新增版本2
9e261f9b885da40c8c3a8a546a235b28d80e5160 新增版本1

可往往是理想很丰满,现实很⻣感。在实际开发中,由于⻓时间的开发了,导致 commit id 早就找不到了,可突然某⼀天,我⼜想回退到 version3,那该如何操作呢?貌似现在不可能了。。。

值得说的是,Git 的版本回退速度⾮常快,因为 Git 在内部有个指向当前分⽀(此处是master)的HEAD 指针, refs/heads/master ⽂件⾥保存当前 master 分⽀的最新 commit id 。
当我们在回退版本的时候,Git 仅仅是给 refs/heads/master 中存储⼀个特定的version,可以简单理解成如下⽰意图:
在这里插入图片描述

撤销修改

如果我们在我们的⼯作区写了很⻓时间代码,越写越写不下去,觉得⾃⼰写的实在是垃圾,想恢复到上⼀个版本。

情况⼀:对于⼯作区的代码,还没有 add

你当然可以直接删掉你⽬前在⼯作区新增的代码,像这样:

# 向ReadMe中新增⼀⾏代码
hyb@139-159-150-152:~/gitcode$ git status
On branch master
nothing to commit, working tree clean
hyb@139-159-150-152:~/gitcode$ vim ReadMe 
hyb@139-159-150-152:~/gitcode$ cat ReadMe 
hello bit
hello git
hello world
hello version1
hello version2
hello version3
This piece of code is like shit #新增代码
hyb@139-159-150-152:~/gitcode$ 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: ReadMe
no changes added to commit (use "git add" and/or "git commit -a")
# 直接删除代码
hyb@139-159-150-152:~/gitcode$ vim ReadMe 
hyb@139-159-150-152:~/gitcode$ cat ReadMe 
hello bit
hello git
hello world
hello version1
hello version2
hello version3
hyb@139-159-150-152:~/gitcode$ git status
On branch master
nothing to commit, working tree clean

幸亏我们⼯作效率不⾼,才写了⼀⾏代码就发现不⾏了,要是你写了3天,⼀直都没有提交,该怎么删掉呢?你⾃⼰都忘了⾃⼰新增过哪些,有同学说,我可以 git diff xxx ⼀下,看看差别在删啊,
那你肯定⼜要花3天时间删代码了,并且很⼤的概率还会改出bug。⼀周过去了,你怎么向你的⽼板交代呢?

⼯作区⽂件回到add状态 – git checkout – [file]

Git 其实还为我们提供了更好的⽅式,我们可以使⽤ git checkout – [file] 命令让⼯作区的⽂件回到最近⼀次 add 或 commit 时的状态。 要注意 git checkout – [file] 命令中的
==-- 很重要,切记不要省略,⼀旦省略,该命令就变为其他意思了,==后⾯我们再说。⽰例如下:

# 向ReadMe中新增⼀⾏代码
hyb@139-159-150-152:~/gitcode$ vim ReadMe 
hyb@139-159-150-152:~/gitcode$ cat ReadMe 
hello bit
hello git
hello world
hello version1
hello version2
hello version3
This piece of code is like shit #新增代码
# 恢复到上⼀次 add 或 commit
hyb@139-159-150-152:~/gitcode$ git checkout -- ReadMe
hyb@139-159-150-152:~/gitcode$ cat ReadMe 
hello bit
hello git
hello world
hello version1
hello version2
hello version3

情况⼆:已经 add ,但没有 commit

add 后还是保存到了暂存区呢?怎么撤销呢?

# 向ReadMe中新增⼀⾏代码
hyb@139-159-150-152:~/gitcode$ vim ReadMe 
hyb@139-159-150-152:~/gitcode$ cat ReadMe 
hello bit
hello git
hello world
hello version1
hello version2
hello version3
This piece of code is like shit #新增代码
# add 存⼊暂存区
hyb@139-159-150-152:~/gitcode$ git add ReadMe
hyb@139-159-150-152:~/gitcode$ git status
On branch master
Changes to be committed:
 (use "git restore --staged <file>..." to unstage)
 modified: ReadMe

让我们来回忆⼀下学过的 git reset 回退命令,该命令如果使⽤ --mixed 参数,可以将暂存区的内容退回为指定的版本内容,但⼯作区⽂件保持不变。那我们就可以回退下暂存区的内容了!!!

⽰例如下:

# --mixed 是默认参数,使⽤时可以省略
hyb@139-159-150-152:~/gitcode$ git reset HEAD ReadMe # 第一步
Unstaged changes after reset:
M ReadMe

⽤ git status 查看⼀下,发现现在暂存区是⼲净的,⼯作区有修改。

hyb@139-159-150-152:~/gitcode$ 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: ReadMe
no changes added to commit (use "git add" and/or "git commit -a")

还记得如何丢弃⼯作区的修改吗?

hyb@139-159-150-152:~/gitcode$ git checkout -- ReadMe # 第二步
hyb@139-159-150-152:~/gitcode$ git status
On branch master
nothing to commit, working tree clean
hyb@139-159-150-152:~/gitcode$ cat ReadMe 
hello bit
hello git
hello world
hello version1
hello version2
hello version3

恢复了!

情况三:已经 add ,并且也 commit 了

不要担⼼,我们可以 git reset --hard HEAD^ 回退到上⼀个版本!不过,这是有条件的,就是你还没有把⾃⼰的本地版本库推送到远程。还记得Git是分布式版本控制系统吗?我们后⾯会讲到远程
版本库,⼀旦你推送到远程版本库,你就真的惨了…… (远端都推送了还恢复啥?只能再提交覆盖了,所以每次push前想清楚再push)

# 向ReadMe中新增⼀⾏代码
hyb@139-159-150-152:~/gitcode$ vim ReadMe 
hyb@139-159-150-152:~/gitcode$ cat ReadMe 
hello bit
hello git
hello world
hello version1
hello version2
hello version3
This piece of code is like shit #新增代码
# 提交
hyb@139-159-150-152:~/gitcode$ git add ReadMe 
hyb@139-159-150-152:~/gitcode$ git commit -m"test quash"
[master 5f71ae1] test quash
 1 file changed, 1 insertion(+)
# 回退
hyb@139-159-150-152:~/gitcode$ git reset --hard HEAD^
HEAD is now at 144a3f8 add file
hyb@139-159-150-152:~/gitcode$ cat ReadMe 
hello bit
hello git
hello world
hello version1
hello version2
hello version3

删除⽂件

在 Git 中,删除也是⼀个修改操作,我们实战⼀下, 如果要删除 file5 ⽂件,怎么搞呢?如果你这样做了:

hyb@139-159-150-152:~/gitcode$ ls
file1 file2 file3 file4 file5 ReadMe
hyb@139-159-150-152:~/gitcode$ rm file5

但这样直接删除是没有⽤的,反⽽徒增烦恼, git status 命令会⽴刻告诉你哪些⽂件被删除了:

hyb@139-159-150-152:~/gitcode$ 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: file5
no changes added to commit (use "git add" and/or "git commit -a")

此时,⼯作区和版本库就不⼀致了,要删⽂件,⽬前除了要删⼯作区的⽂件,还要清除版本库的⽂件。

⼀般⾛到这⾥,有两种可能:
• 确实要从版本库中删除该⽂件
• 不⼩⼼删错了
对第⼆种情况,很明显误删,需要使⽤ git 来进⾏恢复,很简单,我们刚学过(删除也是修改):

hyb@139-159-150-152:~/gitcode$ git checkout -- file5
hyb@139-159-150-152:~/gitcode$ ls
file1 file2 file3 file4 file5 ReadMe

对于第⼀种情况,很明显是没有删完,我们只删除了⼯作区的⽂件。这时就需要使⽤ git rm 将⽂件从暂存区和⼯作区中删除,并且 commit :

暂存+工作区删除 – git rm

hyb@139-159-150-152:~/gitcode$ git rm file5
rm 'file5'
hyb@139-159-150-152:~/gitcode$ git status
On branch master
Changes to be committed:
 (use "git restore --staged <file>..." to unstage)
 deleted: file5
hyb@139-159-150-152:~/gitcode$ git commit -m"deleted file5"
[master 5476bde] deleted file5
 1 file changed, 0 insertions(+), 0 deletions(-)
 delete mode 100644 file5
hyb@139-159-150-152:~/gitcode$ git status
On branch master
nothing to commit, working tree clean

现在,⽂件就从版本库中被删除了。

  • 12
    点赞
  • 10
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值