PS ~\aliceWorkspace> new-item error.txt PS ~\aliceWorkspace> git add . PS ~\aliceWorkspace> git status On branch alice Changes to be committed: (use "git restore --staged <file>..." to unstage) new file: error.txt # 方法一:撤销error.txt对暂存区的git add提交 PS ~\aliceWorkspace> git restore --staged .\error.txt PS ~\aliceWorkspace> git status On branch alice Untracked files: (use "git add <file>..." to include in what will be committed) error.txt nothing added to commit but untracked files present (use "git add" to track) # 方法二:保留工作区的error.txt文件,但删除其在暂存区中的缓存 # 使用 PS ~\aliceWorkspace> git rm--cached .\error.txt rm'error.txt' PS ~\aliceWorkspace> git status On branch alice Untracked files: (use "git add <file>..." to include in what will be committed) error.txt nothing added to commit but untracked files present (use "git add" to track) # 方法三:同时删除工作区的error.txt文件和其在暂存区中的缓存 PS ~\aliceWorkspace> git rm-f .\error.txt rm'error.txt' PS ~\aliceWorkspace> git status On branch alice nothing to commit, working tree clean PS ~\aliceWorkspace> ls Mode LastWriteTime Length Name --------------------------- d-----2022/9/1616:39 aliceInfo -a----2022/9/1615:2916 nameList.txt -a----2022/9/1612:1346 README.md -a----2022/9/1614:4650 task.doc
commit a8dd21a0a01907bb801ccfc874d1fdf2c36fe57c Author: WilsonGoGo <[email protected]> Date: Fri Sep 1616:07:492022 +0800 first edit to aliceInfo/workList.txt ... # 回滚到a8dd...的commit快照 PS ~\aliceWorkspace> git reset a8dd Unstaged changes after reset: D aliceInfo/workList.txt # 再次检查log记录 PS ~\aliceWorkspace> git log commit a8dd21a0a01907bb801ccfc874d1fdf2c36fe57c (HEAD -> alice) Author: WilsonGoGo <[email protected]> Date: Fri Sep 1616:07:492022 +0800 first edit to aliceInfo/workList.txt ...
回滚后的可视化结果
但是检查发现,文件并没有回到工作区,需要继续用git restore将工作区文件从快照切换回来
1 2 3 4 5 6 7 8 9 10 11 12 13 14
# 先通过git status查看 PS ~\aliceWorkspace> git status On branch alice 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: aliceInfo/workList.txt no changes added to commit (use "git add" and/or "git commit -a") # 使用restore命令恢复工作区文件 PS ~\aliceWorkspace> git restore .\aliceInfo\workList.txt # 注: # restore命令是从暂存区撤销改动 # 而我们之前虽然使用git reset回滚了commit快照,但是这并没有影响到工作区已经删除了workList.txt的现状 # 因此需要用restore从commit快照中恢复工作区缺少的文件
示例七:合并分支
1.简单合并分支
alice现在希望将之前的修改合并至dev分支上
1 2 3 4 5 6 7 8 9 10 11 12
# 切换至dev分支 PS ~\aliceWorkspace> git switch dev Switched to branch 'dev' # 将alice分支合并至主分支上 PS ~\aliceWorkspace> git merge alice Updating f712190..a8dd21a Fast-forward aliceInfo/workList.txt | Bin 0 -> 60 bytes nameList.txt | Bin 0 -> 16 bytes 2 files changed, 0 insertions(+), 0 deletions(-) create mode 100644 aliceInfo/workList.txt create mode 100644 nameList.txt
PS ~\aliceWorkspace> git switch dev Switched to branch 'dev' Your branch is ahead of 'origin/dev' by 1 commit. (use "git push" to publish your local commits) PS ~\aliceWorkspace> git merge alice Updating 03c3682..3ebcf1e Fast-forward aliceInfo/workList.txt | Bin 60 -> 144 bytes 1 file changed, 0 insertions(+), 0 deletions(-)
检查可视化结果,可以看到两分支最终再次交汇
3.合并冲突分支
先在dev分支和alice分支上都做出更改,但是这次对task.doc更改的内容是冲突的
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
# dev上更改 PS ~\aliceWorkspace> "working on dev is fine" >> .\task.doc PS ~\aliceWorkspace> git add . PS ~\aliceWorkspace> git commit -m"working on dev is fine" [devf8a5a91] working on dev is fine 1 file changed, 0 insertions(+), 0 deletions(-) # alice上更改 PS ~\aliceWorkspace> git switch alice Switched to branch 'alice' PS ~\aliceWorkspace> "do not directly work on dev" >> .\task.doc PS ~\aliceWorkspace> " complete" >> .\aliceInfo\workList.txt PS ~\aliceWorkspace> git add . PS ~\aliceWorkspace> git commit -m"don't directly work on dev" [alice83c0743] don't directly working on dev 2 files changed, 0 insertions(+), 0 deletions(-)
尝试合并会报错
1 2 3 4 5
PS ~\aliceWorkspace> git merge dev warning: Cannot merge binary files: task.doc (HEAD vs. dev) Auto-merging task.doc CONFLICT (content): Merge conflict in task.doc Automatic merge failed; fix conflicts and then commit the result.