48

If I do this in one of my repositories:

git subtree pull --prefix=frameworks/AquaticPrime --squash AquaticPrime

I get this:

Working tree has modifications.  Cannot add.

If I do this (in the same place, of course):

git status

I get this:

# On branch master
nothing to commit (working directory clean)

I'm not quite sure what's going on here. The git status command implies that I don't have modifications, so I'm assuming that the git subtree pull is referring to modifications in a different branch related to the subtree, but it's not entirely clear.

Can anyone provide enlightenment?

Sam Deane
  • 1,553
  • 1
  • 13
  • 17
  • 3
    From a quick look at the source, `git-subtree` prints that when `git diff-index HEAD --exit-code --quiet` exits "failure", i.e. changes exist. What happens if you run `git diff-index HEAD`? – Cascabel Sep 02 '10 at 03:28
  • 1
    You could also try the `-d` (debug) option. – Cascabel Sep 02 '10 at 03:29
  • 1
    When I run git diff-index HEAD, I get "fatal: ambiguous argument 'HEAD': unknown revision or path not in the working tree.". Similar output if I use the -d debug flag. So the question is, why would HEAD be unknown? – B T May 28 '13 at 01:55
  • 2
    @BT I think you tried to use git subtree in a repo without any commits. Git should be enhanced to give a more useful error in this case. I just added an arbitrary commit to start the repo off, and `subtree` worked after that – Avindra Goolcharan Dec 13 '16 at 14:04
  • I had the same message, but I still needed to pull master and my feature, then merge master to feature before I could pull the recent commits from the subtree. – Jeremy Ray Brown May 11 '21 at 19:38

9 Answers9

45

I just had the same problem. From GIT source, error is thrown when command git diff-index HEAD returns result, even if git status says working tree is clean.

To avoid this error, in my case, I re-checkout the current branch of working tree , and everything seems OK : git checkout <branch>

It's a bit confused, but if someone can explain why ...

Koryonik
  • 2,728
  • 3
  • 22
  • 27
18

git reset --hard fixed it to me

From git reset --help

--hard Resets the index and working tree. Any changes to tracked files in the working tree since are discarded.

Anssi
  • 2,727
  • 1
  • 20
  • 16
  • 2
    use only if you are certain what `git reset --hard` does, you may lose work. maybe `git stash save --include-untracked` would be a safer way to go? (as you could retrieve the stashed work later) – pavol.kutaj Apr 15 '21 at 02:10
14

I got around this now. My problem seemed to me that the repository was brand new: I had never committed anything to it. Once I committed a file to the repo, I was able to move past this error.

However, using the command git subtree add C:\gitTest\repos\subA -d --prefix subA I got the new error:

fatal just how do you expect me to merge 0 trees?

After messing around for a minute, I figured out it requires that a specific revision be passed to it. So this command seems to have succeeded:

git subtree add C:\gitTest\repos\subA HEAD -d --prefix subA

And obviously you don't need the -d debug flag.

B T
  • 57,525
  • 34
  • 189
  • 207
  • 1
    Thank you **very** much!! Your observation regarding problems with empty repository has turned out to be also valid in context of [this question about problems with `git tfs subtree add`](http://stackoverflow.com/questions/25886008/how-to-manage-two-tfs-projects-in-one-git-repository/38081315#38081315). The exact error message was `fatal: ambiguous argument 'HEAD': unknown revision or path not in the working tree.` and it was all about "missing HEAD" on master in empty repo. – quetzalcoatl Jun 28 '16 at 18:18
2

I just had this problem, when I:

  • added a subtree;
  • realized, I added it incorrectly (to a wrong directory);
  • removed it with rm;
  • tried to import it again (to the correct place).

Although puppet diff was -- correctly -- showing nothing, the git diff-index HEAD listed each of the files I just deleted as "deleted" -- even though I never commit-ed (much less push-ed) anything.

I believe, this is a bug in git (using 2.7.0 here)... Bug or not, the work-around is to switch to any other branch (with the usual git checkout ....) and then back to yours. Hope, this helps someone -- even if not the original asker.

Mikhail T.
  • 3,043
  • 3
  • 29
  • 46
0

If you are using Windows, try to use Git Bash instead of PowerShell or cmd.exe. This fixed the problem in my case.

noobar
  • 803
  • 10
  • 15
0

I had the exact same error. This was occurring on my feature branch. The fix:

  1. Pulled from feature branch remote to local
  2. Pulled from master remote to local
  3. Merged local master to feature branch, committed, and pushed to remote
  4. Ran the `git subtree pull --prefix MySubdirection https://my/repository/url remotebranch --squash

I have no clue why I had to get master and merge to my feature branch, but that solved the issues.

Jeremy Ray Brown
  • 1,499
  • 19
  • 23
0

I had this problem with SourceTree. The problem was when I set up the subtree, I was selecting an existing folder. There was a warning but I ignored it. Setting up the subtree again but typing in a new folder name (one that doesn't exist yet) fixed the issue.

Siegfoult
  • 1,844
  • 17
  • 19
0

Ensure that all changes made to your local Git repository have been committed before attempting to execute Git commands that modify your repository, such as git subtree add. This will avoid any issues that might occur due to open commit

-5

Try to pull without --squash as it is described in this stackoverflow question.

Community
  • 1
  • 1
Lars Bilke
  • 4,940
  • 6
  • 45
  • 63
  • 1
    Thanks, I feel kind of rude not up-voting/ticking your answer, but the truth is that it's been so long that I don't use subtree any more, and I can't really test the answer to see if it works! – Sam Deane Jun 18 '12 at 21:15
  • 3
    I have the same problem and removing --squash doesn't solve it. – Bernard Nov 08 '12 at 22:41
  • 2
    I didn't use squash to begin with. In fact I don't even know why you would think it would work because the SO question you posted didn't ask about the message we're getting.. – B T May 28 '13 at 01:49