Problem with Detached HEAD

I have a detached head problem I need help fixing. I have three branches. ‘master’, ‘Reporting’ and ‘Timeline’.

git branch
* (HEAD detached from 8cef63c)
  Reporting
  Timeline
  master

‘Timeline’ is the branch I was working with and had locally committed and non-committed changes. The current status is:

HEAD detached from 8cef63c
Changes not staged for commit:
  (use "git add <file>..." to update what will be committed)
  (use "git checkout -- <file>..." to discard changes in working directory)

        modified: src/stokerMonitor/TODCellEditor.java
        modified: src/stokerMonitor/TimeLineTableModel.java
        modified: src/stokerMonitor/TimelineConfig.java

Untracked files:
  (use "git add <file>..." to include in what will be committed)

        src/TimelineTable.java
        src/stokerMonitor/ButtonColumn.java
        src/stokerMonitor/CreateReport.java
        src/stokerMonitor/JButtonEditor.java
        src/stokerMonitor/Test.java
        src/stokerMonitor/TimeLineComboBox.java
        src/stokerMonitor/TimeLineComboClass.java
        src/stokerMonitor/TimeLineTableListener.java
        src/stokerMonitor/TimeLineTextClass.java
        src/stokerMonitor/TimelineCellRenderer.java
        src/stokerMonitor/TimelineConfigNew.java
        src/stokerMonitor/TimelineTableModel.java
        src/stokerMonitor/delayButton.java

no changes added to commit (use "git add" and/or "git commit -a")

These files are all associated with ‘Timeline’. When I checkout Timeline I get this:

error: Your local changes to the following files would be overwritten by checkout:
        src/stokerMonitor/TODCellEditor.java
        src/stokerMonitor/TimeLineTableModel.java
        src/stokerMonitor/TimelineConfig.java
Please, commit your changes or stash them before you can switch branches.
error: The following untracked working tree files would be overwritten by checkout:
        src/TimelineTable.java
        src/stokerMonitor/TimeLineComboClass.java
        src/stokerMonitor/TimeLineTextClass.java
Please move or remove them before you can switch branches.
Aborting

I read where I am supposed to checkout master to fix a detached head problem but that doesn’t work either.

error: Your local changes to the following files would be overwritten by checkout:
        src/stokerMonitor/TODCellEditor.java
        src/stokerMonitor/TimeLineTableModel.java
        src/stokerMonitor/TimelineConfig.java
Please, commit your changes or stash them before you can switch branches.
Aborting

These have nothing to do with ‘master’. Can someone help me fix this? TIA.

Hi @gw1500se, are you able to use Git Stash to save your uncommitted changes and switch to your Timeline branch before unstashing your changes?

Thanks that-pat. Yes that got me one step further, I think. The detached head is now fixed but I still have problems with my other branches. If I checkout ‘Reporting’ I get this:

error: The following untracked working tree files would be overwritten by checkout:
        src/stokerMonitor/CreateReport.java
        src/stokerMonitor/TimeLineComboClass.java
        src/stokerMonitor/TimeLineTextClass.java
Please move or remove them before you can switch branches.
Aborting

If I checkout ‘Timeline’ I get this:

error: The following untracked working tree files would be overwritten by checkout:
        src/TimelineTable.java
        src/stokerMonitor/TimeLineComboClass.java
        src/stokerMonitor/TimeLineTextClass.java
Please move or remove them before you can switch branches.
Aborting

Removing them scares me as I don’t want to lose any code. I don’t know why I’m getting that for ‘Reporting’ as all changes in that branch were committed some time ago. I beleive they have even been pushed to the remote repository.

Hi @gw1500se,

What that means is that these new files have not been added to a commit in your current branch. You need to add the files using git add and then commit them to the current branch with git commit.

Git can be a tricky workflow at first, but it will become second nature with practice. I went through the exact same confusion when I was learning Git as I think most people do.

For extra help, you may be interested in the GitHub Guides, especially the Git Handbook.

Thanks!

Although I’m not a git expert, I have been using it for some time. The problem is that I don’t want to commit these changes to the master branch as they don’t belong there. I  they belong to the Timeline branch which I cannot checkout so I cannot commit them.

Hi @gw1500se,

I’m sorry, I totally misread what you wrote. If you are using Git 1.7.7 or above, you should be able to stash untracked files using git stash --include-untracked or git stash save -u. You can also use git add to track the files, stash them, then switch to your Timeline branch and unstash them.

Let me know if that works for you.

Thanks but now I am down to the next layer of the onion. Actually 2 layers but they may be related. I can checkout master and it is up to date. From master I can checkout either of the other 2 branches (Reporting and Timeline) and they are up to date. However, if I am in Reporting and checkout Timeline or vise-versa I get an error:

error: The following untracked working tree files would be overwritten by checkout
        src/stokerMonitor/delayButton.java
Please move or remove them before you can switch branches.
Aborting

I always have to checkout master before going to one of the other branches. If that is normal it sure seems strange. Also, once I checkout Timeline, I want to apply the stashed code.

>git stash list
stash@{0}: WIP on master: bdfc452 Help mods & Config default mods
stash@{1}: WIP on (no branch): 6df1750 [WIP] Timeline Table Input Verifier Complete

Stash 1 is what I want to apply to Timeline but it obviously it is now detached:

>git stash apply 6df1750
'6df1750' is not a stash-like commit

Are these related? How do I fix them? TIAA.

Hi @gw1500se,

Not being able to switch from one branch or another that way definitely isn’t normal. Out of curiosity, did you modify your .gitgnore file at any point? That’s the only thing that comes to my mind which could cause you to have untracked files after you switch to one branch from another in this way.

Could you run a git status on each of your branches and copy the output? I’m interested in seeing what each of your branches look like.

As for applying your Git stash object, could you try using git stash apply stash@{1} and let me know if that works?

Thanks!

There was an error with .gitignoret saying it would be overwritten with a checkout so I removed it. I assumed it would be reinstated with the correct version when I checkout that branch. Here are the status outputs:

>git checkout master
Switched to branch 'master'
Your branch is up-to-date with 'origin/master'.
>git status
On branch master
Your branch is up-to-date with 'origin/master'.
Untracked files:
  (use "git add <file>..." to include in what will be committed)

        target/

nothing added to commit but untracked files present (use "git add" to track)

I don’t know why target is untracked although that directory should be associated with just the Reporting branch, I think.

>git checkout Reporting
Switched to branch 'Reporting'
Your branch is up-to-date with 'origin/Reporting'.
>git status
On branch Reporting
Your branch is up-to-date with 'origin/Reporting'.
nothing to commit, working directory clean

Looks good but I’m suspicious.

>git checkout Timeline
error: The following untracked working tree files would be overwritten by checkout:
        src/stokerMonitor/DelayButton.java
Please move or remove them before you can switch branches.
Aborting
>git checkout master
Switched to branch 'master'
Your branch is up-to-date with 'origin/master'.
>git checkout Timeline
Switched to branch 'Timeline'
Your branch is up-to-date with 'origin/Timeline'.
>git status
On branch Timeline
Your branch is up-to-date with 'origin/Timeline'.
nothing to commit, working directory clean

Don’t know why that file is there but I was not able remove it. Oddly now I can:

>git status
On branch Timeline
Your branch is up-to-date with 'origin/Timeline'.
Changes to be committed:
  (use "git reset HEAD <file>..." to unstage)

        deleted: src/stokerMonitor/DelayButton.java

Finally when I tried to apply your stash suggestion I got a bit further but there should be no conflicts:

>git stash apply stash@{1}
Auto-merging src/stokerMonitor/TimelineConfig.java
CONFLICT (content): Merge conflict in src/stokerMonitor/TimelineConfig.java
CONFLICT (modify/delete): src/stokerMonitor/TimeLineTableModel.java deleted in Updated upstream and modified in Stashed changes. Version Stashed changes of src/
stokerMonitor/TimeLineTableModel.java left in tree.
CONFLICT (modify/delete): src/stokerMonitor/TODCellEditor.java deleted in Updated upstream and modified in Stashed changes. Version Stashed changes of src/stoke
rMonitor/TODCellEditor.java left in tree.

So now I am not sure what I can trust:

>git status
On branch Timeline
Your branch is up-to-date with 'origin/Timeline'.
Changes to be committed:
  (use "git reset HEAD <file>..." to unstage)

        deleted: src/stokerMonitor/DelayButton.java

Unmerged paths:
  (use "git reset HEAD <file>..." to unstage)
  (use "git add/rm <file>..." as appropriate to mark resolution)

        deleted by us: src/stokerMonitor/TODCellEditor.java
        deleted by us: src/stokerMonitor/TimeLineTableModel.java
        both modified: src/stokerMonitor/TimelineConfig.java

Thanks again for your help and patience.

Sorry for bouncing this back to the top but I am still in need of help.

Is this repository public somewhere that we can take a look at it?

No. This is a local repository and the remote repo is on my private server. However if there is any specific commands you need I can provide the output. Note that the local repo is on Windows 7 and the remote is on CentOS 7.

Unfortunately, without being able to see the repo and look at it myself, I’m uncertain what state you’re in. I’ve done a number of experiments locally to see if I can replicate what you’re describing, but I have been unable to. I’m uncertain of how to proceed at this point because I just don’t know what the problem is. The best I can recommend at this point is to back up the local repo you have, clone new from the remote repository and then copy the changes from your backed up local repo over to the new local repo to create a new clean state.

One resource I recommend is Git from the Bottom Up. It was instrumental in helping me understand how Git works at a basic level so that I can get out of situations like this that I find myself in.

Thanks, I guess I don’t have much choice. I’ll give it a try but before I do, how do I get the stashed changes from the backup copy, assuming I get that far?

Hi @gw1500se,

I defer to @lee-dohm’s advice because he’s quite a bit more experienced than I am in the nuts and bolts of Git. My recommendation for getting your stashed data would be to create a new branch from the branch that you originally stashed that from and seeing if you can apply the stash.

Knowing that you removed the .gitignore file at some point is very helpful. Because that file tells Git what to track and what not to track but only operates locally, it can cause a ton of chaos when removed or modified without careful planning.

Thanks!