2

I committed a change to my script that completely broke it. I tried restoring to a previous commit in git using this command:

git checkout 2af6889905af99b78f7401cb17b718ba86240c2a aws_utils.sh

But now I'm stuck in a detached head:

git branch -a
* (HEAD detached at 2af6889)

The branch that I am working in is called: update_aws_delete_user_function.

I am not working in the master branch currently.

How do I merge the functional file in the hash 2af6889 back into the main branch called origin/update_aws_delete_user_function?

0

2 Answers 2

2

Detached head means you are no longer on a branch, you have checked out a single commit in the history (in this case the commit previous to HEAD, i.e. HEAD^).

If you want to delete your changes associated with the detached HEAD

You only need to checkout the branch you were on, e.g.

git checkout master

Next time you have changed a file and want to restore it to the state it is in the index, don't delete the file first, just do

git checkout -- path/to/foo

This will restore the file foo to the state it is in the index.

If you want to keep your changes associated with the detached HEAD

  1. Run git log -n 1; this will display the most recent commit on the detached HEAD. Copy-and-paste the commit hash.
  2. Run git checkout master
  3. Run git branch tmp <commit-hash>. This will save your changes in a new branch called tmp.
  4. If you would like to incorporate the changes you made into master, run git merge tmp from the master branch. You should be on the master branch after running git checkout master.

From: Fix a Git detached head?

2
  • Thanks! Sorry if I wasn't clear I am not working in the master branch. So can I just substitute the name of the branch that I am in instead of checking out the master branch? I've updated the OP for additional clarity.
    – bluethundr
    Commented Jul 30, 2018 at 21:35
  • Yeah just substitute your branch's name with master in this case.
    – Zatara7
    Commented Jul 30, 2018 at 21:36
0

There are more complex ways to do this (e.g. revert), but what I would do is:

  1. Copy your script to another location (your desktop in Windows for example).
  2. Check out your update_aws_delete_user_function branch
  3. Put the file back in place
  4. commit (-a)
  5. push your branch

Not the answer you're looking for? Browse other questions tagged or ask your own question.