如果有未提交的更改,是不能
git pull
的,下面演示一下如何不提交更改,拉取代码,实现Android Studio的Update project
修改一个文件
D:\client>git status
On branch lichen_test
Your branch is up-to-date with 'origin/lichen_test'.
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: xxx.java
no changes added to commit (use "git add" and/or "git commit -a")
说明有一个文件被修改了,还没有add,相当于本地修改。这时,我想拉取服务端最新代码,发现是error的:
D:\client>git pull --rebase
error: Cannot pull with rebase: You have unstaged changes.
因为git pull
只是在本地更改commit之后,才是被允许的。
先看一下之前已经stash过的记录:
D:\client>git stash list
stash@{0}: On lichen_test: Uncommitted changes before Update at 2016/5/17 11:22
然后把刚才的本地修改藏起来stash
:
D:\client>git stash
Saved working directory and index state WIP on lichen_test: 6acf645 something
HEAD is now at 6acf645 something
之后,看一下状态:
D:\client>git status
On branch lichen_test
Your branch is up-to-date with 'origin/lichen_test'.
nothing to commit, working directory clean
D:\client>git stash list
stash@{0}: WIP on lichen_test: 6acf645 something
看到的状态是没有需要commit的,工作空间是干净的;stash的历史记录里又多了一条。
然后执行git pull
就可以了:
D:\client>git pull --rebase
Current branch lichen_test is up to date.
刚才藏起来的更改恢复回来:
D:\client>git stash pop
On branch lichen_test
Your branch is up-to-date with 'origin/lichen_test'.
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: xxx.java
no changes added to commit (use "git add" and/or "git commit -a")
Dropped refs/stash@{0} (c09db35f5eb59ada6770fe6ab6073b4f00bc912a)
看一下历史记录
D:\client>git stash list
stash@{0}: On lichen_test: Uncommitted changes before Update at 2016/5/17 11:22
这样就完成了一次Android Studio的Update工程
总结一下stash命令:
git stash #可用来暂存当前正在进行的工作
git stash pop #从Git栈中读取最近一次保存的内容
git stash list #显示Git栈内的所有备份
git stash clear #清空Git栈
git stash apply stash@{1} #可以将你指定版本号为stash@{1}的工作取出来