index and working tree do not reflect changes that are now in HEAD

After my recent git class someone asked this question:

I was trying some remote git repo tests last night, and when I went to push my changes, I received this warning message:

warning: updating the currently checked out branch; this may cause confusion, as the index and working tree do not reflect changes that are now in HEAD.

On remote server I created a directory and did “git init“. Then cloned it from my local machine, did changes, committed, and then push. All seemed straightforward there. Any thoughts?

The issue git is warning you about is that your remote has both the repository and a working copy. That is, on the remote server you have a directory project/ with files in it (the working copy) and the folder project/.git (the repository).

If you push from your local machine to the remote, you will only be updating files in the repository and not the working copy. That is, the non-git files will not be changed. This can be confusing because you might log into the remote after you push an expect the working copy to be different.

To deal with this possible confusion git init provides a --bare option. What this does is create the repository only (no working copy). You can then push and pull from the remote like you might a central svn server.

Let me show an example. Say I have an existing git repository on my local machine and I want to create a new remote to back it up. My workflow would look like this:

mkdir ~/git/newproject.git
cd ~/git/newproject.git
git init --bare
git remote add myserver
git push myserver master

If you want, you could even chain these together as a single command:

ssh "mkdir ~/git/newproject.git && cd ~/git/newproject.git && git init --bare" && echo git remote add myserver me@myservercom:/home/nmurray/git/newproject.git

Hope this helps!

  • Reddit
  • Technorati
  • Twitter
  • Facebook
  • Google Bookmarks
  • HackerNews
  • PDF
  • RSS
This entry was posted in programming. Bookmark the permalink. Post a comment or leave a trackback: Trackback URL.