DEV Community

Anusha Kuppili
Anusha Kuppili

Posted on

How to Copy a File and Push to Git Repo in a Real DevOps Task

In this guide, we’ll walk through a practical DevOps task:

✅ Copying a file from a jump host to a storage server,

✅ Adding that file to a Git repository,

✅ Committing and pushing it to the master branch,

✅ And fixing a common Git permission error (index.lock: Permission denied) if it shows up.


🧩 Scenario

  • A Git repository has been cloned to /usr/src/kodekloudrepos/apps on a storage server
  • A sample index.html file is available on a jump host at /tmp/index.html
  • You’ve been asked to:
    1. Copy this file from the jump host to the storage server
    2. Add and commit it to Git
    3. Push the change to the master branch

🛰️ Step 1: Copy the File from Jump Host to Storage Server

From the jump host, use scp:

scp /tmp/index.html <username>@<storage_server_ip>:/usr/src/kodekloudrepos/apps/
Enter fullscreen mode Exit fullscreen mode

Replace and with actual values.

🔐 Step 2: SSH into the Storage Server
Once the file is copied, log into the storage server:

ssh <username>@<storage_server_ip>
Enter fullscreen mode Exit fullscreen mode

📂 Step 3: Navigate to the Git Repository
Change into the project directory where the Git repo is cloned:

cd /usr/src/kodekloudrepos/apps
Enter fullscreen mode Exit fullscreen mode

Double-check the file is there:

ls -l index.html
Enter fullscreen mode Exit fullscreen mode

📝 Step 4: Add and Commit the File to Git
Now stage and commit the file:

git add index.html
git commit -m "Add sample index.html file"
Enter fullscreen mode Exit fullscreen mode

🚀 Step 5: Push the Commit to the Remote Repository
Push the change to the master branch:

git push origin master
🚨 Common Error: index.lock Permission Denied
Enter fullscreen mode Exit fullscreen mode

You might run into this error:

fatal: Unable to create '/usr/src/kodekloudrepos/apps/.git/index.lock': Permission denied
Enter fullscreen mode Exit fullscreen mode

This happens when your user doesn’t have permission to modify the Git repo. Here’s how to fix it.

🛠️ Step 6: Fixing Git Permission Error
✅ Option 1: Use sudo (Quick Fix)

sudo git add index.html
sudo git commit -m "Add sample index.html file"
sudo git push origin master
Enter fullscreen mode Exit fullscreen mode

🧼 Option 2: Change Ownership (Recommended)
If you're supposed to manage this repo long-term:

sudo chown -R $(whoami):$(whoami) /usr/src/kodekloudrepos/apps
Enter fullscreen mode Exit fullscreen mode

Then you can use Git without sudo.

👤 Option 3: Switch to Root (One-time Fix)
If the repo should stay owned by root:

sudo su - root
cd /usr/src/kodekloudrepos/apps
git add index.html
git commit -m "Add sample index.html file"
git push origin master
Enter fullscreen mode Exit fullscreen mode

✅ Final Verification
Run this to make sure the file is tracked and pushed:

git status
git log --oneline
Enter fullscreen mode Exit fullscreen mode

💬 Got stuck somewhere?
Drop a comment with the exact error or step, and let’s debug it together. This kind of real-world Git workflow is foundational in any DevOps role — and mastering it makes your life easier every time.

Top comments (0)