Intellipaat Back

Explore Courses Blog Tutorials Interview Questions
+1 vote
2 views
in DevOps and Agile by (29.3k points)
edited by

I was doing some work in my repository and noticed a file had local changes. I didn't want them anymore so I deleted the file, thinking I can just check out a fresh copy. I wanted to do the Git equivalent of

svn up .

Using git pull didn't seem to work. Some random searching led me to a site where someone recommended doing

git checkout HEAD^ src/

(src is the directory containing the deleted file).

Now I find out I have a detached head. I have no idea what that is. How can I undo?

1 Answer

+2 votes
by (50.2k points)

Now you have detached the head, detached HEAD means you are no longer on that branch. If you need to delete the changes associated with detached HEAD, you need to check out the branch you were on using

git checkout master

 Instead of deleting the file you just do

git checkout --path/to/file

This will restore the file to the state, which is in the index.

And to keep the changes associated with detached HEAD  

git log -n 1 // to display the most recent commit on the detached HEAD and copy the commit hash.

git checkout master

git branch tmp <commit-hash> // to save changes in a new branch named tmp.

To incorporate the changes you made into a master

git merge tmp // form the master branch

Make sure that you should be on the master branch after running git checkout master.

Related questions

+5 votes
1 answer
0 votes
1 answer
+3 votes
1 answer

31k questions

32.8k answers

501 comments

693 users

Browse Categories

...