bwa- bwa- - 1 month ago 13
Git Question

git: checkout work-tree branch from outside

Git Version: 2.5.1.windows.1 (via git-scm.com)

Setup:

Main Repo: Branch_A -> commit -> commit -> commit -> commit
\
Worktree: -> Branch_B -> commit -> commit
\
-> local Branch_C -> commit


I created a second work-tree via

git worktree add /C/repos/testbranch Branch_B


which worked fine

Now what I want to do is switch the worktree's branch back to branch Branch_B from outside after creating local branches (like Branch_C) in the worktree and commiting to them.
Following this I've tried the following:

Checked out branch is Branch_C with one additional commit over Branch_B

git --work-tree=/C/repos/testbranch/ --git-dir=/C/repos/testrepo/.git/ checkout Branch_B


But the output I get is:

error: Your local changes to the following files would be overwritten by checkout:
bla <-- My test file which is COMMITTED and UNCHANGED in Branch_C
Please, commit your changes or stash them before you can switch branches.
Aborting


I tried doing this from different directories, tried adding/omitting --git-dir and some other things - nothing seems to work. What am I doing wrong?

Edit: Checking out the branch via

git checkout Branch_B


while in /C/repos/testbranch/ works fine.




Background: The repo I'm using is cloned from an SVN repo. What I'm trying to achieve is automating the process of "svn rebase"ing with ANT (<exec> task). The first step is to checkout the branch that points to a branch on the SVN repository. Unfortunately, just using
checkout
in the ANT task fails with
not a git repository: $SECOND_WORKTREE_DIR
.

Steps:


  1. checkout $orig_branch

  2. git svn fetch

  3. (optional) git svn find-rev revision of latest build (might not be the last commit)

  4. git rebase $svn-branch_build-rev $orig_branch


Answer

Found a solution.

  1. You do not need to work in your worktree's folder as the repo stays the same. Say if you need to do an SVN fetch you can just do it from the main worktree.
  2. You can circumvent the problem described above by executing the sh.exe installed with git and passing it a command (which can also be a cd):

    <exec executable="C:/Program Files/Git/bin/sh.exe" failonerror="true">
    <arg value="--login"/>
    <arg value="-c"/>
    <arg value=""cd /C/repos/testbranch && git checkout Branch_B""/>
    </exec>