Can I rebase a Git branch without modifying my working copy?

后端 未结 7 2032
南旧
南旧 2020-12-08 09:16

Suppose I have my \"master\" branch checked out. I\'ve committed some production changes to \"master\", and now I want to rebase my \"experimental\" branch onto the latest m

7条回答
  •  暗喜
    暗喜 (楼主)
    2020-12-08 10:08

    So you want a rebase done on for a branch before you checkout that branch? I really can't see the reason for that, since if you don't checkout that branch you can't work on it. Why do you want to rebase a branch that you don't work on? Do the checkout, it will change your mtime and then do the rebase. The rebase will touch files that are changed and of course you need to rebuild them.

    However, a simple way to solve this is to use an other worktree for the rebase. Just set the enviroment variable GIT_WORK_TREE to an other worktree. Just don't forget to have your HEAD match your worktree.

    Depending on which branch he is at and what's pushed, a push to a non-bare repo can be dangerous. A much better solution is to fetch from the repo with the precious worktree instead. Example:

    ` orgbranch=$(git rev-parse HEAD)

    mkdir /tmp/tmp_wd

    cp -r !(.git) /tmp/tmp_wd

    export GIT_WORK_TREE=/tmp/tmp_wd

    git checkout branch1

    git rebase master

    git checkout $orgbranch

    export GIT_WORK_TREE=

    rm -rf /tmp/tmp_wd`

提交回复
热议问题