Git基础(二)本地Git仓库管理

Git基础(二)本地Git仓库管理

以下内容来自对《精通Git 第二版》的学习总结整理,可直接食用

注:本文以windows10操作系统,cmd命令为参考。

如果您使用的是mac os,那么可能需要注意一下目录符号\/的问题(在windows中,路径类似于C:\Users\yongzhilu\Desktop\gitlearn,在mac中,路径可能类似于/etc/xxx/

1. 从服务器上拉取git项目 git clone

git clone 远程仓库地址 [本地仓库别名]

# 克隆远程hellogit项目
git clone https://gitee.com/example/hellogit.git
# 或
# 克隆远程hellogit项目,重命名为hg
git clone https://gitee.com/example/hellogit.git hg

2. 文件跟踪/添加到暂存区 git add

可以通过 git add 命令来实现对 指定文件/目录 的跟踪

git add a.txt       # 添加跟踪某个文件
git add direct/     # 添加跟踪某个目录内部文件
git add *           # 添加跟踪当前目录下所有文件/目录

3. 检查文件状态信息 git status

为了保持本文内容的简洁,将一些说明性的内容另放其他位置

点击链接查看 文件状态信息分类

要查看哪些文件处于什么状态,可以用 git status命令。

该命令还显示了当前所在 分支,并告诉你这个分支同远程服务器上对应的分支没有偏离。

  • 查看状态详细信息
git status          # 查看当前项目的所有文件状态信息
git status a.txt    # 查看某个文件的状态
git status direct/  # 查看某个目录下所有文件状态信息
  • 查看状态简略信息
git status -s          # 查看当前项目的所有文件状态信息
git status -s a.txt    # 查看某个文件的状态
git status -s direct/  # 查看某个目录下所有文件状态信息

img

??:还没有被跟踪,没有 git add,陌生文件

A:新添加到暂存区的文件/直接 git add 进来的新文件,且没有进行过 git commit

左M:工作区的文件,修改了并且已经 git add 进暂存区,但是没有 git commit

右M:工作区的文件,修改了但是没有 git add 进暂存区

MM:工作区的文件,git commit 到暂存区后又在工作区修改过

右D:从工作区删除,但是没有 git add 进暂存区

左D:从工作区删除,并且 git add 进暂存区,但是没有 git commit

4. 对比文件差异 git diff

你想知道具体修改了什么地方,可以用 git diff 命令。

git diff 比较的是 工作区中当前文件暂存区该文件 之间的差异

② 如果想知道 暂存区该文件git本地仓库该文件 之间的差异,可以使用 git diff --staged 命令

img

git diff 				# 查看所有文件对比信息  工作区->暂存区
git diff a.txt			# 查看某个文件对比信息  工作区->暂存区
git diff b/ 			# 查看某个目录下,所有文件对比信息  工作区->暂存区
git diff --staged		# 查看所有文件对比信息  暂存区 -> git本地仓库
git diff --staged a.txt	# 查看某个文件对比信息  暂存区 -> git本地仓库
git diff --staged b/ 	# 查看某个目录下,所有文件对比信息  暂存区 -> git本地仓库

当然,文件对比使用工具,如vs code插件及其他可视化工具Sourcetree可能会更方便。

5. 提交更新 git commit

git commit命令用于将暂存区的文件全部提交到本地git仓库

git commit

这种方式会启动文本编辑器以便输入本次提交的说明。 (默认会启用 shell 的环境变量 $EDITOR 所指定的软件,一般都是 vim 或 emacs。当然也可以按照 起步 介绍的方式,使用git config --global core.editor 命令设定你喜欢的编辑软件。)

git commit -v

如果想要更详细的对修改了哪些内容的提示,可以用 -v 选项,这会将你所做的改变的 diff 输出放到编辑器中从而使你知道本次提交具体做了哪些修改。

退出编辑器时,Git 会丢掉注释行,用你输入提交附带信息生成一次提交。

img

git commit -m "提交信息"   # 用的较多

你也可以在 commit 命令后添加 -m 选项,将提交信息与命令放在同一行

git commit -a -m "add & commit操作合并"  # 个人用的比较少,因为git commit前,最好还是仔细比对一下

git commit会对已经git add的文件进行提交,上述命令可以对一个文件直接add并提交,简略了手动git add的步骤

6. 移除文件 git rm

注:移除文件会将文件从本地工作区删除,如果只是想不再跟踪某个文件或将文件从暂存区移除,而不是将该文件从本地工作区中删除,那么下面的撤销操作可能会是您想要的结果。

注:移除文件时要小心!移除文件会将文件从本地工作区删除!命令确认敲对了再进行移除!!!

注:移除文件时要小心!移除文件会将文件从本地工作区删除!命令确认敲对了再进行移除!!!

注:移除文件时要小心!命令确认敲对了再进行移除!

注:移除文件时要小心!移除文件会将文件从本地工作区删除!命令确认敲对了再进行移除!!!

  • 移除文件的两个步骤
# 移除文件的两个步骤
# 1. 将文件从暂存区和工作目录中移除
# 注:git rm 操作会将该文件从本地工作目录中删除。
git rm a.txt

# 2. 提交修改,移除本地git仓库中对应的文件
# 注:如果文件仅仅是untracked,那么手动删除即可  
git commit -m "message"

举例:git rm a.txt && git commit -m "message"

# 1.查看当前目录下文件
C:\Users\zsf\Desktop\qweqwe\gitlearn>dir
2022-07-15  18:42    <DIR>          .
2022-07-15  18:42    <DIR>          ..
2022-07-15  18:42                 1 a.txt

# 2.移除这个仅有1B大小的a.txt文件
C:\Users\zsf\Desktop\qweqwe\gitlearn>git rm a.txt
rm 'a.txt'

# 3.可以看到,这个文件的状态被标记为删除
C:\Users\zsf\Desktop\qweqwe\gitlearn>git status -s
D  a.txt

# 4.提交删除操作,没有报错
C:\Users\zsf\Desktop\qweqwe\gitlearn>git commit -m "delete a.txt and commit"
[master daf1c70] delete a.txt and commit
 1 file changed, 1 deletion(-)
 delete mode 100644 a.txt
 
# 5.重新查看文件状态(空,表示该文件的delete操作已经被commit成功)
C:\Users\zsf\Desktop\qweqwe\gitlearn>git status -s

# 6.查看本地文件,a.txt文件已经被移除了(工作空间以及git仓库中均被移除)
C:\Users\zsf\Desktop\qweqwe\gitlearn>dir
2022-07-15  18:42    <DIR>          .
2022-07-15  18:42    <DIR>          ..
  • 手动删除文件

如果有人习惯或想要直接将文件从本地删除,并且想将git仓库中的文件也删除,可以采用下面的方式提交删除操作

如果只是简单地从工作目录中手工删除文件,运行 git status 时就会在 “Changes not staged for commit”

此时需要①运行 git rm a.txt 记录此次移除文件的操作,②然后 git commit 提交

# 注意,
# 此时的D,在右边(表明这是一个没有被跟踪的删除操作)
# 而使用git rm删除文件的D,在左边(表明是一个已被跟踪的删除操作)
C:\Users\zsf\Desktop\qweqwe\gitlearn>git status -s
 D a.txt
 
# step1 使用git rm追踪一下删除操作(个人感觉也可以使用git add,都是为了追踪一下这个文件的修改操作,可能git rm更加规范吧)
C:\Users\zsf\Desktop\qweqwe\gitlearn>git rm a.txt
rm 'a.txt

# step2 提交删除操作
C:\Users\zsf\Desktop\qweqwe\gitlearn>git commit -m "change delete status"
[master b7a9ab3] zcx
 1 file changed, 1 insertion(+)
 create mode 100644 a.txt
  • 强制删除文件

如果删除之前修改过并且已经放到暂存区域的话,则必须要用强制删除选项 -f(译注:即 force 的首字母)。 这是一种安全特性,用于防止误删还没有添加到快照的数据, 这样的数据不能被 Git 恢复。

git rm -f a.txt
# 在修改了a.txt后,并没有git add存放至暂存区,而是想直接移除
C:\Users\zsf\Desktop\qweqwe\gitlearn>git status -s a.txt
 M a.txt
 
# 此时需要通过force强制移除
C:\Users\zsf\Desktop\qweqwe\gitlearn>git rm a.txt
error: the following file has local modifications:
    a.txt
(use --cached to keep the file, or -f to force removal)

# 强制移除
C:\Users\zsf\Desktop\qweqwe\gitlearn>git rm -f a.txt
rm 'a.txt'

# 提交
C:\Users\zsf\Desktop\qweqwe\gitlearn>git commit -m "zxc"
[master 9592cb0] zxc
 1 file changed, 1 deletion(-)
 delete mode 100644 a.txt
  • 批量删除文件
# 删除b目录以及目录下所有文件
git rm -r b/
git commit -m "message"

# git rm 命令后面可以列出文件或者目录的名字,也可以使用 glob 模式
# 注意到星号 * 之前的反斜杠 \, 因为 Git 有它自己的文件模式扩展匹配方式,所以我们不用 shell 来帮忙展开。

# 1.删除log/目录下所有扩展名为.log的文件
git rm log/\*.log

举例1:git rm -r b/

有以下文件
|-b/
  |-a/
  	|-a.txt
  |-b.txt
  |-c.txt
# 1.该目录下有个b目录,任务是将b/b.txt b/c.txt以及b/a/a.txt都移除
C:\Users\zsf\Desktop\qweqwe\gitlearn 的目录
2022-07-15  18:53    <DIR>          .
2022-07-15  18:53    <DIR>          ..
2022-07-15  18:51    <DIR>          b

# 2.执行命令
C:\Users\zsf\Desktop\qweqwe\gitlearn>git rm -r b/
rm 'b/a/a.txt'
rm 'b/b.txt'
rm 'b/c.txt'

# 3.查看状态
C:\Users\zsf\Desktop\qweqwe\gitlearn>git status -s
D  b/a/a.txt
D  b/b.txt
D  b/c.txt

# 4.提交
C:\Users\zsf\Desktop\qweqwe\gitlearn>git commit -m "delete dir b"
[master 42ae66a] delete dir b
 3 files changed, 3 deletions(-)
 delete mode 100644 b/a/a.txt
 delete mode 100644 b/b.txt
 delete mode 100644 b/c.txt
 
# 5.重新查看文件状态
C:\Users\zsf\Desktop\qweqwe\gitlearn>git status -s

# 6.查看本地文件夹,b目录已经被移除
C:\Users\zsf\Desktop\qweqwe\gitlearn>dir
2022-07-15  18:55    <DIR>          .
2022-07-15  18:55    <DIR>

举例2:git rm log/\*.log

# 创建一下使用到的文件
C:\Users\zsf\Desktop\qweqwe\gitlearn>git add log/
C:\Users\zsf\Desktop\qweqwe\gitlearn>git commit -m "init"
[master 828ba82] asd
 7 files changed, 7 insertions(+)
 create mode 100644 log/1.log
 create mode 100644 log/2.log
 create mode 100644 log/a.txt
 create mode 100644 log/b.txt
 create mode 100644 log/20220715/1.log
 create mode 100644 log/20220715/2.log
 create mode 100644 log/20220715/a.txt
 
# 删除.log文件
C:\Users\zsf\Desktop\qweqwe\gitlearn>git rm -r log/\*.log
rm 'log/1.log'
rm 'log/2.log'
rm 'log/20220715/1.log'
rm 'log/20220715/2.log'

# 查看文件状态
C:\Users\zsf\Desktop\qweqwe\gitlearn>git status -s
D  log/1.log
D  log/2.log
D  log/20220715/1.log
D  log/20220715/2.log

# 提交删除操作
C:\Users\zsf\Desktop\qweqwe\gitlearn>git commit -m "delete files end with .log"
[master 9801f46] delete files end with .log
 4 files changed, 4 deletions(-)
 delete mode 100644 log/1.log
 delete mode 100644 log/2.log
 delete mode 100644 log/20220715/1.log
 delete mode 100644 log/20220715/2.log

7. 移动文件/重命名文件 git mv

git mv <file_from> <file_to>

其实,运行 git mv 就相当于运行了下面三条命令:

mv README.md README

git rm README.md

git add README

注:如果file_to的文件夹不存在的话,会mv失败,因为git mv不会自动创建目录,此时需要先创建目录,在进行文件移动

# 移动失败
C:\Users\zsf\Desktop\qweqwe\gitlearn>git mv c.txt /c/c.txt
fatal: Invalid path '/c': No such file or directory

# 创建文件夹后再尝试移动
C:\Users\zsf\Desktop\qweqwe\gitlearn>mkdir c && git mv c.txt c/c.txt

# 移动成功
C:\Users\zsf\Desktop\qweqwe\gitlearn>git status -s
R  c.txt -> c/c.txt

# 提交rename操作
C:\Users\zsf\Desktop\qweqwe\gitlearn>git commit -m "zxc"
[master 069a08c] zxc
 1 file changed, 0 insertions(+), 0 deletions(-)
 rename c.txt => c/c.txt (100%)
  • 注:如果目标文件夹,已经有重名文件会怎么样?
# 经测试,会移动失败
C:\Users\zsf\Desktop\qweqwe\gitlearn>git mv c.txt c/c.txt
fatal: destination exists, source=c.txt, destination=c/c.txt

# 但是我们可以使用-f选项,强制进行重命名
# 但是会发现,rename操作被分割为了delete和modify操作,文件c/c.txt被文件c.txt强制“覆盖”

C:\Users\zsf\Desktop\qweqwe\gitlearn>git mv -f c.txt c/c.txt

C:\Users\zsf\Desktop\qweqwe\gitlearn>git status -s
D  c.txt
M  c/c.txt
# 除此之外,rename操作,被执行为1次insert和2次delete,应该是将c.txt和c/c.txt都删除,重新生成了一个c/c.txt
C:\Users\zsf\Desktop\qweqwe\gitlearn>git commit -m "force rename file"
[master 8f50095] asd
 2 files changed, 1 insertion(+), 2 deletions(-)
 delete mode 100644 c.txt

8. 查看提交历史记录 git log

一般在实际工作中,使用可视化工具,可能会比命令行更加直观

但是如果想自定义查看某些条件下的历史提交记录,那么这个命令还是很有必要学一下的

git log 功能非常强大

本文仅列出一些有趣的命令,且这些命令都是可以组合的,不过你可以自己设计想要的命令。

参考链接: Git - Viewing the Commit History (git-scm.com)

  • git log
# 最简单的命令,查看所有历史提交记录
git log
  • git -<n>
# -<n> 
# 只显示最近n次提交
git log -2
  • git log -p
# -p 
# 显示每次提交的内容差异,当进行代码审查,或者快速浏览某个搭档提交的 commit 所带来的变化的时候,这个参数就非常有用了。
git log -p -1

C:\Users\zsf\Desktop\sd\git-examples>git log -p -1
commit 856052f2a8cc9bc36426445e23cb239ac3299691 (HEAD -> master)
Author: yongzhilu <1046803954@qq.com>
Date:   Fri Jul 15 21:51:23 2022 +0800

    third commit

diff --git a/a.txt b/a.txt
index 2e65efe..1325d7c 100644
--- a/a.txt
+++ b/a.txt
@@ -1 +1 @@
-a
\ No newline at end of file
+a啊
\ No newline at end of file
diff --git a/b.txt b/b.txt
index 2e65efe..eb49652 100644
--- a/b.txt
+++ b/b.txt
@@ -1 +1 @@
-a
\ No newline at end of file
+ac
\ No newline at end of file
  • git log --stat
# --stat
# 显示每次更新的文件修改统计信息。
git log --stat

C:\Users\zsf\Desktop\sd\git-examples>git log --stat
commit 856052f2a8cc9bc36426445e23cb239ac3299691 (HEAD -> master)
Author: yongzhilu <1046803954@qq.com>
Date:   Fri Jul 15 21:51:23 2022 +0800

    third commit

 a.txt | 2 +-
 b.txt | 2 +-
 2 files changed, 2 insertions(+), 2 deletions(-)
  • git log --shortstat
# --shortstat
# 只显示 --stat 中最后的行数修改添加移除统计,不显示文件列表
git log --shortstat

C:\Users\zsf\Desktop\sd\git-examples>git log --shortstat -1
commit 856052f2a8cc9bc36426445e23cb239ac3299691 (HEAD -> master)
Author: yongzhilu <1046803954@qq.com>
Date:   Fri Jul 15 21:51:23 2022 +0800

    third commit

 2 files changed, 2 insertions(+), 2 deletions(-)
  • git log --name-only
# --name-only
# 仅在提交信息后显示已修改的文件清单。
git log --name-only

C:\Users\zsf\Desktop\sd\git-examples>git log --name-only -1
commit 856052f2a8cc9bc36426445e23cb239ac3299691 (HEAD -> master)
Author: yongzhilu <1046803954@qq.com>
Date:   Fri Jul 15 21:51:23 2022 +0800

    third commit

a.txt
b.txt
  • git log --name-status
# --name-status
# 显示新增、修改、删除的文件清单
git log --name-status

C:\Users\zsf\Desktop\sd\git-examples>git log --name-status -1
commit 856052f2a8cc9bc36426445e23cb239ac3299691 (HEAD -> master)
Author: yongzhilu <1046803954@qq.com>
Date:   Fri Jul 15 21:51:23 2022 +0800

    third commit

M       a.txt
M       b.txt
  • git log --abbrev-commit
# --abbrev-commit
# 仅显示 SHA-1 的前几个字符,而非所有的 40 个字符
# 对于查commitId十分有用
git log --abbrev-commit

C:\Users\zsf\Desktop\sd\git-examples>git log --abbrev-commit -1
commit 856052f (HEAD -> master)
Author: yongzhilu <1046803954@qq.com>
Date:   Fri Jul 15 21:51:23 2022 +0800

    third commit
  • git log --pretty=子选项
# --pretty=子选项
# --pretty。 这个选项可以指定使用不同于默认格式的方式展示提交历史
# oneline 将每个提交放在一行显示,查看的提交数很大时非常有用
git log --pretty=xxxxx

C:\Users\zsf\Desktop\sd\git-examples>git log --pretty=oneline
856052f2a8cc9bc36426445e23cb239ac3299691 (HEAD -> master) third commit
af84262b950505f20aac11566dd211ba209180df zxc
21775e9bdd1212acdf868c14bedc9cda6399e0a9 (origin/master, origin/HEAD) Initial commit

# format,可以定制要显示的记录格式
git log --pretty=format:"%h - %an, %ar : %s"

C:\Users\zsf\Desktop\sd\git-examples>git log --pretty=format:"%h - %an, %ar : %s"
856052f - yongzhilu, 41 minutes ago : third commit
af84262 - yongzhilu, 81 minutes ago : zxc
21775e9 - yongzhilu, 86 minutes ago : Initial commit
  • git log --graph
# --graph  这个选项添加了一些ASCII字符串来  形象地展示你的分支、合并历史:
git log --graph

C:\Users\zsf\Desktop\sd\git-examples>git log --graph
* commit 856052f2a8cc9bc36426445e23cb239ac3299691 (HEAD -> master)
| Author: yongzhilu <1046803954@qq.com>
| Date:   Fri Jul 15 21:51:23 2022 +0800
|
|     third commit
|
* commit af84262b950505f20aac11566dd211ba209180df
| Author: yongzhilu <1046803954@qq.com>
| Date:   Fri Jul 15 21:11:37 2022 +0800
|
|     zxc
|
* commit 21775e9bdd1212acdf868c14bedc9cda6399e0a9 (origin/master, origin/HEAD)
  Author: yongzhilu <1046803954@qq.com>
  Date:   Fri Jul 15 13:06:25 2022 +0000

      Initial commit
  • git log --since/--after
# --since/--after
# 仅显示指定时间之后的提交
git log --since/--after=xxxx

# 查询2周内的提交记录
C:\Users\zsf\Desktop\sd\git-examples>git log --since=2.weeks
commit 856052f2a8cc9bc36426445e23cb239ac3299691 (HEAD -> master)
Author: yongzhilu <1046803954@qq.com>
Date:   Fri Jul 15 21:51:23 2022 +0800

    third commit

# 查询2022-07-14之后的提交记录
C:\Users\zsf\Desktop\sd\git-examples>git log --after="2022-07-14"
commit ac658f135308a65895cc984325fb51ad7b65403e (HEAD -> master)
Author: yongzhilu <1046803954@qq.com>
Date:   Fri Jul 15 23:04:12 2022 +0800

    zxc
  • git log --until/--before
# --until/--before
# 仅显示指定时间之前的提交。
git log --since/--before=xxxx

# 查询2022-08-01之前的提交记录
C:\Users\zsf\Desktop\sd\git-examples>git log --before="2022-08-01"
commit ac658f135308a65895cc984325fb51ad7b65403e (HEAD -> master)
Author: yongzhilu <1046803954@qq.com>
Date:   Fri Jul 15 23:04:12 2022 +0800

    zxc
  • git log --author
# --author
# 查询指定作者的提交
git log --author yongzhilu

# 可使用user.name查找
git log --author <user.name>

C:\Users\zsf\Desktop\sd\git-examples>git log --author yongzhilu
commit 856052f2a8cc9bc36426445e23cb239ac3299691 (HEAD -> master)
Author: yongzhilu <1046803954@qq.com>
Date:   Fri Jul 15 21:51:23 2022 +0800

    third commit


# 也可以用user.email查找
git log --author <user.email>

C:\Users\zsf\Desktop\sd\git-examples>git log --author 1046803954@qq.com
commit 856052f2a8cc9bc36426445e23cb239ac3299691 (HEAD -> master)
Author: yongzhilu <1046803954@qq.com>
Date:   Fri Jul 15 21:51:23 2022 +0800

    third commit
  • git log --author="yongzhi1u" --grep="third commit" --all-match
# --all-match
# 注:如果在使用多个查询条件时,如果需要同时满足所有条件,需要加上--all-match选项,否则,满足任意一个条件的提交都会被匹配出来
C:\Users\zsf\Desktop\sd\git-examples>git log --author="yongzhilu" --grep="third commit" --all-match
commit 856052f2a8cc9bc36426445e23cb239ac3299691
Author: yongzhilu <1046803954@qq.com>
Date:   Fri Jul 15 21:51:23 2022 +0800

    third commit
  • git log --grep="third commit"
# --grep
# 根据某个条件进行查询,条件最好用双引号括起来,否则空格会被认为是参数的结束
git log --grep="third commit

C:\Users\zsf\Desktop\sd\git-examples>git log --grep="third commit"
commit 856052f2a8cc9bc36426445e23cb239ac3299691 (HEAD -> master)
Author: yongzhilu <1046803954@qq.com>
Date:   Fri Jul 15 21:51:23 2022 +0800

    third commit
  • git log <file-path>
# path
# 直接查看某个文件/目录的日志,可以在命令的最后指定文件的路径
# 因为是放在最后位置上的选项,所以用两个短划线(--)隔开之前的选项和后面限定的路径名。
C:\Users\zsf\Desktop\sd\git-examples>git log --author="yongzhilu" -- a.txt
commit 856052f2a8cc9bc36426445e23cb239ac3299691
Author: yongzhilu <1046803954@qq.com>
Date:   Fri Jul 15 21:51:23 2022 +0800

    third commit

commit af84262b950505f20aac11566dd211ba209180df
Author: yongzhilu <1046803954@qq.com>
Date:   Fri Jul 15 21:11:37 2022 +0800

    zxc

9. 撤销操作

  • 撤销对文件的暂存(取消git add)

例如,你已经修改了两个文件并且想要将它们作为两次独立的修改提交,但是却意外地输 入了 git add * 暂存了它们两个。 如何只取消暂存两个中的一个呢?

use git restore --staged <file>... to unstage

#========================================================
C:\Users\zsf\Desktop\sd\git-examples>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:   a.txt
        modified:   b.txt

no changes added to commit (use "git add" and/or "git commit -a")
#========================================================
C:\Users\zsf\Desktop\sd\git-examples>git add *
#========================================================
C:\Users\zsf\Desktop\sd\git-examples>git status
On branch master
Your branch is ahead of 'origin/master' by 1 commit.
  (use "git push" to publish your local commits)

Changes to be committed:
  (use "git restore --staged <file>..." to unstage)
        modified:   a.txt
        modified:   b.txt
#========================================================
C:\Users\zsf\Desktop\sd\git-examples>git restore --staged a.txt
#========================================================
C:\Users\zsf\Desktop\sd\git-examples>git status
On branch master
Your branch is ahead of 'origin/master' by 1 commit.
  (use "git push" to publish your local commits)

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

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:   a.txt
  • 回滚对文件的修改(本地文件回滚到上次提交时的版本)

如果你并不想保留对 CONTRIBUTING.md 文件的修改怎么办? 你该如何方便地撤消修改 - 将它还原成上次提交

时的样子(或者刚克隆完的样子,或者刚把它放入工作目录时的样子)?

use git restore <file>... to discard changes in working directory

注:这个命令不能将staged的文件恢复,需要先恢复成unstaged,再恢复成上次提交时的

#========================================================
C:\Users\zsf\Desktop\sd\git-examples>git status
On branch master
Your branch is ahead of 'origin/master' by 1 commit.
  (use "git push" to publish your local commits)

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

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:   a.txt
#========================================================
C:\Users\zsf\Desktop\sd\git-examples>git restore a.txt
#========================================================

C:\Users\zsf\Desktop\sd\git-examples>git status
On branch master
Your branch is ahead of 'origin/master' by 1 commit.
  (use "git push" to publish your local commits)

Changes to be committed:
  (use "git restore --staged <file>..." to unstage)
        modified:   b.txt
  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值