什么时候应该使用git存储? [英] When should I use git stash?

查看:173
本文介绍了什么时候应该使用git存储?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果我在分支A上工作并突然需要在分支B上进行提交之前需要在分支B上工作,则我在A上存储我的更改,结帐B,在那里完成我的工作,然后结帐A并应用存储。

如果我在A上工作而且我想停止工作一天,我是否应该保留工作,然后在第二天应用它,当我恢复工作时,或者我应该保留原样 - 在工作目录中未提交修改过的文件。我不明白为什么我需要在这种情况下使用存储,除非有一些安全方面的好处。

另外,另一种情况 - 我在工作和工作在家。如果我还没有准备好提交回家时,我可以隐藏我的工作,把它推到GitHub上,然后把它放在家里?

解决方案

隐藏只是一种方便的方法。由于分支机构非常便宜并且易于在git中管理,因此我个人几乎总是倾向于创建一个新的临时分支而不是存储,但这主要是品味问题。



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

 #假设最新的提交已经完成
#开始工作下一个补丁,并发现我缺少一些东西

#将当前乱我做了
git stash保存

#工作目录中的一些更改

#现在将它们添加到最后的提交中:
git add - u
git commit - 修正

#回去工作!
git stash pop


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.

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.

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?

解决方案

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 --ammend

# back to work!
git stash pop

这篇关于什么时候应该使用git存储?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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