如何用git stash保留工作而不提交

76 阅读1分钟

有一个紧急的请求,比如错误修复,你必须切换到它,但又不想提交或丢失你在当前工作分支中的工作。这时,git stash 就派上用场了。欲了解更多信息,请点击git stash链接。

Git 储藏室

inanzzz@inanzzz:~/project$ git branch
  develop
* my-feature-branch
inanzzz@inanzzz:~/project$ git status
On branch my-feature-branch
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:   file_one.txt
	modified:   file_two.txt

no changes added to commit (use "git add" and/or "git commit -a")
inanzzz@inanzzz:~/project$ git stash
Saved working directory and index state WIP on my-feature-branch: 860fcb2 Commit
HEAD is now at 860fcb2 Commit
inanzzz@inanzzz:~/project$ git status
On branch my-feature-branch
nothing to commit, working directory clean

做其他紧急工作

签出到 "develop",做任何你需要做的事情。你可以创建一个工作分支等,并推送它。

inanzzz@inanzzz:~/project$ git checkout develop
Switched to branch 'develop'
inanzzz@inanzzz:~/project$ git branch
* develop
  my-feature-branch

回到原始分支

我们又回到了原来的地方,用git stash apply ,从git stash

inanzzz@inanzzz:~/project$ git checkout my-feature-branch 
Switched to branch 'my-feature-branch'
inanzzz@inanzzz:~/project$ git status
On branch my-feature-branch
nothing to commit, working directory clean
inanzzz@inanzzz:~/project$ git stash apply
On branch my-feature-branch
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:   file_one.txt
	modified:   file_two.txt

no changes added to commit (use "git add" and/or "git commit -a")