GIT(03)逆向状态流转

状态流转图


  • 各种场景下撤销修改
    在这里插入图片描述

撤销修改


  • 当前本地分支状态
    • 修改 ReleaseNote.txt文件(tracked)
    • 新增test.txt文件(untracked)
git status

On branch bthss_V3.0.4
Your branch is up to date with 'origin/bthss_V3.0.4'.

Changes not staged for commit:
  (use "git add <file>..." to update what will be committed)
  (use "git checkout -- <file>..." to discard changes in working directory)

        modified:   ReleaseNote.txt

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

        test.txt

no changes added to commit (use "git add" and/or "git commit -a")
  • 撤销 (tracked)文件 (git checkout
git checkout <file>
git checkout ReleaseNote.txt   

git status
On branch bthss_V3.0.4
Your branch is up to date with 'origin/bthss_V3.0.4'.

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

        test.txt

nothing added to commit but untracked files present (use "git add" to track)
  • 撤销(untracked)文件(git clean
git clean -f
Removing test.txt

git status
On branch bthss_V3.0.4
Your branch is up to date with 'origin/bthss_V3.0.4'.

nothing to commit, working tree clean

git reset撤销


  • git reset的撤销功能,其实可以覆盖所有的撤销。除了未跟踪状态
  • git reset本质做的事情就是重置HEAD(当前分支的版本顶端)到另外一个commit 版本。
  • 命令参数
    • git reset --soft :重置HEAD到另外一个commit,仅此而已,不会回退暂存区和工作区
    • git reset --mix :默认参数;重置HEAD返回到另外一个commit,并且重置暂存区,保留工作区
    • git reset --hard :重置HEAD返回到另外一个commit,重置所有区,会丢失代码
  • 撤销commit之后的文件,需要指定回退的话需要重置到上一个版本的commitId, 单纯的git reset还是回退到当前commitId是没有意义的。所以用到–soft也是要指定commitId;同样–hard,–参数不带版本号,只能
    • git reset HEAD^,意思是上一个版本;也可以写成HEAD~1;如果你进行了2次commit,想都撤回,可以使用HEAD~2
    • git log先获取到每次提交的commit_id git reset <commit_id> 要撤销本次提交,则要恢复到上个版本提交的commit_id
  • 当前有4个文件
    在这里插入图片描述
  • git status 查看文件状态
git status

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

Changes to be committed:
  (use "git reset HEAD <file>..." to unstage)

        new file:   test-stage.txt

Changes not staged for commit:
  (use "git add <file>..." to update what will be committed)
  (use "git checkout -- <file>..." to discard changes in working directory)

        modified:   ReleaseNote.txt

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

        test-untract.txt
  • git reset --soft 撤销
$ git reset HEAD^ --soft

git status
On branch bthss_V3.0.5
Your branch is up to date with 'origin/bthss_V3.0.5'.

Changes to be committed:
  (use "git reset HEAD <file>..." to unstage)

        modified:   ReleaseNote.txt
        new file:   test-commit.txt
        new file:   test-stage.txt

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

        test-untract.txt
  • git reset --mix 撤销
git reset --mix
Unstaged changes after reset:
M       ReleaseNote.txt

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

Changes not staged for commit:
  (use "git add <file>..." to update what will be committed)
  (use "git checkout -- <file>..." to discard changes in working directory)

        modified:   ReleaseNote.txt

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

        test-stage.txt
        test-untract.txt

no changes added to commit (use "git add" and/or "git commit -a")
  • git reset --hard 撤销
$ git reset --hard
HEAD is now at 1b4d720 test

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

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

        test-untract.txt

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

撤销已推送


这个应该没有办法再撤销了。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值