文章目录 前言 一、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
–
二、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
–
git add 命令, git restore
3.而 git reset HEAD
git restore --staged
欢迎大家讨论,我总结的有问题的话欢迎指正。
