Git basic workflow

bsr picture bsr · Apr 19, 2010 · Viewed 22.8k times · Source

Possible Duplicate:
git push error '[remote rejected] master -> master (branch is currently checked out)'

I am new to Git and trying to use it for a local grails project.
The steps I followed:

  1. create the grails project
  2. go to the project directory and git init
  3. Add all the files in the project in staging area and commit.
  4. The git status at the repo gives the below message

    BXX@BXX-PC /c/Work/Grails/projects/yyy/tables (master)
    $ git status
    # On branch master
    nothing to commit (working directory clean)
    
  5. Trying to keep it as the master branch, make the changes by cloning the repo, and later push the changes back. For that

  6. In my IDE, checkout the project (IntelliJ).This actually clone the project to another dir.
  7. Make the changes and commit the project
  8. Push the local changes to master.

    15:41:56.249: git push -v origin master
    Pushing to c:/Work/Grails/projects/xxx/tables
    remote: error: refusing to update checked out branch: refs/heads/master        
    remote: error: By default, updating the current branch in a non-bare repository        
    remote: error: is denied, because it will make the index and work tree inconsistent        
    remote: error: with what you pushed, and will require 'git reset --hard' to match        
    remote: error: the work tree to HEAD. 
    

The cloned repo status is

$ git status
# On branch master
# Your branch is ahead of 'origin/master' by 1 commit.
#
nothing to commit (working directory clean)

Please help me with understanding this. Is there a better workflow to follow. I may be able to initialize the repo through Intellij, and try to work on the main branch. Still not sure what is wrong above.

thank you.

Answer

mipadi picture mipadi · Apr 19, 2010

The problem is that you're trying to push to a non-bare repo. A non-bare repo is one which has an associated working tree (that is, the files are actually checked out to disk). By default, Git won't let you push to a non-bare repo; pushing to a non-bare repo only updates Git's internal data structures, and does not alter the working tree (the files on disk), which means that if you then go back to the repo you pushed to and start working on the files, you'll be working on older copies of the files. Naturally this will cause problems when you try to commit your changes.

The best way to do this is to push to a bare repository, which is one created by passing the --bare flag to Git when creating the repo:

$ mkdir new_repo
$ cd new_repo
$ git --bare init

Of course, the bare repo won't have any files checked out, so you can't actually work it in (you'll have to clone it first).

If you're just using a Git repo for local development (and not sharing or serving the Git repo), you don't have to have a remote repo to push to; you can work on a single copy of a local, non-bare repo.