你可能不知道的关于 git-stash 的有用小技巧 已翻译 100%

oschina 投递于 2018/01/29 14:29 (共 8 段, 翻译完成于 01-30)
阅读 4707
收藏 2
Git
0
加载中

If you have been using Git for a while you might have used Git stash. It’s one of the useful features in Git.

Here are some of the useful tricks I learned about Git stash last week.

  1. Git stash save

  2. Git stash list

  3. Git stash apply

  4. Git stash pop

  5. Git stash show

  6. Git stash branch <name>

  7. Git stash clear

  8. Git stash drop

已有 2 人翻译此段
我来翻译

Git stash save

This command is like Git stash. But this command comes with various options. I will discuss some important options in this post.

Git stash with message

git stash save “Your stash message”.

The above command stashes with a message. We will see how this is helpful in a bit.

Stashing untracked files

You can also stash untracked files.

git stash save -u
or
git stash save --include-untracked

已有 2 人翻译此段
我来翻译

Git stash list

Before discussing this command, let me tell you something about how stash works.

When you Git stash or Git stash save, Git will actually create a Git commit object with some name and then save it in your repo.

So it means that you can view the list of stashes you made at any time.

git stash list

See the example below:


You can see the list of stashes made. And the most recent stash made is in the top.

And you can see that the top stash is given a custom message (using Git stash save “message” ).

已有 1 人翻译此段
我来翻译

Git stash apply

This command takes the top most stash in the stack and applies it to the repo. In our case it is stash@{0}

If you want to apply some other stash you can specify the stash id.

Here’s the example:

git stash apply stash@{1}

已有 1 人翻译此段
我来翻译

Git stash pop

This command is very similar to stash apply but it deletes the stash from the stack after it is applied.

Here’s the example:


As you can see the top stash is deleted and stash@{0} is updated with older stash.

Likewise, if you want a particular stash to pop you can specify the stash id.

git stash pop stash@{1}

已有 1 人翻译此段
我来翻译

Git stash show

This command shows the summary of the stash diffs. The above command considers only the latest stash.

Here’s the example:


If you want to see the full diff, you can use

git stash show -p

Likewise with other commands, you can also specify the stash id to get the diff summary.

git stash show stash@{1}

已有 1 人翻译此段
我来翻译

Git stash branch <name>

This command creates a new branch with the latest stash, and then deletes the latest stash ( like stash pop).

If you need a particular stash you can specify the stash id.

git stash branch <name> stash@{1}

This will be useful when you run into conflicts after you’ve applied the stash to the latest version of your branch.

Git stash clear

This command deletes all the stashes made in the repo. It maybe impossible to revert.

已有 1 人翻译此段
我来翻译

Git stash drop

This command deletes the latest stash from the stack. But use it with caution, it maybe be difficult to revert.

You can also specify the stash id.

git stash drop stash@{1}

Hope you got some useful tricks about Git stash.

If you enjoyed the article try to give some claps and share it :) :)

已有 1 人翻译此段
我来翻译
本文中的所有译文仅用于学习和交流目的,转载请务必注明文章译者、出处、和本文链接。
我们的翻译工作遵照 CC 协议,如果我们的工作有侵犯到您的权益,请及时联系我们。
加载中

评论(1)

Tocy
Tocy
跟git stash manual类似,我整理的中文版链接是这个:http://www.cnblogs.com/tocy/p/git-stash-reference.html
返回顶部
顶部