在分支之间切换,保存藏匿?

时间:2016-05-05 18:34:55

标签: git git-stash git-checkout

有时我可以使用等价的git stash && git checkout $branch && git stash pop来记住给定分支的树的状态,并且仅恢复为该特定分支存储的那个。怎么可以轻松实现呢?

2 个答案:

答案 0 :(得分:1)

您还可以使用git-whistlesstash-and-checkout命令。 Git-Whistles是一个Ruby gem,所以如果你安装了Ruby,你可以使用gem install git-whistles安装git-whistles。然后,您只需执行git stash-and-checkout [branch]即可实现您想要做的事情。

为简洁起见,我为git定义了一个shell别名g,为co定义了一个别名stash-and-checkout,因此要隐藏当前分支上的任何未提交的工作,请切换到如果是新分支,并弹出该分支的存储(如果有的话),我只需输入g co [branch]即可。有关git别名设置示例,请参阅github.com/vwal/my-git-extras上的 .gitconfig

答案 1 :(得分:0)

按照设计,git stash会在任何分支上进行临时提交 1 。这样你就可以在任何其他地方使用它。

如果要保存索引和/或工作树的状态并将该状态与特定分支相关联,Git提供的内容是git commit。真的,这就是你所需要的:一个普通的旧提交。 2 你可以"不承认"之后,一旦你回到那个分支,git reset --soft HEAD^

如果你想把它包装成一个包,你可以选择一个你承诺永远不会在"真实"中使用的短提交消息。提交,并为git checkout写一个小包装器(注意,这是非常未经测试的):

#! /bin/sh
# git-swapto: commit any temporary work, swap to another branch;
# automatically de-commits temporary work.

# for git-sh-setup: can work in subdirectory
SUBDIRECTORY_OK=true
. $(git --exec-path)/git-sh-setup

case $# in
1) ;;
*) die "usage: git-swapto <branch>";;
esac

require_work_tree

# From require_clean_work_tree, converted to status;
# assumes work tree exists.
work_tree_is_clean() {
    git update-index -q --ignore-submodules --refresh
    # if diff-files says different, fail (work tree not clean)
    git diff-files --quiet --ignore-submodules || return 1
    # if diff-index says index is different, fail (index not clean)
    git diff-index --cached --quiet --ignore-submodules HEAD || return 1
    # both work tree and index are clean => nothing to commit
    return 0
}

# Force branch name as argument, since we want to do a soft
# reset if there is a temporary commit on it.  Also, require
# that current HEAD name a branch.
case "$(git rev-parse --symbolic-full-name "$1")" in
refs/heads/*) ;;
*) die "`$1': not a branch name";;
esac
git symbolic-ref -q HEAD >/dev/null || die "not currently on a branch"

temp_commit_string="!! temporary commit, do not push"
# test whether HEAD refers to our temporary commit
current_commit_is_temp() {
    local head_text="$(git log --no-walk --pretty=format:%B)"
    test "$head_text" = "$temp_commit_string"
}

# Before leaving this branch, make a temporary commit if
# necessary, amending existing temporary commit if there is one.
# (We should never need --amend, this is just paranoia.)
if ! work_tree_is_clean; then
    echo "note: leaving temp commit behind" 1>&2
    if current_commit_is_temp; then
        git commit -a --amend --no-edit ||
            die "cannot update current temp commit"
    else
        git commit -a -m "$temp_commit_string" --no-edit ||
            die "cannot make temp commit"
    fi
fi

# work tree is now clean, switch to target
git checkout "$1" || die "cannot switch to $1"

# finally, if it is our special temp commit, reset it away
if current_commit_is_temp; then
    git reset --soft HEAD^ || die "failed to unmake temp commit"
fi
# all done, for good or ill...

1 实际上它至少有两次,有时是三次临时提交。这是更多&#34;机制&#34;而不是&#34;政策&#34;虽然,而不是在一个分支上&#34;政策/设计。

2 或者,与git stash一样,如果要保留&#34; index&#34;,请使用两个提交。和&#34;工作树&#34;分别。否则只需像往常一样将内容添加到索引中,然后进行一次提交。