跳转到主内容
极星编程网:以代码为星,赴技术山海!

一次搞清 git checkout,git restore 和 git reset

文章目录 前言 一、git checkout 用法总结 1.切换与创建分支 2.还原工作区(文件内容) 二、git restore 用法总结 三、git reset 用法总结 四、对比分析 1. restore 与 reset 对比 2. restore 与 checkout 对比 3. reset 与 checkout 对比 前言 作为刚转行的软件配置管理员(SCM),一直没搞清楚 git checkout,git restore 和 git reset的用法和区别,正好这几天不忙,简单梳理了一下,终身学习,持续进步。

一、git checkout 用法总结 1.切换与创建分支 git checkout

切换分支

# git switch

切换分支

git checkout -b

创建并切换至分支

# git switch -c

创建并切换至分支

git checkout -b

origin/

在本地创建和远程分支对应的分支,本地和远程分支的名称最好一致

2.还原工作区(文件内容) git checkout – 丢弃工作区的修改,并用最近一次的commit内容还原到当前工作区( 对文件中内容的操作,无法对添加文件、删除文件起作用 ) git checkout HEAD^ – 将指定commit提交的内容(HEAD^表示上一个版本)还原到当前工作区 git checkout

– 将指定分支的指定提交内容还原到当前分支工作区

二、git restore 用法总结 git restore --staged

将暂存区的修改重新放回工作区(

包括对文件自身的操作,如添加文件、删除文件 ) git restore

丢弃工作区的修改(

不包括对文件自身的操作,如添加文件、删除文件 ) 三、git reset 用法总结 git reset HEAD

丢弃暂存区的修改,重新放回工作区,会将暂存区的内容和本地已提交的内容全部恢复到未暂存的状态,不影响原来本地文件(

相当于撤销git add 操作,不影响上一次commit后对本地文件的修改 ) ( 包括对文件的操作,如添加文件、删除文件 ) git reset –hard HEAD 清空暂存区,将已提交的内容的版本恢复到本地,本地的文件也将被恢复的版本替换( 恢复到上一次commit后的状态,上一次commit后的修改也丢弃 ) 四、对比分析 checkout、restore、reset这三个词刚开始真得把我整懵了,通过以上简单的梳理现在思路清晰了许多,最后通过例子实际操作对比一下,相信对你会有帮助的,感谢你看到这里。

1. restore 与 reset 对比 1.添加文件后想撤销: 新建文件practice1并git add:

$ ls

NXP_MPC5746C_Bootloader_Z4/ $ touch practice1 $ ls NXP_MPC5746C_Bootloader_Z4/ practice1 $ git add practice1 $ git status On branch st_dev Your branch is up to date with 'origin/st_dev'.

Changes to be committed: (use "git restore --staged ..." to unstage) new file: practice1

现在不想要practice1文件了,用git restore --staged practice1命令重新放回工作区:

$ git restore --staged practice1

$ git status On branch st_dev Your branch is up to date with 'origin/st_dev'.

Untracked files: (use "git add ..." to include in what will be committed) practice1

nothing added to commit but untracked files present (use "git add" to track)

或者用git reset HEAD practice1命令重新放回工作区,此时两个命令的功能相同:

$ git reset HEAD practice1

$ git status On branch st_dev Your branch is up to date with 'origin/st_dev'.

Untracked files: (use "git add ..." to include in what will be committed) practice1

nothing added to commit but untracked files present (use "git add" to track)

工作区也不想要了,想删除文件(文件还未被跟踪,直接通过rm删除本地文件):

$ rm practice1

$ ls NXP_MPC5746C_Bootloader_Z4/

2.删除文件后想撤销: 提交文件后,工作区是干净的:

$ git status

On branch st_dev Your branch is ahead of 'origin/st_dev' by 1 commit. (use "git push" to publish your local commits)

nothing to commit, working tree clean

现在删除文件,查看状态:

$ git rm practice1

rm 'practice1' $ ls NXP_MPC5746C_Bootloader_Z4/ $ git status On branch st_dev Your branch is ahead of 'origin/st_dev' by 1 commit. (use "git push" to publish your local commits)

Changes to be committed: (use "git restore --staged ..." to unstage) deleted: practice1

不想删除了,重新放回工作区:

$ git restore --staged practice1

$ git status On branch st_dev Your branch is ahead of 'origin/st_dev' by 1 commit. (use "git push" to publish your local commits)

Changes not staged for commit: (use "git add/rm ..." to update what will be committed) (use "git restore ..." to discard changes in working directory) deleted: practice1

no changes added to commit (use "git add" and/or "git commit -a") $ ls NXP_MPC5746C_Bootloader_Z4/ #现在文件在工作区仍然是删除状态

用git restore practice1 恢复文件到工作区

$ git restore practice1

$ ls NXP_MPC5746C_Bootloader_Z4/ practice1 $ git status On branch st_dev Your branch is ahead of 'origin/st_dev' by 1 commit. (use "git push" to publish your local commits)

nothing to commit, working tree clean

或者用git reset HEAD practice1命令将文件重新放回工作区,此时两个命令的功能相同:

$ git rm practice1

rm 'practice1' $ ls NXP_MPC5746C_Bootloader_Z4/ $ git status On branch st_dev Your branch is ahead of 'origin/st_dev' by 1 commit. (use "git push" to publish your local commits)

Changes to be committed: (use "git restore --staged ..." to unstage) deleted: practice1 $ git reset HEAD practice1 #与git restore practice1作用相同 Unstaged changes after reset: D practice1 $ ls NXP_MPC5746C_Bootloader_Z4/ $ git status On branch st_dev Your branch is ahead of 'origin/st_dev' by 1 commit. (use "git push" to publish your local commits)

Changes not staged for commit: (use "git add/rm ..." to update what will be committed) (use "git restore ..." to discard changes in working directory) deleted: practice1

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

再从工作区恢复文件:

$ git restore practice1

$ ls NXP_MPC5746C_Bootloader_Z4/ practice1

2. restore 与 checkout 对比 3. reset 与 checkout 对比 这两点明天再写,我现在的想法是: 1. git checkout – 是将暂存区的修改重新放回工作区,但只能操作文件内容,不能添加、删除文件; 2. git restore --staged

相当于撤销

git add 命令, git restore

是放弃对工作区的修改,对文件内容的操作能使用此命令,但对文件的操作(添加、删除)不起作用;

3.而 git reset HEAD

与

git restore --staged

的作用相同。

欢迎大家讨论,我总结的有问题的话欢迎指正。

相关文章