1、取消文件修改
1、test.txt中加一行,不提交,取消修改
git checkout -- filename
16140@DESKTOP-NF7R8DC MINGW64 /d/笔记和课程/code review/mygit (master)
$ vi test.txt
16140@DESKTOP-NF7R8DC MINGW64 /d/笔记和课程/code review/mygit (master)
$ 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: test.txt
no changes added to commit (use "git add" and/or "git commit -a")
16140@DESKTOP-NF7R8DC MINGW64 /d/笔记和课程/code review/mygit (master)
$ git checkout -- test.txt
16140@DESKTOP-NF7R8DC MINGW64 /d/笔记和课程/code review/mygit (master)
$ cat test.txt
hello world
welcome
2、提交到暂存区,又新增一行不加到暂存区,然后取消修改
16140@DESKTOP-NF7R8DC MINGW64 /d/笔记和课程/code review/mygit (master)
$ vi test.txt
16140@DESKTOP-NF7R8DC MINGW64 /d/笔记和课程/code review/mygit (master)
$ git add .
16140@DESKTOP-NF7R8DC MINGW64 /d/笔记和课程/code review/mygit (master)
$ git status
On branch master
Changes to be committed:
(use "git restore --staged <file>..." to unstage)
modified: test.txt
16140@DESKTOP-NF7R8DC MINGW64 /d/笔记和课程/code review/mygit (master)
$ vi test.txt
16140@DESKTOP-NF7R8DC MINGW64 /d/笔记和课程/code review/mygit (master)
$ git status
On branch master
Changes to be committed:
(use "git restore --staged <file>..." to unstage)
modified: test.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: test.txt
16140@DESKTOP-NF7R8DC MINGW64 /d/笔记和课程/code review/mygit (master)
$ git checkout -- test.txt
16140@DESKTOP-NF7R8DC MINGW64 /d/笔记和课程/code review/mygit (master)
$ cat test.txt
hello world
welcome
hello python
这样可以看出git checkout -- 文件名删除工作区中新修改的内容。
同理git上面给的提示指令git restore 文件名也是删除工作区中新修改的内容
3、修改文件添加到了暂存区,然后取消提交
16140@DESKTOP-NF7R8DC MINGW64 /d/笔记和课程/code review/mygit (master)
$ vi test.txt
16140@DESKTOP-NF7R8DC MINGW64 /d/笔记和课程/code review/mygit (master)
$ git add .
16140@DESKTOP-NF7R8DC MINGW64 /d/笔记和课程/code review/mygit (master)
$ git status
On branch master
Changes to be committed:
(use "git restore --staged <file>..." to unstage)
modified: test.txt
16140@DESKTOP-NF7R8DC MINGW64 /d/笔记和课程/code review/mygit (master)
$ git reset HEAD test.txt
Unstaged changes after reset:
M test.txt
16140@DESKTOP-NF7R8DC MINGW64 /d/笔记和课程/code review/mygit (master)
$ 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: test.txt #红色
no changes added to commit (use "git add" and/or "git commit -a")
可以看出来,git reset HEAD 文件名和git restore --staged 文件名都是将之前加到暂存区的内容从暂存区移除到工作区。
2、实现版本的回退
checkout也能像reset --hard一样实现版本的回退
16140@DESKTOP-NF7R8DC MINGW64 /d/笔记和课程/code review/mygit (master)
$ git log
commit f6e05f43d17b5e15fe54a8797be7366b540f7542 (HEAD -> master)
Merge: 83f067e 015e700
Author: Oliver <16140@qq.com>
Date: Fri Jun 3 13:00:49 2022 +0800
Merge branch 'dev'
commit 015e70086f628f9dd829dc616fdf1aa4bf585815 (dev)
Author: Oliver <16140@qq.com>
Date: Fri Jun 3 12:58:10 2022 +0800
add conflict.txt from dev
commit 83f067ef5a2b7466d21c7dec0845744bed3569d2
Merge: cfabbb2 c649328
Author: Oliver <16140@qq.com>
Date: Fri Jun 3 11:56:26 2022 +0800
fix conflict between master and new branch
16140@DESKTOP-NF7R8DC MINGW64 /d/笔记和课程/code review/mygit (master)
$ git checkout 83f067
Note: switching to '83f067'.
You are in 'detached HEAD' state. You can look around, make experimental
changes and commit them, and you can discard any commits you make in this
state without impacting any branches by switching back to a branch.
If you want to create a new branch to retain commits you create, you may
do so (now or later) by using -c with the switch command. Example:
git switch -c <new-branch-name>
Or undo this operation with:
git switch -
Turn off this advice by setting config variable advice.detachedHead to false
HEAD is now at 83f067e fix conflict between master and new branch
M test.txt
16140@DESKTOP-NF7R8DC MINGW64 /d/笔记和课程/code review/mygit ((83f067e...))
$ cat conflict.txt
hello master
hello new branch
16140@DESKTOP-NF7R8DC MINGW64 /d/笔记和课程/code review/mygit ((83f067e...))
$ git branch
* (HEAD detached at 83f067e)
dev
master
new_branch
可以发现这个时候这个版本不处于任何分支,而是处于一个游离的状态。
修改文件可以把它交到master分支去:
16140@DESKTOP-NF7R8DC MINGW64 /d/笔记和课程/code review/mygit ((83f067e...))
$ vi test.txt
#增加hello branch in detached到文件中
16140@DESKTOP-NF7R8DC MINGW64 /d/笔记和课程/code review/mygit ((83f067e...))
$ git status
HEAD detached at 83f067e
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: test.txt
no changes added to commit (use "git add" and/or "git commit -a")
16140@DESKTOP-NF7R8DC MINGW64 /d/笔记和课程/code review/mygit ((83f067e...))
$ git checkout master
Previous HEAD position was 83f067e fix conflict between master and new branch
Switched to branch 'master'
M test.txt
16140@DESKTOP-NF7R8DC MINGW64 /d/笔记和课程/code review/mygit (master)
$ git branch
dev
* master
new_branch
16140@DESKTOP-NF7R8DC MINGW64 /d/笔记和课程/code review/mygit (master)
$ cat test.txt
hello world
welcome
hello Java
hello branch in detached
3、stash
可以给分支改名字:
16140@DESKTOP-NF7R8DC MINGW64 /d/笔记和课程/code review/mygit (master)
$ git branch -m new_branch new_branch2
16140@DESKTOP-NF7R8DC MINGW64 /d/笔记和课程/code review/mygit (master)
$ git branch
dev
* master
new_branch2
1、先把stash的提示信息显示出来
master分支提交到本地仓库后,切换分支,修改文件,造成master分支和其他分支不一致,会报stash要求。
说明master和新分支不一致,如果马上切换当前的修改会丢失:
16140@DESKTOP-NF7R8DC MINGW64 /d/笔记和课程/code review/mygit (master)
$ 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: test.txt
no changes added to commit (use "git add" and/or "git commit -a")
16140@DESKTOP-NF7R8DC MINGW64 /d/笔记和课程/code review/mygit (master)
$ git add .
16140@DESKTOP-NF7R8DC MINGW64 /d/笔记和课程/code review/mygit (master)
$ git commit -m 'test stash demand'
[master cd0d8ee] test stash demand
1 file changed, 4 insertions(+)
16140@DESKTOP-NF7R8DC MINGW64 /d/笔记和课程/code review/mygit (master)
$ git status
On branch master
nothing to commit, working tree clean
16140@DESKTOP-NF7R8DC MINGW64 /d/笔记和课程/code review/mygit (master)
$ vim test.txt
16140@DESKTOP-NF7R8DC MINGW64 /d/笔记和课程/code review/mygit (master)
$ git checkout new_branch2
Switched to branch 'new_branch2'
16140@DESKTOP-NF7R8DC MINGW64 /d/笔记和课程/code review/mygit (new_branch2)
$ vim test.txt
16140@DESKTOP-NF7R8DC MINGW64 /d/笔记和课程/code review/mygit (new_branch2)
$ git checkout master
error: Your local changes to the following files would be overwritten by checkout:
test.txt
Please commit your changes or stash them before you switch branches.
Aborting
2、stash保存起来
使用stash把临时的修改保存起来,再切换分支:
16140@DESKTOP-NF7R8DC MINGW64 /d/笔记和课程/code review/mygit (new_branch2)
$ git stash
Saved working directory and index state WIP on new_branch2: c649328 new branch fix conflict.txt
16140@DESKTOP-NF7R8DC MINGW64 /d/笔记和课程/code review/mygit (new_branch2)
$ git checkout master
Switched to branch 'master'
16140@DESKTOP-NF7R8DC MINGW64 /d/笔记和课程/code review/mygit (master)
$ vi test.txt
现在是修改master的分支的内容,stash保存,再切换回new branch分支
先显示所有stash信息:
16140@DESKTOP-NF7R8DC MINGW64 /d/笔记和课程/code review/mygit (new_branch2)
$ git checkout master
Switched to branch 'master'
16140@DESKTOP-NF7R8DC MINGW64 /d/笔记和课程/code review/mygit (master)
$ vi test.txt
16140@DESKTOP-NF7R8DC MINGW64 /d/笔记和课程/code review/mygit (master)
$ vi conflict.txt
16140@DESKTOP-NF7R8DC MINGW64 /d/笔记和课程/code review/mygit (master)
$ git checkout new_branch2
error: Your local changes to the following files would be overwritten by checkout:
conflict.txt
Please commit your changes or stash them before you switch branches.
Aborting
16140@DESKTOP-NF7R8DC MINGW64 /d/笔记和课程/code review/mygit (master)
$ git stash
Saved working directory and index state WIP on master: cd0d8ee test stash demand
16140@DESKTOP-NF7R8DC MINGW64 /d/笔记和课程/code review/mygit (master)
$ git checkout new_branch2
Switched to branch 'new_branch2'
16140@DESKTOP-NF7R8DC MINGW64 /d/笔记和课程/code review/mygit (new_branch2)
$ git stash list
stash@{0}: WIP on master: cd0d8ee test stash demand
stash@{1}: WIP on new_branch2: c649328 new branch fix conflict.txt
恢复之前的保存
比如:
git stash pop
git stash apply (stash内容并不删除,需要通过git stash drop stash@{0}手动删除)
git stash pop (恢复的同时将stash内容删除)
比如:git stash apply stash@{0}
如下:
16140@DESKTOP-NF7R8DC MINGW64 /d/笔记和课程/code review/mygit (new_branch2)
$ git stash apply stash@{1}
On branch new_branch2
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: test.txt
no changes added to commit (use "git add" and/or "git commit -a")
16140@DESKTOP-NF7R8DC MINGW64 /d/笔记和课程/code review/mygit (new_branch2)
$ git stash list
stash@{0}: WIP on master: cd0d8ee test stash demand
stash@{1}: WIP on new_branch2: c649328 new branch fix conflict.txt
16140@DESKTOP-NF7R8DC MINGW64 /d/笔记和课程/code review/mygit (new_branch2)
$ git stash drop stash@{1}
Dropped stash@{1} (b9281c78f053e205da4997cf983566c6ebce0ea6)
弹出并删除之前的保存可以用pop,只恢复不删除用stash apply。