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.
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
- 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.
- 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:
- 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
projecttoproject.gitand removing the working directory:
- Set Up the Remote:
- Change the remote URL to point to your new bare repository:
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:
- Create a Different Branch Locally:
- Push the Changes:
- SSH into the Server and Switch Branch:
- If you have SSH access, switch to your new branch on the server and merge manually:
Solution 3: Force Update with Caution
This method should be used with extreme caution as it can overwrite changes on the server:
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
| Issue | Probable Cause | Solution |
| Local Push Error | Branch checked out on remote non-bare repo | Convert to bare repo or use alternate branch |
| Remote Update | Branch cannot be pushed directly | Temporarily push to a different branch |
| Caution | Potential overwriting with force push | Use force push as a last resort |
| Server Config | Repository configurations or hooks | Checks 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
- Git Push ERROR Repository not found
- git push error src refspec main does not match any on linux
- Git push existing repo to a new and different remote repo server?
- git push fails RPC failed; result22, HTTP code 411
- git push fatal no configured push destination
- Git push fatal 'origin' does not appear to be a git repository - fatal Could not read from remote repository.
- git push hangs after Total line
- Git push rejected after feature branch rebase
.png&w=3840&q=75)
Tackling System Design Interview Problems
A short course that equips you with the skills to approach system design interviews methodically.
Start the free courseNo 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 courseTrack 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.