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


前言

作为刚转行的软件配置管理员(SCM),一直没搞清楚 git checkout,git restore 和 git reset的用法和区别,正好这几天不忙,简单梳理了一下,终身学习,持续进步。


一、git checkout 用法总结

1.切换与创建分支

git checkout <branch_name > 切换分支
#git switch <branch_name> 切换分支

git checkout -b <branch_name> 创建并切换至分支
#git switch -c <branch_name> 创建并切换至分支

git checkout -b <branch_name>origin/<branch_name> 在本地创建和远程分支对应的分支,本地和远程分支的名称最好一致

2.还原工作区(文件内容)

git checkout – <file_name> 丢弃工作区的修改,并用最近一次的commit内容还原到当前工作区(对文件中内容的操作,无法对添加文件、删除文件起作用

git checkout HEAD^ – <file_name> 将指定commit提交的内容(HEAD^表示上一个版本)还原到当前工作区

git checkout <branch_name> – <file_name> 将指定分支的指定提交内容还原到当前分支工作区

二、git restore 用法总结

git restore --staged <file_name> 将暂存区的修改重新放回工作区(包括对文件自身的操作,如添加文件、删除文件

git restore <file_name> 丢弃工作区的修改(不包括对文件自身的操作,如添加文件、删除文件

三、git reset 用法总结

git reset HEAD <file_name> 丢弃暂存区的修改,重新放回工作区,会将暂存区的内容和本地已提交的内容全部恢复到未暂存的状态,不影响原来本地文件(相当于撤销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 <file>..." 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 <file>..." 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 <file>..." 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 <file>..." 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 <file>..." to update what will be committed)
  (use "git restore <file>..." 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 <file>..." 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 <file>..." to update what will be committed)
  (use "git restore <file>..." 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 – <file_name>是将暂存区的修改重新放回工作区,但只能操作文件内容,不能添加、删除文件;
2.git restore --staged <file_name>相当于撤销git add 命令,git restore <file_name> 是放弃对工作区的修改,对文件内容的操作能使用此命令,但对文件的操作(添加、删除)不起作用;
3.而git reset HEAD <file_name>与 git restore --staged <file_name>的作用相同。

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


评论 16
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值