简体   繁体   中英

How do I work in one branch, but commit my work to another?

Imagine the following scenario. I work in branch foo and have some uncommited edits. I want to commit my edits in branch wip/foo without committing or loosing work in branch foo . Is it possible, and how do I do it?

Progress

I ended up writing the following script:

#! /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."

You can use the stash mechanism:

git stash
git checkout wip/foo
git stash pop

You don't even need to stash them.

Uncommitted changes don't exist in any branch. Simply change branches and commit:

git checkout wip/foo
git commit

If wip/foo doesn't exist yet, use git checkout -b wip/foo instead .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM