# 发现所有文件不小心被添加到暂存区 root@MagicBookPro:~/git-learn# git status Changes to be committed: (use "git rm --cached <file>..." to unstage) new file: READEME.md new file: project.config
# 查看status, 发现 project.config 从暂存区中移除, 未被 tracked root@MagicBookPro:~/git-learn# git status Changes to be committed: (use "git rm --cached <file>..." to unstage) new file: READEME.md Untracked files: (use "git add <file>..." to include in what will be committed) project.config
root@MagicBookPro:~/git-learn# cat READEME.md This is first commit root@MagicBookPro:~/git-learn# git add . root@MagicBookPro:~/git-learn# vim READEME.md root@MagicBookPro:~/git-learn# cat READEME.md test restore root@MagicBookPro:~/git-learn# git restore READEME.md root@MagicBookPro:~/git-learn# cat READEME.md This is first commit
root@MagicBookPro:~/git-learn# vim READEME.md root@MagicBookPro:~/git-learn# cat READEME.md asdfsdfsjfklsafjsklfThis is first commit root@MagicBookPro:~/git-learn# git reset --hard HEAD is now at fa317be There is a bug root@MagicBookPro:~/git-learn# cat READEME.md This is first commit
当前代码还未commit, 需要切换到另一个分支修复bug
可以使用 stash, 将工作区暂时保存起来, 等另一个分支完成后再切换回来继续code
1 2 3 4 5 6 7 8
# 贮藏当前代码 git stash # 查看贮藏起来的分支 git stash list # 删除stash git stash pop # 恢复到指定stash git stash apply stash@{0}
# 切换不成功, 有没有提交的 changes # 只有分支指向同一个commit才能切换, 这里为了演示在mater分支上提交了一次 root@MagicBookPro:~/git-learn# git switch bugfix error: Your local changes to the following files would be overwritten by checkout: READEME.md Please commit your changes or stash them before you switch branches. Aborting
# 贮藏当前代码 root@MagicBookPro:~/git-learn# git stash Saved working directory and index state WIP on master: ee009aa 123
# 成功切换 root@MagicBookPro:~/git-learn# git switch bugfix Switched to branch 'bugfix'
# 切换为原分支 root@MagicBookPro:~/git-learn# git switch master Switched to branch 'master'
# 恢复代码 root@MagicBookPro:~/git-learn# git stash apply 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: READEME.md
no changes added to commit (use "git add" and/or "git commit -a")