我如何在一个分支中工作,但将我的工作提交到另一个分支



想象一下下面的场景。我在分支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

最新更新