使用 git stash save 或 git commit 进行本地更改?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/18737640/
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
Use git stash save or git commit for local changes?
提问by Balthier
I changed some files in my repo, but don't want them to be pushed public or create any temporary branch to store them. I just want to save these changes in somewhere. So which command is better:
我更改了我的 repo 中的一些文件,但不希望它们被公开或创建任何临时分支来存储它们。我只想将这些更改保存在某个地方。那么哪个命令更好:
git stash save "save message"
or
或者
git commit -am "save message"
?
?
If I use git commit
, is it true that all of my local commits will be pushed publicly by one git push
command? What if I just want to push one specific commit among them?
如果我使用git commit
,我的所有本地提交是否都将通过一个git push
命令公开推送?如果我只想在其中推送一个特定的提交怎么办?
采纳答案by functionpointer
When pushing, you always push one specific commit (usually the commit at the tip of your currently checked out branch). However, as the commit's hash partly consists of the commits it bases on (its parent commits), you haveto push all parent commits also. And by pushing the parent commits you also have to push their parent commits and so on. So, you can only push the whole history of a specific commit.
推送时,您总是推送一个特定的提交(通常是当前签出分支顶端的提交)。但是,由于提交的哈希部分由它所基于的提交(其父提交)组成,因此您还必须推送所有父提交。通过推送父提交,您还必须推送他们的父提交等等。因此,您只能推送特定提交的整个历史记录。
If you create a commit just to store something but not for pushing, you need to make sure that you never push that commit, nor any commits that base on that commit. To do that, after you have done your work that bases on the temporary commit, you need to squash the temporary commit into the new commit that you create to push it.
如果你创建一个提交只是为了存储一些东西而不是为了推送,你需要确保你永远不会推送那个提交,也没有任何基于该提交的提交。为此,在完成基于临时提交的工作后,您需要将临时提交压缩到您创建的新提交中以推送它。
In other words, yes it is possible to use a commit for temporary, private storage. However, it is much easier to use the stash feature. In fact, the feature is madefor this very use case.
换句话说,是的,可以将提交用于临时的私有存储。但是,使用 stash 功能要容易得多。事实上,该功能做出了这个非常使用情况。
回答by torek
Personally I prefer just going straight to private (local) branches, but stashes work. Be aware of two things about stashes:
我个人更喜欢直接去私人(本地)分支机构,但隐藏工作。请注意有关 stashes 的两件事:
- They are their own commits. Except for the label, there's no fundamental difference between the "stash" commit and a commit tied to a branch or tag label. (A tag label has the form
refs/tags/tag-foo
; a branch has the formrefs/tags/branch-foo
; and the—single—labeled stash commit is labeledrefs/stash
. Of course, branch labels also have the "automatically moves as you add commits" feature, but if you never add more commits there, they never move, so they work just as well to save a single commit.) - The stash "stack"1is implemented using reflogs. Reflogs canexpire—by default most do (after 30 or 90 days), and those in
refs/stash
do not, but you can change this with configuration entries—so stacked stash commits can also "expire" (at the same time the reflog entry expires). (More precisely, they "become collectable", but this distinction is not helpful if they're gone. :-) )
- 他们是他们自己的承诺。除了标签之外,“stash”提交和绑定到分支或标签标签的提交之间没有根本区别。(一个标签标签的形式为
refs/tags/tag-foo
;一个分支的形式为refs/tags/branch-foo
;一个标记为 stash 的提交被标记为refs/stash
。当然,分支标签也有“在你添加提交时自动移动”功能,但如果你从不添加更多提交在那里,它们从不移动,因此它们也能很好地保存单个提交。) - 存储“堆栈” 1是使用引用日志实现的。引用日志可能会过期——默认情况下,大多数会(在 30 或 90 天后),而那些
refs/stash
不会,但您可以使用配置条目更改此设置——因此堆叠存储提交也可以“过期”(同时引用日志条目会过期) . (更准确地说,它们“变得可收藏”,但如果它们消失了,这种区别也无济于事。:-))
The intent with stashes is to save something short-term. If you've ever come back to a repo late and find a bunch of stashes, all named "WIP on branch", it is no fun trying to figure them out.
stash 的目的是为了短期保存一些东西。如果你很晚才回到仓库并发现一堆隐藏的东西,都被命名为“分支上的 WIP”,那么试图找出它们是没有乐趣的。
The other features/bugs :-) stash
provide are:
其他功能/错误:-)stash
提供的是:
git stash branch
lets you change your mind after the fact and turn a stash into a branch. So, if "short term" turns out to be an issue (you were going to fix it this afternoon but now it's been pushed off for at least a month) you can just turn the stash into a branch after all.git stash apply [--index]
will do its best to "re-make" the applied change in the current branch. With--index
it will try to restore both the staged and unstaged changes independently. (There are cases where this is impossible, though.)git stash pop
automatically discards the stash reference for you. Unfortunately it does this even if you meant to usegit stash pop --index
and left out the--index
part. It's easy to lose some of your state (staged vs unstaged) if you usepop
. If you useapply
, and laterdrop
once you're sure you have everything back the way you wanted, you can avoid this problem.
git stash branch
让你事后改变主意,把一个藏匿处变成一个分支。所以,如果“短期”成为一个问题(你今天下午要解决它,但现在它已经被推迟了至少一个月)你毕竟可以把藏匿处变成一个分支。git stash apply [--index]
将尽最大努力“重新制作”当前分支中应用的更改。随着--index
它会尝试恢复双方上演独立不分级的变化。(不过,在某些情况下这是不可能的。)git stash pop
自动为您丢弃存储引用。不幸的是,即使您打算使用git stash pop --index
并忽略该--index
部分,它也会这样做。如果您使用pop
. 如果您使用apply
, 之后drop
一旦您确定一切都按您想要的方式进行,您就可以避免这个问题。
Note that git stash branch
implies --index
: the newly created branch will have staged-and-unstaged changes restored to the way they were when you did the git stash
. (The branch will branch off from the commit you were on when you did the git stash
, too.) Commit the changes (git add
-ing more if desired, or as two separate commits, or whatever) and proceed as if you'd made a private branch in the first place.
请注意,这git stash branch
意味着--index
:新创建的分支会将暂存和未暂存的更改恢复到您执行git stash
. (该分支也将从您执行该操作时所在的提交分支git stash
。)提交更改(git add
如果需要,可以添加更多,或者作为两个单独的提交,或其他)并继续进行,就像您创建了一个私有分支一样首先。
1The expire-able part of the stack consists of all stashes other than stash@{0}
, in git stash list
output.
1堆栈的可过期部分由输出中除stash@{0}
,以外的所有存储组成git stash list
。
回答by Mark A. Donohoe
I do things a little bit differently. Stashes to me are more for quick saves, not daily work since they aren't (easily) granular in what you can actually stash. (i.e. If I have 20 changed files and I want to create two stashes of ten each, it's not easy to do.)
我做事有点不同。对我来说,藏匿处更多是为了快速保存,而不是日常工作,因为它们不是(很容易)细化您实际可以藏匿的内容。(即,如果我有 20 个更改的文件,并且我想创建两个 stash,每个 10 个,这并不容易。)
That's why I want my daily changes committed to an actual, albeit temporary branch only for my personal usage so I can include notes and such of my work as I go. Daily check-ins, experiments, etc. Basically things I don'twant pushed to the final repo.
这就是为什么我希望将我的日常更改提交到一个实际的,尽管只是我个人使用的临时分支,这样我就可以在我进行时包括笔记和我的工作。每日签到功能,实验等基本的东西我不希望被推到了最终回购。
When I'm in a state where I'm ready to commit back to the main repo, I use the 'soft reset' command on the commit I originally branched from. This puts all of my temp-branch-committed changes back as current changes on that original commit without any of my daily working history.
当我处于准备提交回主存储库的状态时,我在最初分支的提交上使用“软重置”命令。这将我所有的临时分支提交的更改都作为原始提交的当前更改而没有我的任何日常工作历史记录。
I then create a new branch for these "new" changes and I can either commit them all at once, or I can break it apart into several commits if it makes sense (i.e. one for the back-end stuff, another for the front-end stuff, another for resources, etc.)
然后我为这些“新”更改创建一个新分支,我可以一次提交它们,或者如果有意义的话我可以将它分解成几个提交(即一个用于后端内容,另一个用于前端 -结束的东西,另一个资源等)
When I'm done, I'm left with a nice, new, clean branch with a history that makes sense to other devs, free of my daily notes, and ready to merge and push back into the main repo. Then I can delete my temp branches and move on to the next task.
当我完成后,我留下了一个漂亮的、新的、干净的分支,它的历史对其他开发者有意义,没有我的日常笔记,并准备合并并推回主仓库。然后我可以删除我的临时分支并继续执行下一个任务。
So to recap...
所以回顾一下...
- Create a working branch
- Make as many commits/sub-branches as you need to get your job done
- When you're ready to merge back without keeping that history, git-reset back to the original commit where you branched. All your changes are now local changes.
- Re-commit and merge as you see fit
- 创建一个工作分支
- 根据完成工作的需要进行尽可能多的提交/子分支
- 当您准备好在不保留该历史记录的情况下合并回来时, git-reset 回到您分支的原始提交。您的所有更改现在都是本地更改。
- 重新提交并按照您认为合适的方式合并
Another benefit is I can actually push the temporary branches to the remote repo so I can work from multiple locations which you can't do with a stash. Just remember when you're done, clean things back up off of the server to keep repo browsing clean. (Some may argue that technically the commits are still there, just detached, which is true, but branches are light-weight in GIT, and in a way, it becomes another safety net for not losing work as you can get back a detached commit if really necessary.)
另一个好处是我实际上可以将临时分支推送到远程存储库,这样我就可以在多个位置工作,而这些位置无法使用 stash。请记住,当你完成后,清理服务器上的备份以保持 repo 浏览干净。(有些人可能会争辩说,从技术上讲,提交仍然存在,只是分离的,这是真的,但分支在 GIT 中是轻量级的,在某种程度上,它成为了另一个不会丢失工作的安全网,因为您可以取回分离的提交如果真的有必要。)
回答by Jatin Malwal
I suggest you to use stashing tool for it. That is why is it here. You can stash your chnges and later add them to your code. There are lots more functionality which you can use with git stash. Here is the link http://git-scm.com/book/en/Git-Tools-Stashing
我建议你使用藏匿工具。这就是为什么它在这里。您可以隐藏您的更改,然后将它们添加到您的代码中。您可以使用 git stash 使用更多功能。这是链接http://git-scm.com/book/en/Git-Tools-Stashing
I would suggest you to once go through the documentation of git here. Also read about the tool. After this you'll become the master of git for sure.
我建议你在这里浏览一下 git 的文档。另请阅读有关该工具的信息。在此之后,您肯定会成为 git 的主人。