Git – 储藏(git stash)本地更改

有时,一些局部更改还不想提交,那么就可以储藏(git stash)起来。

git stash 是一个更改的堆栈,一个本地文件的更改被储藏,被修改文件恢复原样。等要恢复更改时,可以使用git stash pop,把更改应用于文件。

简单来说,git stash储藏更改,并去除相关文件中的更改,git stash pop恢复更改,并删除储藏堆栈中的更改。

关于git stash, 还其他有一些常用命令:

  • git stash list – 显示储藏堆栈中的更改列表
  • git stash show – 显示储藏条目中的最新更改
  • git stash apply – 作用与git stash pop类似,恢复更改,但是不删除储藏堆栈中的更改。

另一个非常方便的命令是git stash branch {branch NAME},它将创建一个新分支,从当前分支头部开始,并将已储藏的更改应用于该分支。

接下来,我们来解决前一章节中,拉取时本地有未提交更改的问题:

  1. git stash储藏本地更改
  2. git pull拉取远程更改
  3. git stash pop再应用恢复更改

示例

Kevin@qikegu MINGW64 /g/project/git-demo (master)
$ git stash
Saved working directory and index state WIP on master: b8a5b1a Merge branch 'master' of https://github.com/kevinhwu/git-demo

Kevin@qikegu MINGW64 /g/project/git-demo (master)
$ git pull
Auto-merging test1.txt
hint: Waiting for your editor to close the file...
Merge made by the 'recursive' strategy.
 test1.txt | 1 +
 1 file changed, 1 insertion(+)

Kevin@qikegu MINGW64 /g/project/git-demo (master)
$ git stash pop
Auto-merging test1.txt
On branch master
Your branch is ahead of 'origin/master' by 11 commits.
  (use "git push" to publish your local commits)

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:   test1.txt

no changes added to commit (use "git add" and/or "git commit -a")
Dropped refs/stash@{0} (ad2b6afe2f9c0b8e969324209c646c262bdd4478)



浙ICP备17015664号-1 浙公网安备 33011002012336号 联系我们 网站地图  
@2019 qikegu.com 版权所有,禁止转载