This is an article which is written to handle or to solve the problem generated when someone is using Git version control utility to push either files or folders to a Git repository server. The main purpose for executing ‘push command’ is for updating remote refs along with the associated objects. But eventually pushing those files or folders triggered an error message as shown in the title of this article. So, git utility is utilized to push files or folders and suddenly an error message generated : ‘[remote rejected] master->master’. Below is the actual process happened :
- After adding and committing files, folders exist in the folder associated or initialized for Git version control, the last action is to push it to remote Git repository. Below is the command pattern :
git push --set-upstream remote-git-repository-label-name branch-label-name Description : git : It is the command which is executed to call the git version control utility push : It is the operation which is performed to push files and folders to a remote Git repository remote-git-repository-label-name : It is the remote Git repository label defined before branch-label-name : It is the branch name
For an example, the above command pattern will be explained in the following command execution :
user@hostname:~/mysource$ git push --set-upstream myapp master Username for 'http://srv.dev.com': myself Password for 'http://myself@srv.dev.com': Counting objects: 9032, done. Delta compression using up to 4 threads. Compressing objects: 100% (8536/8536), done. Writing objects: 100% (9032/9032), 16.91 MiB | 1.91 MiB/s, done. Total 9032 (delta 2055), reused 0 (delta 0) remote: GitLab: You are not allowed to push code to protected branches on this project. To http://srv.dev.com/gitlab/john.riley/app.git ! [remote rejected] master -> master (pre-receive hook declined) error: failed to push some refs to 'http://srv.dev.com/gitlab/john.riley/app.git' user@hostname:mysource$
The command execution failed because it is rejected. The information which is also given a specific keyword hint is the one given in the above generated output command :
remote: GitLab: You are not allowed to push code to protected branches on this project.
The solution is simply clear, it can be read in the article titled ‘How to Grant Access to Gitlab Repository’ in this link. Just add the associated user which is used to push the files or folders from the local Git repository to have access the remote Git repository. It can be done by reading the article stated previously as further reference. So, in this problem which is generated error message shown above, the solution is quite easy. Just add the privilege of the project to the user which is going to connect for pushing the files and folders already been committed locally.
So, it is the end of the solution given, but if you want to check out what kind of process which I have been going through until I get the actual answer which is done like the one stated in the article ‘How to Grant Access to Gitlab Repository’ for giving the proper access to the repository, below are those things performed but in the end those are failed to solve the problem.
2. But instead of checking the permission on the Gitlab project whether the user given for pushing files or folders has the right to perform it or not, I insist to push it to another refs named ‘origin’.
user@hostname:mysource$ git push --set-upstream myapp origin error: src refspec origin does not match any. error: failed to push some refs to 'http://srv.dev.com/gitlab/john.riley/app.git' user@hostname:~/mysource$
3. The above attempt also ends in vain, the cause of the failure is because there is no available refs named ‘origin’ in the Git remote repository’s project. It can be assumed that this is happened because the remote Git repository URL defined currently available is only ‘master’.
4. This is another attempt taken by fetching the files or folders available as the first step from the remote Git repository as shown below :
user@hostname:~/mysource$ git fetch myapp Password for 'http:// myself@srv.dev.com': Auto packing the repository in background for optimum performance. See "git help gc" for manual housekeeping. user@hostname:~/mysource$
5. There is no specific output displayed after executing the above command. In that case, the execution is performed again by executing the following command for pushing the files and folders related as shown below :
Try to push to the ‘origin’ branch :
user@hostname:~/mysource$ git push --set-upstream myapp origin error: src refspec origin does not match any. error: failed to push some refs to 'http://srv.dev.com/gitlab/john.riley/app.git' user@hostname:~/mysource$
Still, it doesn’t work either after being fetch from the remote Git repository. Try to re-execute by pushing folders and files to the master branch as shown below :
user@hostname:~/mysource$ git push --set-upstream myapp master Username for 'http://srv.dev.com': myself Password for 'http://myself@srv.dev.com': Counting objects: 9032, done. Delta compression using up to 4 threads. Compressing objects: 100% (6479/6479), done. Writing objects: 100% (9032/9032), 16.91 MiB | 1.64 MiB/s, done. Total 9032 (delta 2057), reused 9032 (delta 2057) remote: GitLab: You are not allowed to push code to protected branches on this project. To http://srv.dev.com/gitlab/john.riley/app.git ! [remote rejected] master -> master (pre-receive hook declined) error: failed to push some refs to 'http://srv.dev.com/gitlab/john.riley/app.git' user@hostname:~/mysource$
Try to checkout from master branch and it is switched to a new branch named ‘origin’.
user@hostname:~/mysource$ git checkout -b origin Switched to a new branch 'origin' user@hostname:~/mysource$
After switching to another branch named ‘origin’, try to checkout using the URL Git remote repository defined with the name of ‘myapp’.
user@hostname:~/mysource$ git checkout myapp error: pathspec 'myapp' did not match any file(s) known to git. user@hostname:~/mysource$
It is also failed because the process itself doesn’t recognized the label defined. Try to checkout from master branch :
user@hostname:~/mysource$ git checkout -b master fatal: A branch named 'master' already exists. user@hostname:~/mysource$
The process has failed because in the local repository created and initialized, there is already a branch named ‘master’. Try to push to the branch master with the URL remote Git repository specified.
user@hostname:~/mysource$ git push --set-upstream myapp master Username for 'http://srv.dev.com': myself Password for 'http://myself@srv.dev.com': Counting objects: 9032, done. Delta compression using up to 4 threads. Compressing objects: 100% (6479/6479), done. Writing objects: 100% (9032/9032), 16.91 MiB | 2.62 MiB/s, done. Total 9032 (delta 2057), reused 9032 (delta 2057) remote: GitLab: You are not allowed to push code to protected branches on this project. To http://srv.dev.com/gitlab/john.riley/app.git ! [remote rejected] master -> master (pre-receive hook declined) error: failed to push some refs to 'http://srv.dev.com/gitlab/john.riley/app.git' user@hostname:~/mysource$
Still, the error pertains with the error ‘remote: GitLab: You are not allowed to push code to protected branches on this project.’. Try to redefine all the branch URL for remote Git repository as follows either the ‘origin’ or the ‘master :
user@hostname:~/mysource$ git remote add origin http://srv.dev.com/gitlab/john.riley/app.git
user@hostname:~/mysource$ git remote add master http://srv.dev.com/gitlab/john.riley/app.git
After defining the URL remote Git repository server, try to fetch all branches :
user@hostname:~/mysource$ git fetch origin master Username for 'http://srv.dev.com': myself Password for 'http://myself@srv.dev.com': fatal: Couldn't find remote ref master user@hostname:~/mysource$ git pull origin master Username for 'http://srv.dev.com': myself Password for 'http://myself@srv.dev.com': fatal: Couldn't find remote ref master
The fetch process failed, try to check the status as follows :
user@hostname:~/mysource$ git status On branch origin nothing to commit, working directory clean user@hostname:~/mysource$ git checkout -b master fatal: A branch named 'master' already exists. user@hostname:~/mysource$ git push origin master Username for 'http://srv.dev.com': myself Password for 'http://myself@srv.dev.com': Counting objects: 9032, done. Delta compression using up to 4 threads. Compressing objects: 100% (6479/6479), done. Writing objects: 100% (9032/9032), 16.91 MiB | 2.56 MiB/s, done. Total 9032 (delta 2057), reused 9032 (delta 2057) remote: GitLab: You are not allowed to push code to protected branches on this project. To http://srv.dev.com/gitlab/john.riley/app.git ! [remote rejected] master -> master (pre-receive hook declined) error: failed to push some refs to 'http://srv.dev.com/gitlab/john.riley/app.git' user@hostname:~/mysource$
The attempt above has also failed. Try to checkout from another branch as shown below named ‘init’ :
user@hostname:~/mysource$ git checkout -b init Switched to a new branch 'init' user@hostname:~/mysource$ git push origin init:init Username for 'http://srv.dev.com': myself Password for 'http://myself@srv.dev.com': Counting objects: 9032, done. Delta compression using up to 4 threads. Compressing objects: 100% (6479/6479), done. Writing objects: 100% (9032/9032), 16.91 MiB | 2.50 MiB/s, done. Total 9032 (delta 2057), reused 9032 (delta 2057) remote: GitLab: You are not allowed to push code to protected branches on this project. To http://srv.dev.com/gitlab/john.riley/app.git ! [remote rejected] init -> init (pre-receive hook declined) error: failed to push some refs to 'http://srv.dev.com/gitlab/john.riley/app.git' user@hostname:~/mysource$
Still, all those efforts with various of command performed is actually useless. It is because the user given to connect and to operate on the repository specified doesn’t have any permission on access. So, try to grant access first for the user used to connect to the repository specified.