Git
Git Errors
Remote Rejected
Branch Checked Out
Version Control

Git push error 'remote rejected master - master branch is currently checked out'

Interview Questions practice on Codemia

Over 8,000 real interview questions from top companies, searchable by company and role.

Browse interview questions

Git is a distributed version control system that allows developers to track changes in source code throughout the development process. One of the common tasks performed in Git is pushing local repository changes to a remote repository. However, sometimes you may encounter an error message such as [remote rejected] master -> master (branch is currently checked out). This article dives deep into this error, explaining why it occurs and how to resolve it efficiently.

Understanding the Error

When you execute a Git push command, you are attempting to transfer your local changes to a remote repository. The error [remote rejected] master -> master (branch is currently checked out) indicates that the target branch you are trying to push to on the remote server is currently checked out, rendering it unable to accept changes.

Causes of the Error

  1. Checked Out Branch on Non-Bare Repositories:
    • If the remote repository is non-bare and the branch you are pushing to is checked out on the server, Git prevents the operation. Non-bare repositories have a working tree, unlike bare repositories which only store Git data and are typically used for collaboration.
  2. Active Working Directory:
    • Some Git servers or setups have the repository checked out to a working directory, thereby locking the branch to avoid inconsistencies.

Resolving the Error

Solution 1: Use a Bare Repository as Remote

A bare repository does not have a working directory, thus enabling multiple developers to push to it without conflicts. To resolve the issue by making sure your remote repository is bare, follow these steps:

  1. Convert Remote Repository to Bare:
    • If you have access to the remote server, convert your existing repository into a bare repository by renaming it from say project to project.git and removing the working directory:
bash
1     mv project project.git
2     cd project.git
3     rm -rf *
4     git init --bare
  1. Set Up the Remote:
    • Change the remote URL to point to your new bare repository:
bash
     git remote set-url origin git@server:project.git

Solution 2: Push to a Different Branch

If converting to a bare repository is not an option, consider pushing to a different branch and then merge once it's convenient:

  1. Create a Different Branch Locally:
bash
   git checkout -b temp-branch
  1. Push the Changes:
bash
   git push origin temp-branch
  1. SSH into the Server and Switch Branch:
    • If you have SSH access, switch to your new branch on the server and merge manually:
bash
     git checkout temp-branch
     git merge master

Solution 3: Force Update with Caution

This method should be used with extreme caution as it can overwrite changes on the server:

bash
git push origin +master

Note: This command overwrites the master branch on the server with your local changes.

Solution 4: Server Configuration

Ensure server hooks or configurations aren’t locking the branch. It may be necessary to adjust hook scripts or Git configurations to permit branch updates.

Key Points Summary

IssueProbable CauseSolution
Local Push ErrorBranch checked out on remote non-bare repoConvert to bare repo or use alternate branch
Remote UpdateBranch cannot be pushed directlyTemporarily push to a different branch
CautionPotential overwriting with force pushUse force push as a last resort
Server ConfigRepository configurations or hooksChecks settings to permit branch operations

Additional Details

Understanding Bare vs. Non-Bare Repositories

A bare repository is crucial for collaboration in Git. They should be used for remote repositories whenever multiple people are working on the same codebase. This allows for branches to be safely updated and managed without interfering with active working directories on the server.

When to Use Force Push

Force pushing should be used sparingly and ideally in a controlled environment or when you are the sole contributor. It alters the history of the repository and can result in loss of changes if used improperly.

Understanding and resolving issues like the Git push error [remote rejected] master -> master (branch is currently checked out) is part of mastering the intricacies of Git. By following the solutions provided above, developers can ensure safer and more efficient repository management.


Related reading
Free course
Beginner
7 lessons
2 hours
Tackling System Design Interview Problems

A short course that equips you with the skills to approach system design interviews methodically.

Start the free course
Basis Lab
No course covers this one yet

Basis Lab writes one for you from a sentence about what you want to be able to do, then teaches it and checks you understood. Your first course is free.

Build my course
Track what you have practised

A free account saves your progress, solutions and study plan across every problem on Codemia.

Interview Questions practice on Codemia

Over 8,000 real interview questions from top companies, searchable by company and role.

Browse interview questions