想象一下下面的场景。我在分支foo
工作,有一些未提交的编辑。我想在分支wip/foo
中提交我的编辑,而不提交或丢失分支foo
的工作。可能吗,我该怎么做?
进展
我最终编写了以下脚本:
#! /bin/bash
# usage: wipscript.sh repo file branch
#
# For example,
# wipscript.sh ~/repo file.sh development
repo=$1
file=$2
branch=$3
cd $repo
hv=`git rev-parse HEAD`
branch_present=`git branch | grep wip/$branch`
# echo $branch_present
# echo "------"
git add $file
git commit -a -m $file
if [[ $branch_present == *"wip/$branch"* ]]
then
# echo "in then"
git checkout wip/$branch;
else
# echo "doing else"
git checkout -b wip/$branch;
fi
git checkout $branch -- $file
git commit -am $file
git checkout $branch
git reset --soft $hv
# echo "Finished."
您可以使用存储机制:
git stash
git checkout wip/foo
git stash pop
你甚至不需要把它们藏起来。
任何分支中都不存在未提交的更改。只需更改分支并提交:
git checkout wip/foo
git commit
如果wip/foo
尚不存在,请使用 git checkout -b wip/foo instead
。