Git Level4: Git Setup from Scratch

I successfully created the hook and direct push to master was blocked by the hook.But the at when i try to submit my work i got this error message:

- '/opt/games.git' git repository on storage server is not a bare repository

I’m certain that I created bare repository at /opt/games.git
here is the commands i used :

ssh natasha@ststor01

sudo yum install -y git

git config --global user.email "[email protected]"
git config --global user.name "testuser"
cd /opt/games.git
git init --bare
cp /tmp/update /opt/games.git/hooks/
chmod +x /opt/games.git/hooks/update
cd /usr/src/kodekloudrepos
git clone /opt/games.git games
cd games
git checkout -b xfusioncorp_games
cp /tmp/readme.md .
git add readme.md
git commit -m "Added readme.md"

git push origin xfusioncorp_games

Tried push directly to master to check if it fails due the hook

git checkout -b master 
git push origin master

Hi @Tharun_Umesh

When you start, ensure that the /opt/games.git directory exists.
You need to create the dir and then start the process of initializing the bare repo.

@Santosh_KodeKloud I did it again and created the directory before initializing the bare repo.

Before submitting my work I checked if the repository is bare,using this command:

sudo git --git-dir=/opt/ecommerce.git rev-parse --is-bare-repository

And the output is true(You can see it in the screenshot as well),But the result is still same :confused:

Probably what you have wrong here is how you created the origin, bare repo. The command should be:

git init --bare /opt/ecommerce.git

I ran this as root, and also cloned the repo as root; I changed the user to be “natasha” after I committed the readme.md file to the branch. The grader accepted this.

@rob_kodekloud I also tried that. Here is the entire terminal output (without the installation logs):

thor@jumphost ~$ ssh [email protected]

The authenticity of host '172.16.238.15 (172.16.238.15)' can't be established.
ED25519 key fingerprint is SHA256:40w5jP+k0VaHm4eZM1phiozCYi0TKNW7aYlHAOa6IXI.
This key is not known by any other names
Are you sure you want to continue connecting (yes/no/[fingerprint])? yes
Warning: Permanently added '172.16.238.15' (ED25519) to the list of known hosts.

[email protected]'s password: 
[natasha@ststor01 ~]$ sudo yum install git -y


[natasha@ststor01 ~]$ git config --global user.name test
[natasha@ststor01 ~]$ git config --golbal user.email [email protected]

[natasha@ststor01 ~]$ git config --global --list
user.name=test
[email protected]

[natasha@ststor01 ~]$ sudo mkdir -p /opt/beta.git
[sudo] password for natasha: 
[natasha@ststor01 ~]$ git init --bare /opt/beta.git
/opt/beta.git/info/: Permission denied

[natasha@ststor01 ~]$ sudo git init --bare /opt/beta.git
hint: Using 'master' as the name for the initial branch. This default branch name
hint: is subject to change. To configure the initial branch name to use in all
hint: of your new repositories, which will suppress this warning, call:
hint:
hint:   git config --global init.defaultBranch <name>
hint:
hint: Names commonly chosen instead of 'master' are 'main', 'trunk' and
hint: 'development'. The just-created branch can be renamed via this command:
hint:
hint:   git branch -m <name>
Initialized empty Git repository in /opt/beta.git/

[natasha@ststor01 ~]$ sudo chown -R natasha:natasha /opt/beta.git
[natasha@ststor01 ~]$ sudo cp /tmp/update /opt/beta.git/hooks/update
[natasha@ststor01 ~]$ sudo chmod +x /opt/beta.git/hooks/update
[natasha@ststor01 ~]$ sudo mkdir -p /usr/src/kodekloudrepos
[natasha@ststor01 ~]$ cd /usr/src/kodekloudrepos
[natasha@ststor01 kodekloudrepos]$ git clone /opt/beta.git beta
fatal: could not create work tree dir 'beta': Permission denied
[natasha@ststor01 kodekloudrepos]$ sudo git clone /opt/beta.git beta
Cloning into 'beta'...
warning: You appear to have cloned an empty repository.
done.

[natasha@ststor01 kodekloudrepos]$ cd beta
[natasha@ststor01 beta]$ git checkout -b xfusioncorp_beta
fatal: detected dubious ownership in repository at '/usr/src/kodekloudrepos/beta'
To add an exception for this directory, call:

        git config --global --add safe.directory /usr/src/kodekloudrepos/beta
[natasha@ststor01 beta]$ git config --global --add safe.directory /usr/src/kodekloudrepos/beta
[natasha@ststor01 beta]$ git checkout -b xfusioncorp_beta
error: cannot lock ref 'HEAD': Unable to create '/usr/src/kodekloudrepos/beta/.git/HEAD.lock': Permission denied
Switched to a new branch 'xfusioncorp_beta'

[natasha@ststor01 beta]$ sudo git checkout -b xfusioncorp_beta
Switched to a new branch 'xfusioncorp_beta'
[natasha@ststor01 beta]$ cp /tmp/readme.md .
cp: cannot create regular file './readme.md': Permission denied
[natasha@ststor01 beta]$ sudo cp /tmp/readme.md .
[natasha@ststor01 beta]$ sudo git add readme.md
[natasha@ststor01 beta]$ sudo git commit -m "Add readme.md file"
[xfusioncorp_beta (root-commit) fde27ae] Add readme.md file
 Committer: root <[email protected]>
Your name and email address were configured automatically based
on your username and hostname. Please check that they are accurate.
You can suppress this message by setting them explicitly. Run the
following command and follow the instructions in your editor to edit
your configuration file:

    git config --global --edit

After doing this, you may fix the identity used for this commit with:

    git commit --amend --reset-author

 1 file changed, 1 insertion(+)
 create mode 100644 readme.md

[natasha@ststor01 beta]$ sudo git push origin xfusioncorp_beta
Enumerating objects: 3, done.
Counting objects: 100% (3/3), done.
Writing objects: 100% (3/3), 257 bytes | 257.00 KiB/s, done.
Total 3 (delta 0), reused 0 (delta 0), pack-reused 0 (from 0)
To /opt/beta.git
 * [new branch]      xfusioncorp_beta -> xfusioncorp_beta

[natasha@ststor01 beta]$ git checkout -b master
fatal: cannot lock ref 'refs/heads/master': Unable to create '/usr/src/kodekloudrepos/beta/.git/refs/heads/master.lock': Permission denied
[natasha@ststor01 beta]$ sudo git checkout -b master
Switched to a new branch 'master'
Your branch is based on 'origin/master', but the upstream is gone.
  (use "git branch --unset-upstream" to fixup)

[natasha@ststor01 beta]$ git push origin master
Total 0 (delta 0), reused 0 (delta 0), pack-reused 0 (from 0)
remote: Manual pushes to the master branch is restricted!!
remote: error: hook declined to update refs/heads/master
To /opt/beta.git
 ! [remote rejected] master -> master (hook declined)
error: failed to push some refs to '/opt/beta.git'
[natasha@ststor01 beta]$ sudo git push origin master
Total 0 (delta 0), reused 0 (delta 0), pack-reused 0 (from 0)
remote: Manual pushes to the master branch is restricted!!
remote: error: hook declined to update refs/heads/master
To /opt/beta.git
 ! [remote rejected] master -> master (hook declined)
error: failed to push some refs to '/opt/beta.git'

Let’s look at the full task:

Some new developers have joined xFusionCorp Industries and have been assigned Nautilus project. They are going to start development on a new application, and some pre-requisites have been shared with the DevOps team to proceed with. Please note that all tasks need to be performed on storage server in Stratos DC.

a. Install git, set up any values for user.email and user.name globally and create a bare repository /opt/official.git.

b. There is an update hook (to block direct pushes to the master branch) under /tmp on storage server itself; use the same to block direct pushes to the master branch in /opt/official.git repo.

c. Clone /opt/official.git repo in /usr/src/kodekloudrepos/official directory.

d. Create a new branch named xfusioncorp_official in repo that you cloned under /usr/src/kodekloudrepos directory.

e. There is a readme.md file in /tmp directory on storage server itself; copy that to the repo, add/commit in the new branch you just created, and finally push your branch to the origin.

f. Also create master branch from your branch and remember you should not be able to push to the master directly as per the hook you have set up.

So, general guidelines for this:

  1. There are no instructions to do otherwise, so I do everything as root.
  2. I follow the instructions as exactly as I can.

My session looks like this:

## (a)
[root@ststor01 ~]# git init --bare /opt/official.git
hint: Using 'master' as the name for the initial branch. This default branch name
hint: is subject to change. To configure the initial branch name to use in all
hint: of your new repositories, which will suppress this warning, call:
hint:
hint:   git config --global init.defaultBranch <name>
hint:
hint: Names commonly chosen instead of 'master' are 'main', 'trunk' and
hint: 'development'. The just-created branch can be renamed via this command:
hint:
hint:   git branch -m <name>
Initialized empty Git repository in /opt/official.git/
[root@ststor01 ~]# git config --global user.name natasha
[root@ststor01 ~]# git config --global user.email [email protected]

## (c)
[root@ststor01 ~]# ls /tmp
readme.md  update
[root@ststor01 ~]# cp /tmp/update /opt/official.git/hooks/
[root@ststor01 ~]# pushd /opt/official.git/hooks/
/opt/official.git/hooks ~
[root@ststor01 hooks]# vi update

## (d)
[root@ststor01 hooks]# git clone /opt/official.git /usr/src/kodekloudrepos/official
Cloning into '/usr/src/kodekloudrepos/official'...
warning: You appear to have cloned an empty repository.
done.
[root@ststor01 hooks]# cd /usr/src/kodekloudrepos/official
[root@ststor01 official]# ls
[root@ststor01 official]# ls -a
.  ..  .git
[root@ststor01 official]# git checkout -b xfusioncorp_official
Switched to a new branch 'xfusioncorp_official'

## (e)
[root@ststor01 official]# cp /tmp/readme.md .
[root@ststor01 official]# git add .
[root@ststor01 official]# git commit -m 'add readme'
[xfusioncorp_official (root-commit) a55e83e] add readme
 1 file changed, 1 insertion(+)
 create mode 100644 readme.md
[root@ststor01 official]# git push origin xfusioncorp_official
Enumerating objects: 3, done.
Counting objects: 100% (3/3), done.
Writing objects: 100% (3/3), 239 bytes | 239.00 KiB/s, done.
Total 3 (delta 0), reused 0 (delta 0), pack-reused 0 (from 0)
To /opt/official.git
 * [new branch]      xfusioncorp_official -> xfusioncorp_official
[root@ststor01 official]# git checkout master
error: pathspec 'master' did not match any file(s) known to git
[root@ststor01 official]# git checkout -b master
Switched to a new branch 'master'
Your branch is based on 'origin/master', but the upstream is gone.
  (use "git branch --unset-upstream" to fixup)
[root@ststor01 official]# git merge xfusioncorp_official
Already up to date.
[root@ststor01 official]# ls -l
total 4
-rw-r--r-- 1 root root 33 Sep 17 21:18 readme.md
[root@ststor01 official]# git log
commit a55e83e11e469dfb22c344a74682cd32de5e9f9a (HEAD -> master, origin/xfusioncorp_official, xfusioncorp_official)
Author: natasha <[email protected]>
Date:   Wed Sep 17 21:19:15 2025 +0000

    add readme

## (f)
[root@ststor01 official]# git push origin master
Total 0 (delta 0), reused 0 (delta 0), pack-reused 0 (from 0)
remote: Manual pushes to the master branch is restricted!!
remote: error: hook declined to update refs/heads/master
To /opt/official.git
 ! [remote rejected] master -> master (hook declined)
error: failed to push some refs to '/opt/official.git'

The grader accepts this.

@rob_kodekloud Because the task instructions didn’t provide a root password, I can’t access the Nautilus Storage Server as root. The only credentials provided in the Infrastructure Details for the Storage Server were for the user ‘natasha’. So, I’ve completed the task using the ‘natasha’ account as it was the only option. Is there somewhere else I can find the root password for the storage server, or is there another alternative way to proceed?

You can use sudo. natasha is in the sudoer’s file, so if you’re logged into the system as “natasha” (and you are in this case), sudo -i will make you root.

It worked! :relieved: The grader accepted my submission once I performed the task as the root user. It should be specifically mentioned in the task instructions to perform this action as the root user instead of the ‘natasha’ user. Without that explicit instruction, it’s easy to become confused about what went wrong, as these types of issues can be difficult to identify.

Thank you for your help, @rob_kodekloud . Your time and assistance in solving this problem are greatly appreciated.