2016-05-05 5 views
0

あるブランチのツリーの状態を覚えておいて、この特定のブランチに対して隠していたものだけを復元するのに相当するgit stash && git checkout $branch && git stash popを使用することもできます。どのように簡単に達成することができますか?デザインによってブランチを保存し、隠しを保存しますか?

答えて

0

git stashは一時的に上の任意の分岐ではありませんをコミットします。これはあなたがどこにでもそれを適用することができるようにです。

インデックスやワークツリーの状態を保存し、その状態を特定のブランチに関連付ける場合は、Gitが提供するものはgit commitです。本当に、それはあなたが必要とするすべてです:普通の古いコミット。 git reset --soft HEAD^を使用して、そのブランチに戻ってくると、後で「コミットしない」ことができます。

これを1つのパッケージにまとめたい場合は、「本当の」コミットで使用しないことを約束した短いコミットメッセージを選択し、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... 

実際には、少なくとも2、そして時には3、一時的なコミットを行います。しかし、これは "ポリシー"よりも "メカニズム"ですが、 "ブランチにはない"はポリシー/デザインです。

それとも、あなたは別に「インデックス」と「仕事・ツリー」を保持したい場合はgit stashと同様に、コミットを使用します。さもなければ、いつものようにインデックスに物を追加してから、1回コミットしてください。

1

stash-and-checkoutコマンドをgit-whistlesとすることもできます。 Git-whistlesはRubyの宝石ですので、Rubyがインストールされている場合は、gem install git-whistlesでgit-whistlesをインストールできます。次に、あなたがしたいことを達成するために単にgit stash-and-checkout [branch]を行うことができます。

は、簡潔にするために、私はとても新しいブランチにコミットされていない現在のブランチ上での作業、スイッチを隠しておくと、(もしあれば)そのブランチのスタッシュをポップするために、stash-and-checkoutのためのgitのためのシェルのエイリアスg、およびgitのエイリアスcoを定義しました単にg co [branch]と入力するだけです。 gitエイリアス設定の例については、.gitconfiggithub.com/vwal/my-git-extras)を参照してください。

関連する問題