Git push error [remote rejected] master -> master (branch is currently checked out)

Git push error [remote rejected] master -> master (branch is currently checked out)

The error message error: refusing to update checked out branch: refs/heads/master is emitted by the remote repository and it means youre trying to push code to remote non-bare repository that has different code currently checked out in the working directory. The best way to avoid this problem is to push to bare repositories only – this problem cannot ever happen with a bare repository.

You can simply convert your remote repository to bare repository (there is no working copy in the bare repository – the folder contains only the actual repository data).

Execute the following command in your remote repository folder:

git config --bool core.bare true

Then delete all the files except .git in that folder. And then you will be able to perform git push to the remote repository without any errors.

I just had the same error while I began learning Git. Some of the other answers are clearly not for someone new to Git!

I am going to use non technical terms to get the idea across. Anyway, what is happening is that you have two repositories, one is the original you first made, and the other the work one you just made.

Right now you are in your work repository and are using the master branch. But you also happen to be logged in in your original repository to the same master branch. Now since youre logged in in the original, Git fears you might mess up because you might be working on the original and screw things up. So you need to return to the original repository and do git checkout someotherbranch, and now you can push with no problems.

Git push error [remote rejected] master -> master (branch is currently checked out)

The error message describes what has happened. More modern versions of Git refuse to update a branch via a push if that branch is checked out.

The easiest way to work between two non-bare repositories is either to

  1. always update the repositories by pull (or fetch and merge) or, if you have to,

  2. by pushing to a separate branch (an import branch) and then merging that branch into the master branch on the remote machine.

The reason for this restriction is that the push operation operates only on the remote Git repository, it doesnt have access to the index and working tree. So, if allowed, a push on the checked-out branch would change the HEAD to be inconsistent with the index and working tree on the remote repository.

This would make it very easy to accidentally commit a change that undoes all of the pushed changes and also makes it very difficult to distinguish between any local changes that have not been committed and differences between the new HEAD, the index and the working tree that have been caused by push moving HEAD.

Leave a Reply

Your email address will not be published.