在开发完成一个版本后,我们会提交一分干净的代码到分支上,然后新建一个分支,去开发新的功能。
但是,天有不测风云,在新功能做了一部分的时候,还没开发完成,Boss告知,上一个版本有个问题,需要修改。此时,新功能还未开发完成,不想提交。那怎么办?
此时,我们可以使用Git stash命令,命令简介:git stash
git stash
命令用来临时地保存一些还没有提交的工作,以便在分支上不需要提交未完成工作就可以清理工作目录。
$ git stash
warning: LF will be replaced by CRLF in app/src/main/java/com/xxx/xxx/
xxx/ConfigInfo.java.
The file will have its original line endings in your working directory.
warning: LF will be replaced by CRLF in app/src/main/res/layout/item_index_ad.xml.
The file will have its original line endings in your working directory.
Saved working directory and index state WIP on zzy/feature/issue-8: 25d8758 Merg
e pull request #7 from buzzerbeat/zzy/feature/issue-5
HEAD is now at 25d8758 Merge pull request #7 from buzzerbeat/zzy/feature/issue-5
报了两个警告,暂时不用管它。
然后在使用git status命令:
$ git status
On branch zzy/feature/issue-8
Your branch is up-to-date with 'origin/zzy/feature/issue-8'.
nothing to commit, working directory clean
最后一句显示,工作目录是干净的了。此时,我们就可以做一些其他的操作了。
一会后,我们调整完了,需要接着开发新的功能,我们就需要把之前的代码拿出来,之前的代码其实是储藏在栈中了,这时候,需要借助 git stash pop 命令,使代码弹出栈。
$ git stash pop
On branch zzy/feature/issue-8
Your branch is up-to-date with 'origin/zzy/feature/issue-8'.
Changes to be committed:
(use "git reset HEAD <file>..." to unstage)
new file: app/libs/GDTUnionSDK.4.8.527.min.jar
new file: app/src/main/res/layout/item_index_ad.xml
Changes not staged for commit:
(use "git add <file>..." to update what will be committed)
(use "git checkout -- <file>..." to discard changes in working directory)
modified: app/build.gradle
modified: app/src/main/AndroidManifest.xml
...//这里省略,因为我修改了很多。
这样,我们又可以接着新功能愉快的开发了。
以上。
附:Git 工具 - 储藏与清理 这里讲的相当完善,不仅仅只这一个方面。
学习更多git使用,戳这里!