Search code examples
androidgitandroid-studiogit-detached-head

Push changes based on previous commit?


GitLog

I checked out the commit right before my most recent commit, made some changes, and now I want to overwrite/merge the code I currently have (edited version of "Refined zoom...") with the most recent commit ("Added call API..."). However, Android studio will not allow me to push, because the head is detached. What and I do to fix this? Do I create a new branch and attempt to merge? Delete the latest commit and then try to push? Any help is appreciated.


Solution

  • Even in basic git (command line or such) if you try to do a pull on a detached head it will complain - this is because it does not really know what to do with the merge (you have no branch).

    So, as you suggest, the simplest (IMHO) approach is to create some branch and then merge whatever you need in it (I don't know anything about Android studio so I'll give example in git command line).

    These are the basic steps which apply to branching in general:

    • create branch: git branch new_branch
    • move onto that branch: git checkout new_branch
    • Commit your changes (which are still not committed): git commit -am "my changes" (you may need to add any new files first with git add ...).
    • merge in whatever you want: git merge <some commit hash, branch-name, or tag>
    • sort out any merge issues (if any)
    • then you can push: git push origin new_branch