1

How can I make git forget that a worktree once contained a branch, say integration-branch.

I have a worktree some-stuff that was based on integration-branch and I did a git squash merge on that branch, which means was reset to actually be integration-branch. However, I deleted the directory (see below). But, whenever I make a new worktree say other-stuff based upon integration-branch and try to do a squash merge, it makes me recreate the worktree directory some-stuff and do the integration there.

I want it instead to checkout integration-branch to the current worktree (other-stuff) and do the squash here and after I have done the push allow me to forget that this worktree (other-stuff, some-stuff) ever existed and ever contained the branch integration-branch.

It won't even let me make a new worktree for "integration-branch" claiming that it is already checked out to "some-stuff" even if the worktree for some-stuff has been deleted with rm -rf some-stuff.

How do I make git forget that some-stuff ever existed? Somewhere, it is tenaciously holding onto that worktree.

I first tried git worktree rm some-stuff. I later tried git checkout dummy and git branch dummy in the somestuff worktree. I have also deleted (rm -rf some-stuff).

FreshD
  • 2,371
  • 2
  • 18
  • 31
intel_chris
  • 369
  • 1
  • 2
  • 10
  • I think people are producing confused answers as they start with the assumption that you have only the one standard work-tree, instead of starting from the assumption that you ran `git worktree add`... – torek Aug 13 '19 at 16:27

4 Answers4

1

So if you merged your integration branch you still have the local and probably also the remote branch there. To get rid of them you have to delete them.

git branch -D integration                # Delete local branch
git push origin --delete integration     # Delete remote branch

No this branch does not exitst anymore and you can create it again with git checkout -b integration based on the current worktree.

Another option if you want to directly reuse your integration branch and you already merged it into master is the following

git checkout integration               # No you are back at you unmerged changes
git reset --hard origin/master         # Now your integration branch has you latest changes on master, so also the merege
git push origin --force integration    # Update the remote integration branch to the new changes
FreshD
  • 2,371
  • 2
  • 18
  • 31
  • I don't want to delete the integration branch it is a branch shared by the team (effectively a sub-master). I just don't want it checked out anywhere. I will try branch -D some-sfuff and see if that helps, – intel_chris Aug 13 '19 at 09:37
  • I will try branch -D some-sfuff and see if that helps, It didn't. It also wouldn't let me git branch -D intergration, saying it was already checkout out to some-stuff. Nor, in the some-stuff directory would it let me do a get checkout integration (claiming it was already checked out). – intel_chris Aug 13 '19 at 09:48
1

To recap a bit, you started with:

git worktree add ../integration-branch

(or something similar) to create a second work-tree somewhere outside the standard work-tree area. That second work-tree existed, and had branch integration-branch checked out.

If you have, since then, removed the second work-tree entirely, simply run:

git worktree prune

in the main work-tree to get Git to notice that the additional work-tree no longer exists, and remove it from the list of other work-trees and their checked-out branches. Note that this will inspect all added work-trees to see if they still exist: if you added five work-trees, all on their own branches, so that six branches are currently considered checked-out, and subsequently removed two of them, the prune will leave you with four checked-out branches.

If you haven't removed the other work-tree, you can move into it and change its HEAD to some other branch, or to no branch at all:

(cd ../integration-branch; git checkout --detach integration-branch)

for instance. (The parentheses here are for bash or any other POSIX-ish shell, to make the parenthesized sub-commands run in a sub-shell so that your main shell remains in the current top-level directory of the main work-tree.)

torek
  • 330,127
  • 43
  • 437
  • 552
  • Close, both worktrees were in the standard work-tree area. However, the prune command sounds like what I was looking for. Thanks. – intel_chris Aug 13 '19 at 16:50
  • This is just my opinion, but: it's not a great idea to put an added work-tree underneath a regular one. Too easy to accidentally add the added work-tree's files to the main repository.... – torek Aug 13 '19 at 17:19
  • I always put the worktrees side-by-side in the area that git has created for them and I never use the main repository. The actual pathnames look something more like this: /repos/myname/branch – intel_chris Aug 13 '19 at 17:36
  • So there would have been /repos/myname/some-stuff and /repos/myname/other-stuff. The branch names omitted all that though and were integration-branch, some-sfuff, and other-stuff. We don't squash merge into master, we only squash into integration-branch and we use a pull from integration-branch when we create a new working branch. – intel_chris Aug 13 '19 at 18:11
0

Do you think filtering the branch against unwanted directory would be an option for you? Remove folder and its contents from git/GitHub's history

I admit that I am not entirely sure if I got your point. My understanding was that you don't want git to keep track of the folder even if it was already added / removed in the past. Then you could also add it to .gitignore if you think this could be an option

Zuk
  • 1
  • 1
0

I found under the root .git directory a worktrees sub-directory and in that sub-directories for some-stuff and other-stuff. Deleting those caused git to forget out those working trees. Apparently, they were not getting removed by the "git worktree rm" command.

I must admit the deleting stuff in the .git directory is not something I like doing, but one of my colleagues suggested looking there.

intel_chris
  • 369
  • 1
  • 2
  • 10