开发的过程中,有时之前开发的功能出现了BUG,但是本地又在相同的文件中开发了新的需求时,需要将代码还原再修复BUG。可是代码还原的话大大提高了二次开发的成本。 通过git stash这个命令可以轻松实现。
git stash用法
git stash是将本地当前未提交的内容暂存起来并且将修改的文件还原到修改之前的状态,用于后续恢复当前的工作,不会被git push到远程分支。
$ git status
On branch branch
Your branch is up to date with 'origin/branch'.
Changes not staged for commit:
(use "git add ..." to update what will be committed)
(use "git checkout -- ..." to discard changes in working directory)
modified: path/filename
$ git stash
Saved working directory and index state WIP on branch: commitId commitMessage
$ git status
On branch branch
Your branch is up to date with 'origin/branch'.
nothing to commit, working tree clean
这样我们就可以在不还原新的开发的情况下去处理已知的BUG了。
git stash pop删除暂存并恢复暂存的内容
使用git stash pop来恢复之前暂存的内容。
$ git stash pop
On branch branch
Your branch is up to date with 'origin/branch'.
Changes not staged for commit:
(use "git add ..." to update what will be committed)
(use "git checkout -- ..." to discard changes in working directory)
modified: path/filename
no changes added to commit (use "git add" and/or "git commit -a")