开发的过程中,有时之前开发的功能出现了BUG,但是本地又在相同的文件中开发了新的需求时,需要将代码还原再修复BUG。可是代码还原的话大大提高了二次开发的成本。 通过git help -a 获取全部的git命令可以看到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
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")
Dropped refs/stash@{0} (abcb5f4acecedb44a92ff1b6c94542b503af4a01)