0%

git stash 系列命令的使用

  • toc
    {:toc}

Lists

$ > git stash -h
usage: git stash list [<options>]
   or: git stash show [<stash>]
   or: git stash drop [-q|--quiet] [<stash>]
   or: git stash ( pop | apply ) [--index] [-q|--quiet] [<stash>]
   or: git stash branch <branchname> [<stash>]
   or: git stash [save [--patch] [-k|--[no-]keep-index] [-q|--quiet]
                       [-u|--include-untracked] [-a|--all] [<message>]]
   or: git stash clear

$ > man git stash

$ > man git log

git stash list

列出所有暂存文件,
The command takes options applicable to the git log command to control what is shown and how. See git-log(1).

git stash show

$ git stash show 默认第一个 `stash@{0}`
$ git stash show stash@{0} 显示那些文件有改动
$ git stash show -p stash@{0} 显示具体各个文件改动

git stash drop

$ git stash drop 丢弃暂存,默认第一个
$ git stash drop stash@{1} 丢弃第二个暂存

git stash clear

$ git stash clear 清除所有暂存

git stash pop

$ git stash pop 恢复最新的暂存,并从list中删除
$ git stash pop stash@{id} 恢复指定暂存,并从list中删除

git stash apply

$ git stash apply 恢复最新的暂存,不从list中删除
$ git stash apply stash@{id} 恢复指定暂存,不从list中删除

git stash

$ git stash 可用来暂存当前正在进行的工作
$ git stash save "message" 添加信息
$ git stash save -a "message" --all
$ git stash save -u "message" --include-untracked
$ git stash save --keep-index    # save all other changes to the stash

git stash branch

git stash create

git stash store

git stash conflict

CONFLICT

# On branch master
# Unmerged paths:
#   (use "git reset HEAD <file>..." to unstage)
#   (use "git add/rm <file>..." as appropriate to mark resolution)
#
#   both modified:      src/js/globals.tpl.js
no changes added to commit (use "git add" and/or "git commit -a")

Afterwards just run git reset to unstage the changes and keep on hacking – or git add ... and commit.

git reset or git add

暂存文件与更新文件存在冲突解决如下:

$ git stash pop -> CONFLICT
# ...manually resolve conflict(s)
$ git add file

丢弃修改暂存

git st中列出来的文件:

  • [G]绿色:加入暂存区文件,不可以用 git diff
  • [R]红色:未加入暂存区文件,可以用 git diff
$ git stash pop -> CONFLICT
$ git st
位于分支 test
要提交的变更:
  (使用 "git reset HEAD <文件>..." 以取消暂存)

  修改:     bsp/lib/libpanel.a [G]
  修改:     scripts/inc.Makefile.conf.mak [G]

  未合并的路径:
  (使用 "git reset HEAD <文件>..." 以取消暂存)
  (使用 "git add <文件>..." 标记解决方案)

  双方修改:   app/full_screen.c [R]

$ git reset HEAD 恢复提交
重置后取消暂存的变更:
M   solution/app/full_screen.c
M   solution/bsp/lib/libpanel.a
M   solution/scripts/inc.Makefile.conf.mak

$ git st 
位于分支 test
尚未暂存以备提交的变更:
  (使用 "git add <文件>..." 更新要提交的内容)
    (使用 "git checkout -- <文件>..." 丢弃工作区的改动)

    修改:     app/full_screen.c [R]
    修改:     bsp/lib/libpanel.a [R]
    修改:     scripts/inc.Makefile.conf.mak [R]

$ git reset HEAD app/full_screen.c
重置后取消暂存的变更:
M   solution/app/full_screen.c
$ git st
位于分支 test
要提交的变更:
(使用 "git reset HEAD <文件>..." 以取消暂存)

    修改:     bsp/lib/libpanel.a [G]
    修改:     scripts/inc.Makefile.conf.mak [G]

尚未暂存以备提交的变更:
(使用 "git add <文件>..." 更新要提交的内容)
(使用 "git checkout -- <文件>..." 丢弃工作区的改动)

    修改:     app/full_screen.c [R]

$ git reset --hard 回到上次提交时的状态,干净分支
位于分支 test

EXAMPLES

Pulling into a dirty tree

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

Interrupted workflow

# ... 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 or more commits out of the changes in the work tree,
and you want to test each 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'