我什么时候应该使用 git stash?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/20537223/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
When should I use git stash?
提问by Alexander
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 上工作并且突然需要在分支 A 上进行提交之前在分支 B 上工作,我将更改存储在 A 上,结帐 B,在那里完成我的工作,然后结帐 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.
如果我在 A 工作并且我想在当天停止工作,我应该将我的工作藏起来,然后在第二天重新开始工作时应用它,还是应该保持原样 - 工作中未提交的修改文件目录。我不明白为什么在这种情况下我需要使用 stash,除非有一些安全好处。
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?
另外,另一种情况 - 我在工作和家里都工作。如果我想回家时还没有准备好提交,我可以把我的工作藏起来,把它推送到 GitHub,然后把那个藏在家里吗?
回答by Mureinik
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.
Stash 只是一种方便的方法。由于分支在 git 中非常便宜且易于管理,我个人几乎总是喜欢创建一个新的临时分支而不是 stashing,但这主要是一个品味问题。
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
回答by Alexander Yushko
I will break answer on three paragraphs.
我将打破三段的答案。
Part 1:
第1部分:
git stash
(To save your un-committed changes in a "stash". Note:this removes changes from working tree!)
git stash
(将您未提交的更改保存在“存储”中。注意:这会从工作树中删除更改!)
git checkout some_branch
(change to intended branch -- in this case some_branch
)
git checkout some_branch
(更改为预期分支 - 在这种情况下some_branch
)
git stash list
(list stashes)
git stash list
(列出藏品)
You can see:
stash@{0}: WIP on {branch_name}: {SHA-1 of last commit} {last commit of you branch}
stash@{0}: WIP on master: 085b095c6 modification for test
你可以看到:
stash@{0}:{branch_name} 上的 WIP:{SHA-1 of last commit} {last commit of you branch}
stash@{0}:WIP on master:085b095c6 修改测试
git stash apply
(to apply stash to working tree in current branch)
git stash apply
(将 stash 应用到当前分支中的工作树)
git stash apply stash@{12}
(if you will have many stashes you can choose what stash will apply -- in this case we apply stash 12
)
git stash apply stash@{12}
(如果你有很多 stash,你可以选择应用什么 stash —— 在这种情况下我们应用 stash 12
)
git stash drop stash@{0}
(to remove from stash list -- in this case stash 0
)
git stash drop stash@{0}
(从 stash 列表中删除 - 在本例中为 stash 0
)
git stash pop stash@{1}
(to apply selected stash and drop it from stash list)
git stash pop stash@{1}
(应用选定的存储并将其从存储列表中删除)
Part 2:
You can hide your changes with this command but it is not necessary.
You can continue on the next day without stash.
This commands for hide your changes and work on different branches or for implementation some realisation of your code and save in stashes without branches and commitsor your custom case!
And later you can use some of stashes and check wich is better.
第 2 部分:
您可以使用此命令隐藏更改,但这不是必需的。
您可以在没有藏匿的情况下继续第二天。
此命令用于隐藏您的更改并在不同的分支上工作,或者用于实现您的代码的一些实现并保存在没有分支和提交或自定义案例的存储中!
稍后您可以使用一些藏匿处并检查哪个更好。
Part 3:
Stash command for local hide your changes.
If you want work remotely you must commit and push.
第 3 部分:
本地的 Stash 命令隐藏您的更改。
如果你想远程工作,你必须提交和推送。
回答by nzrytmn
The main idea is
主要思想是
Stash the changes in a dirty working directory away
将更改隐藏在脏工作目录中
So Basicallly Stash command keep your some changes that you don't need them or want them at the moment; but you may need them.
所以基本上 Stash 命令会保留一些您目前不需要或想要的更改;但你可能需要它们。
Use git stashwhen you want to record the current stateof the working directory and the index, but want to go back to a clean working directory. The command saves your local modifications away and reverts the working directory to match the HEADcommit.
当您想记录工作目录和索引的当前状态,但又想回到干净的工作目录时,请使用git stash。该命令保存您的本地修改并恢复工作目录以匹配HEAD提交。
回答by Anushil Kumar
You can use the following commands:
您可以使用以下命令:
To save your uncommitted changes
git stash
To list your saved stashes
git stash list
To apply/get back the uncommited changes where x is 0,1,2...
git stash apply stash@{x}
保存未提交的更改
git stash
列出您保存的藏品
git stash list
要应用/取回 x 为 0,1,2 的未提交更改...
git stash apply stash@{x}
Note:
笔记:
To apply a stash and remove it from the stash list
git stash pop stash@{x}
To apply a stash and keep it in the stash list
git stash apply stash@{x}
应用存储并将其从存储列表中删除
git stash pop stash@{x}
应用存储并将其保存在存储列表中
git stash apply stash@{x}
回答by gyorgyabraham
If you hit git stash
when you have changes in the working copy (not in the staging area), git will create a stashed object and pushes onto the stack of stashes (just like you did git checkout -- .
but you won't lose changes). Later, you can pop from the top of the stack.
如果您git stash
在工作副本(不在暂存区域)中有更改时点击,则 git 将创建一个隐藏对象并将其推入存储堆栈(就像您所做的那样,git checkout -- .
但您不会丢失更改)。稍后,您可以从堆栈顶部弹出。
回答by Severin
The stash command will stash any changes you have made since your last commit. In your case there is no reason to stash if you are gonna continue working on it the next day. I would only use stash to undo changes that you don't want to commit.
stash 命令将存储自上次提交以来所做的任何更改。在你的情况下,如果你第二天要继续工作,就没有理由藏起来。我只会使用 stash 来撤消您不想提交的更改。