Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

git 问题汇总 #21

Open
yqfang opened this issue Jul 16, 2017 · 0 comments
Open

git 问题汇总 #21

yqfang opened this issue Jul 16, 2017 · 0 comments

Comments

@yqfang
Copy link
Owner

yqfang commented Jul 16, 2017

git stash

backgroud

使用 git 多分支时常常遇到这样的情况

在 develop 分支下开发了一半,突然 master 分支上有一个紧急 bug 需要 fix,这时由于 develop 分支下存在处于 staging 状态的文件或者处于 untracked 的文件。如果要切换到 master 分支且保持当前工作状态不丢失,我们应该怎么做?

通常我们会一股脑地敲

git add .
git commit -am "switch master to fix some bugs"

你看,这是 commit 很搞笑呢!当然这个 commit message 算是好的,大多数像我一样浮躁的人,就胡乱写了。

言归正传,这里真的需要提交吗?

答案是,不需要。因为有 git stash

ganhuo

Stashing takes the dirty state of your working directory – that is, your modified tracked files and staged changes – and saves it on a stack of unfinished changes that you can reapply at any time.

官方对于 git stash 的定义确实是字字珠玑,我们要注意这里几个关键词:

modified, tracked,staged

用大白话说,unstaged or unindexed or untracked 的 file 是不会被 stashed 的

save on a stack, reapply

似乎暴露了什么,没错,git stash 后,我们的当前工作空间的状态就被存在一个栈中。此时我们可以放心地切换到 master 分支做一些修改。然后再切换回来,通过 git stash apply 恢复之前的工作状态。

当然 git stash 还有其他比较高级的用法,参考 reference 进行进一步的学习。

reference

  1. Git Tools - Stashing and Cleaning
  2. Git: How to recover stashed uncommitted changes
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant