M. Wood
M. Wood

Reputation: 587

initialized gitlab repo has remote detached head

I have initialized a new remote repository using gitlab website, and am trying to push an existing local repo into it via the provided instructions.

$ git init
$ git remote add origin [url]
$ git add .
$ git commit -m "initial commit"
$ git push -u origin master

When I check the status of the remote repo, it appears that I am headless.

$ git checkout origin/master
Note: checking out 'origin/master'.

You are in 'detached HEAD' state. You can look around, make experimental
changes and commit them, and you can discard any commits you make in this
state without impacting any branches by performing another checkout.

Conversely, my local master appears fine.

$ git checkout master
Switched to branch 'master'
Your branch is up to date with 'origin/master'.

I want to stop running around headless (remotely). How do I fix this?

Thanks!

Upvotes: 0

Views: 718

Answers (2)

Greg Bacon
Greg Bacon

Reputation: 139521

When you run

git checkout origin/master

or

git checkout $sha1_hash

then you will be placed in a detached HEAD state in your local repository. The remote-tracking ref origin/master records which commit in history was referenced by origin’s master at the time of your most recent git fetch or git pull.

You almost never want to directly checkout a remote-tracking ref. Instead, use them to give directions to git diff or git log.

To see the commits that have been added to GitLab while you've been off doing something else:

git fetch
git log origin/master..master

To see the individual patches or as one big delta:

git fetch
git diff -p origin/master master
git diff origin/master master

Note that you could be on another branch entirely and still run these commands, no need to stop your work in progress and checkout the master branch just to take a peek like this.

A nonstandard but highly useful alias is git lol for

git log --graph --decorate --pretty=oneline --abbrev-commit

To set up the alias and some nice coloring of git output, add the following to your global git config (~/.gitconfig on Unix-like operating systems).

[alias]
        lol = log --graph --decorate --pretty=oneline --abbrev-commit
        lola = log --graph --decorate --pretty=oneline --abbrev-commit --all
[color]
        branch = auto
        diff = auto
        interactive = auto
        status = auto

This will give you a poor man’s graphical history browser. With the alias set up, run

git lol master origin/master

to see where they are with respect to one another.

Upvotes: 2

Drew Blessing
Drew Blessing

Reputation: 2705

There is nothing wrong with your repository, at least given the information you've shared.

The local, working copy of your repository has its own HEAD, which is master presumably. When you check out origin/master, Git says you're in a detached HEAD state because you've now checked out the latest commit for origin/master instead of your local working copy. Your local repository's HEAD/master may actually point to the same commit as origin/master, but this still results in a detached HEAD state.

However, note that the detached HEAD state is only happening in your checked out, working copy. It's not indicating any problem with the remote, etc.

It may be helpful for you to read the answer(s) in the question/answer How can I reconcile detached HEAD with master/origin?

Upvotes: 1

Related Questions