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

2

5

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

Shipentine answered 28/2, 2017 at 9:47 Comment(0)
L
8

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.

Laryngoscope answered 28/2, 2017 at 9:49 Comment(6)
I created new branch, but now I have 2 branches, master and dev. Can I as end result have only master branch with data from curent dev?Shipentine
Yes, you can. I guess master is your Default branch so, don't delete default branch. Do you want to replace your master branch with dev branch now?Laryngoscope
I have files that I want to delete from master. Could I go again to master head, and in next commit remove remote files that I don't need?Shipentine
Yes you can it. git checkout master, delete the files, git commit -am 'Delete unnecessary files', git push origin HEADLaryngoscope
But that would not apply my new changes. If I apply your commands, I would not have there my last changes. But if I position to master head, clear unnesesary files in next commit, hard copy my changes (files) to that repository and commit that again, it will do what I wanted. Bit that's not suposed to be done like that.Shipentine
You need to merge dev branch into master branch to get your new changes. Updated answer.Laryngoscope
H
-1

You can do git reset --hard <commit-id>

Then make your changes/commit.

Cheers

Hindustani answered 28/2, 2017 at 11:29 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.