Git
Remote Rejected Error
Master Branch
Version Control
Troubleshooting Git Errors

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

Master System Design with Codemia

Enhance your system design skills with over 120 practice problems, detailed solutions, and hands-on exercises.

When working with Git, a version control system, it's not uncommon to encounter various errors that can impede the progress of pushing code to a repository. One such error is [remote rejected] master -> master (branch is currently checked out). This message can be confusing, especially for those who are new to Git. Below, we will break down what this error means, why it occurs, and how to resolve it.

Understanding the Error

The error [remote rejected] master -> master (branch is currently checked out) typically occurs when you try to push changes to a non-bare repository on a branch that is currently checked out. In Git, repositories can be either bare or non-bare:

  • Bare Repository: A bare repository in Git is a repository that doesn’t contain a working directory. It only includes the version history of your project. This type of repository is typically used on servers to handle changes from multiple users because it only contains the Git database.
  • Non-Bare Repository: A non-bare repository, conversely, contains a working directory where files can be edited directly. It holds not only the Git database but also the current version of all files in the project for editing.

The distinction is crucial because a non-bare repository's checked-out branch represents the current working directory and the HEAD points directly to the latest commit in that branch. Attempting to push to this branch while it is checked out can lead to discrepancies and conflicts between the local working state and the incoming changes.

Example Scenario

To illustrate, consider the following example where a user attempts to push to a remote repository that has a checked-out branch:

  1. Developer A has a local clone of the repository and makes changes to the master branch.
  2. Developer B also has a clone and is currently working on the same master branch.
  3. Developer A completes their changes and pushes them to the remote repository which Developer B is using as their central repository. If the master branch is checked out on the server (the remote repository), Git will refuse the push because it changes the branch that is active and might lead to inconsistencies.

Resolving the Error

The optimal way to resolve this error involves a few steps:

  1. Convert the Remote Repository to a Bare Repository: If you control the server, consider converting the remote repository to a bare repository. This can be done by setting the repository's core.bare configuration item to true. You can do this by running the following command in your repository:
bash
   git config --bool core.bare true

After converting to bare, it won’t have a working directory.

  1. Change the Checked-out Branch on the Remote: If you do not have permission or ability to convert the repository to a bare repository, then as a workaround, log into the remote repository, if possible, and switch to a different branch or to a detached HEAD state:
bash
    git checkout --detach
  1. Avoid Pushing to the Checked-out Branch: Always ensure that the branch you are pushing to is not currently checked out on the remote repository.

Summary Table

Issue ComponentDescription Solution
Branch Currently Checked OutPushing to a branch that is active in the working directory on the remote repoSwitch to a non-active branch or detach HEAD in the remote
Non-Bare RepositoryContains a working directory and active branchConvert the repository to a bare repository
Bare RepositoryOnly contains Git’s database and no working directoryIdeal for central repositories to avoid push errors

Conclusion

To prevent the Git push error [remote rejected] master -> master (branch is currently checked out), it is advisable to use bare repositories for central shared repositories. For non-bare repositories, ensure that no branches being pushed to are currently checked out. Understanding these concepts can lead to a smoother workflow and prevent potential conflicts while collaborating on projects using Git.


Course illustration
Course illustration

All Rights Reserved.