git stash的预期用例是什么? [英] What is the intended use-case for git stash?

查看:85
本文介绍了git stash的预期用例是什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果我在分支A上工作并且突然需要在分支B上进行工作,然后才准备对分支A进行提交,那么我会将更改存储在A上,签出B,在那做我的工作,然后签出A并应用该存储.

If I work on branch A and suddenly need to work on branch B before being ready with a commit on branch A, I stash my changes on A, checkout B, do my work there, then checkout A and apply the stash.

如果我在A上工作并且想停止一天的工作,我应该存放我的工作,然后在第二天(当我恢复工作时)应用它,还是应该保留原样?未提交的修改工作目录中的文件?我看不出为什么在这种情况下需要使用存储,除非有一些安全性好处.

If I work on A and I want to stop working for the day, should I stash my work and then apply it the next day (when I resume my work), or should I just leave things as they are—uncommitted modified files in the working directory? I don't see why I would need to use stash in this case, except if there is some security benefit.

还有另一种情况:我在工作中和在家中都工作.如果我想回家时还没有准备好提交,我可以将工作存放起来,将其推送到GitHub,然后再将其存放在家里吗?

Also, another scenario: I work both at work and at home. If I am not ready with a commit when I want to go home, can I stash my work, push it to GitHub and then pull that stash at home?

推荐答案

隐藏只是一种便捷的方法.由于分支是如此便宜且易于在git中管理,因此我个人几乎总是更喜欢创建一个新的临时分支,而不是存储,但这主要是个人喜好.

Stash is just a convenience method. Since branches are so cheap and easy to manage in git, I personally almost always prefer creating a new temporary branch than stashing, but it's a matter of taste mostly.

我喜欢隐藏的一个地方是,如果发现我在上一次提交中忘记了某些东西,并且已经开始处理同一分支中的下一个:

The one place I do like stashing is if I discover I forgot something in my last commit and have already started working on the next one in the same branch:

# Assume the latest commit was already done
# start working on the next patch, and discovered I was missing something

# stash away the current mess I made
git stash save

# some changes in the working dir

# and now add them to the last commit:
git add -u
git commit --amend

# back to work!
git stash pop

这篇关于git stash的预期用例是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆