Search code examples
gitversion-controlheadgit-detached-head

How to continue project from specific commit and fix HEAD detached issue?


I checkouted specific commit from my project and continued from there, hoping that changes after that commit would be deleted, and that commit I checkouted would be new head. I commited new changes, but I can't push them. I'm still new to git.

What I have done is:

  1. git checkout commit_hash
  2. edited project
  3. git commit -m "new changes"
  4. git push -u origin master

I got:

To https://github.com/myusername/project.git
 ! [rejected]        master -> master (fetch first)
error: failed to push some refs to 'https://github.com/miloradsimic/ISA16.git'
hint: Updates were rejected because the remote contains work that you do
hint: not have locally. This is usually caused by another repository pushing
hint: to the same ref. You may want to first integrate the remote changes
hint: (e.g., 'git pull ...') before pushing again.
hint: See the 'Note about fast-forwards' in 'git push --help' for details.

When I typed git status I got:

HEAD detached from 506f0ec
nothing to commit, working directory clean

I changed project hierarchy, in head commit, so I want to get it back on previous state. I dont want to merge it with head.

I did it this way (easy way, not professional): I had in one folder project with changes, not positioned on head. I downloaded head commit into new folder, and copied all files (except git files) from folder with my changes to this folder. Removed unnecessary files. Added all changes, commited and pushed.

Thanks, Milorad Simic


Solution

  • When we checkout to a commit-hash we are no longer on a branch (detached HEAD).

    HEAD detached from 506f0ec

    To solve it just create a new branch, do changes, do new Commits, Push to remote.

    Checkout to a commit-hash.

    $ git checkout <commit-hash>
    

    You can do changes/commits here and create a new branch later before push to remote (your case here). But I prefer to create a new branch (say, dev) first.

    $ git checkout -b dev
    // do changes here
    
    $ git add .
    $ git commit -m 'message'
    $ git push -u origin HEAD       # push to remote/dev
    

    Merge dev branch into master branch:

    You need to merge dev branch into master branch to get your new changes.

    $ git checkout master
    $ git pull origin dev      # pull 'dev' new changes into 'master', pull = fetch + merge
    
    $ git push origin HEAD     # update origin/master
    

    Replace master branch with dev branch

    If you want to reset master with the changes of dev branch then replace master with dev branch.

    $ git checkout dev
    $ git branch -D master      # delete local master branch
    
    $ git checkout -b master    # create new local/master branch with exactly 'dev's' history
    $ git push -f origin HEAD   # force(-f) push to remote since git history is changed
    

    Note: now master and dev have the same commits/changes.