git stash相关使用

最近在使用Git管理项目工程的时候,遇到了很多问题,也学习到了很多关于Git常见使用的技巧,下面就其中关于Git Stash的用法和大家分享下。


首先,简单介绍下Git Stash命令的用法,详细的用法在man文档中有相关介绍,下面我来说明常见的使用。
git stash: 备份当前的工作区的内容,从最近的一次提交中读取相关内容,让工作区保证和上次提交的内容一致。同时,将当前的工作区内容保存到Git栈中。
git stash pop: 从Git栈中读取最近一次保存的内容,恢复工作区的相关内容。由于可能存在多个Stash的内容,所以用栈来管理,pop会从最近的一个stash中读取内容并恢复。
git stash list: 显示Git栈内的所有备份,可以利用这个列表来决定从那个地方恢复。

git stash clear: 清空Git栈。此时使用gitg等图形化工具会发现,原来stash的哪些节点都消失了。


关于Git Stash的详细解释,适用场合,这里做一个说明:


使用git的时候,我们往往使用branch解决任务切换问题,例如,我们往往会建一个自己的分支去修改和调试代码, 如果别人或者自己发现原有的分支上有个不得不修改的bug,我们往往会把完成一半的代码 commit提交到本地仓库,然后切换分支去修改bug,改好之后再切换回来。这样的话往往log上会有大量不必要的记录。其实如果我们不想提交完成一半或者不完善的代码,但是却不得不去修改一个紧急Bug,那么使用'git stash'就可以将你当前未提交到本地(和服务器)的代码推入到Git的栈中,这时候你的工作区间和上一次提交的内容是完全一样的,所以你可以放心的修 Bug,等到修完Bug,提交到服务器上后,再使用'git stash apply'将以前一半的工作应用回来。也许有的人会说,那我可不可以多次将未提交的代码压入到栈中?答案是可以的。当你多次使用'git stash'命令后,你的栈里将充满了未提交的代码,这时候你会对将哪个版本应用回来有些困惑,'git stash list'命令可以将当前的Git栈信息打印出来,你只需要将找到对应的版本号,例如使用'git stash apply stash@{1}'就可以将你指定版本号为stash@{1}的工作取出来,当你将所有的栈都应用回来的时候,可以使用'git stash clear'来将栈清空。
在这里顺便提下git format
-patch -n , n是具体某个数字, 例如 'git format-patch -1' 这时便会根据log生成一个对应的补丁,如果 'git format-patch -2' 那么便会生成2个补丁,当然前提是你的log上有至少有两个记录。


看过上面的信息,就可以知道使用场合了:当前工作区内容已被修改,但是并未完成。这时Boss来了,说前面的分支上面有一个Bug,需要立即修复。可是我又不想提交目前的修改,因为修改没有完成。但是,不提交的话,又没有办法checkout到前面的分支。此时用Git Stash就相当于备份工作区了。然后在Checkout过去修改,就能够达到保存当前工作区,并及时恢复的作用。


git-stash Manual Page

NAME

git-stash - Stash the changes in a dirty working directory away

SYNOPSIS

git stash list [<options>]git stash show [<stash>]git stash drop [-q|--quiet] [<stash>]git stash ( pop | apply ) [--index] [-q|--quiet] [<stash>]git stash branch <branchname> [<stash>]git stash [save [--patch] [-k|--[no-]keep-index] [-q|--quiet] [<message>]]git stash cleargit stash create

DESCRIPTION

Use git stash when you want to record the current state of theworking directory and the index, but want to go back to a cleanworking directory. The command saves your local modifications awayand reverts the working directory to match the HEAD commit.

The modifications stashed away by this command can be listed withgit stash list, inspected withgit stash show, and restored(potentially on top of a different commit) withgit stash apply.Calling git stash without any arguments is equivalent togit stash save.A stash is by default listed as "WIP on branchname …", butyou can give a more descriptive message on the command line whenyou create one.

The latest stash you created is stored in refs/stash; olderstashes are found in the reflog of this reference and can be named usingthe usual reflog syntax (e.g.stash@{0} is the most recentlycreated stash, stash@{1} is the one before it,stash@{2.hours.ago}is also possible).

OPTIONS

save [--patch] [--[no-]keep-index] [-q|--quiet] [<message>]

Save your local modifications to a new stash, and rungit reset --hard to revert them. The <message> part is optional and gives the description along with the stashed state. For quickly making a snapshot, you can omitboth "save" and <message>, but giving only <message> does not trigger this action to prevent a misspelled subcommand from making an unwanted stash.

If the --keep-index option is used, all changes already added to theindex are left intact.

With --patch, you can interactively select hunks from in the diffbetween HEAD and the working tree to be stashed. The stash entry isconstructed such that its index state is the same as the index stateof your repository, and its worktree contains only the changes youselected interactively. The selected changes are then rolled backfrom your worktree.

The --patch option implies --keep-index. You can use--no-keep-index to override this.

list [<options>]

List the stashes that you currently have. Each stash is listed with its name (e.g. stash@{0} is the latest stash, stash@{1} is the one before, etc.), the name of the branch that was current when the stash was made, and a short description of the commit the stash was based on.

stash@{0}: WIP on submit: 6ebd0e2... Update git-stash documentation
stash@{1}: On master: 9cc0589... Add git-stash

The command takes options applicable to the git logcommand to control what is shown and how. Seegit-log(1).

show [<stash>]

Show the changes recorded in the stash as a diff between the stashed state and its original parent. When no<stash> is given, shows the latest one. By default, the command shows the diffstat, but it will accept any format known togit diff (e.g., git stash show -p stash@{1} to view the second most recent stash in patch form).

pop [--index] [-q|--quiet] [<stash>]

Remove a single stashed state from the stash list and apply it on top of the current working tree state, i.e., do the inverse operation ofgit stash save. The working directory must match the index.

Applying the state can fail with conflicts; in this case, it is notremoved from the stash list. You need to resolve the conflicts by handand callgit stash drop manually afterwards.

If the --index option is used, then tries to reinstate not only the workingtree’s changes, but also the index’s ones. However, this can fail, when youhave conflicts (which are stored in the index, where you therefore can nolonger apply the changes as they were originally).

When no <stash> is given, stash@{0} is assumed.

apply [--index] [-q|--quiet] [<stash>]

Like pop, but do not remove the state from the stash list.

branch <branchname> [<stash>]

Creates and checks out a new branch named <branchname> starting from the commit at which the<stash> was originally created, applies the changes recorded in <stash> to the new working tree and index, then drops the<stash> if that completes successfully. When no <stash> is given, applies the latest one.

This is useful if the branch on which you ran git stash save haschanged enough that git stash apply fails due to conflicts. Sincethe stash is applied on top of the commit that was HEAD at the timegit stash was run, it restores the originally stashed state withno conflicts.

clear

Remove all the stashed states. Note that those states will then be subject to pruning, and may be impossible to recover (seeExamples below for a possible strategy).

drop [-q|--quiet] [<stash>]

Remove a single stashed state from the stash list. When no<stash> is given, it removes the latest one. i.e. stash@{0}

create

Create a stash (which is a regular commit object) and return its object name, without storing it anywhere in the ref namespace.

DISCUSSION

A stash is represented as a commit whose tree records the state of theworking directory, and its first parent is the commit atHEAD whenthe stash was created. The tree of the second parent records thestate of the index when the stash is made, and it is made a child oftheHEAD commit. The ancestry graph looks like this:

       .----W
      /    /
-----H----I

where H is the HEAD commit, I is a commit that records the stateof the index, and W is a commit that records the state of the workingtree.

EXAMPLES

Pulling into a dirty tree

When you are in the middle of something, you learn that there areupstream changes that are possibly relevant to what you aredoing. When your local changes do not conflict with the changes inthe upstream, a simplegit pull will let you move forward.

However, there are cases in which your local changes do conflict withthe upstream changes, andgit pull refuses to overwrite yourchanges. In such a case, you can stash your changes away,perform a pull, and then unstash, like this:

$ git pull
 ...
file foobar not up to date, cannot merge.
$ git stash
$ git pull
$ git stash pop
Interrupted workflow

When you are in the middle of something, your boss comes in anddemands that you fix something immediately. Traditionally, you wouldmake a commit to a temporary branch to store your changes away, andreturn to your original branch to make the emergency fix, like this:

# ... hack hack hack ...
$ git checkout -b my_wip
$ git commit -a -m "WIP"
$ git checkout master
$ edit emergency fix
$ git commit -a -m "Fix in a hurry"
$ git checkout my_wip
$ git reset --soft HEAD^
# ... continue hacking ...

You can use git stash to simplify the above, like this:

# ... hack hack hack ...
$ git stash
$ edit emergency fix
$ git commit -a -m "Fix in a hurry"
$ git stash pop
# ... continue hacking ...
Testing partial commits

You can use git stash save --keep-index when you want to make two ormore commits out of the changes in the work tree, and you want to testeach change before committing:

# ... hack hack hack ...
$ git add --patch foo            # add just first part to the index
$ git stash save --keep-index    # save all other changes to the stash
$ edit/build/test first part
$ git commit -m 'First part'     # commit fully tested change
$ git stash pop                  # prepare to work on all other changes
# ... repeat above five steps until one commit remains ...
$ edit/build/test remaining parts
$ git commit foo -m 'Remaining parts'
Recovering stashes that were cleared/dropped erroneously

If you mistakenly drop or clear stashes, they cannot be recoveredthrough the normal safety mechanisms. However, you can try thefollowing incantation to get a list of stashes that are still in yourrepository, but not reachable any more:

git fsck --unreachable |
grep commit | cut -d\  -f3 |
xargs git log --merges --no-walk --grep=WIP


发布了62 篇原创文章 · 获赞 3 · 访问量 15万+
展开阅读全文

没有更多推荐了,返回首页

©️2019 CSDN 皮肤主题: 大白 设计师: CSDN官方博客

分享到微信朋友圈

×

扫一扫,手机浏览