|
| 1 | ++++ |
| 2 | +title = "Build Your Own Git Hosting Service" |
| 3 | +slug = "build-your-own-git-hosting-service" |
| 4 | +date = 2025-03-11T11:39:10+05:30 |
| 5 | +image = "/images/2025/build-your-own-git-hosting-service/header.png" |
| 6 | +draft = false |
| 7 | +authors = ["Sahil Nayak"] |
| 8 | +description = "We can create our own remote repository service by setting up and hosting to our own server. It works just like popular services such as GitHub or Bitbucket." |
| 9 | +tags = ["Git", "GitHub"] |
| 10 | +categories = ["Git", "GitHub"] |
| 11 | +type = "" |
| 12 | ++++ |
| 13 | + |
| 14 | +Yes, you read that right! We can create our remote repository service by setting up and hosting it on our own server. It works just like popular services such as GitHub or Bitbucket but with the **added benefit of complete control over access and hosting**. |
| 15 | + |
| 16 | +The remote repository is often referred to as a **bare repository**. |
| 17 | + |
| 18 | +What exactly is a bare repository? How does it differ from a local one? Can we use the same Git commands to interact with it? 🤔 |
| 19 | + |
| 20 | +Let’s first explore the **key differences between local and bare repositories**. |
| 21 | + |
| 22 | +#### Local Vs Bare Repositories |
| 23 | + |
| 24 | +| **Aspect** | **Local Repository** | **Bare Repository** | |
| 25 | +| --------------------- | ------------------------------------------------------------------------------------ | ----------------------------------------------------------------- | |
| 26 | +| **Setup** | `git init` | `git init --bare` | |
| 27 | +| **Working Directory** | Has a working directory (your project files). | No working directory, only git metadata. | |
| 28 | +| **Actions** | You can edit files, stage changes, commit, checkout, and perform many other actions. | Limited to pushing, pulling, and other remote operations. | |
| 29 | +| **Purpose** | Used for local development; allows you to work on files and commit changes. | Serves as a centralized repository for sharing and collaboration. | |
| 30 | + |
| 31 | +We’ve cleared up the basic concepts, Now you might feel **"Talk is cheap. Show me the code."** |
| 32 | + |
| 33 | +So, let's dive into the practicals. |
| 34 | + |
| 35 | +#### Experiment 1️⃣: Local bare repository & workspace connection |
| 36 | + |
| 37 | +In your project directory, |
| 38 | + |
| 39 | +**1. Create two folders with the following names** |
| 40 | + |
| 41 | +| 📂 `local` | Your project working directory (Just like when you clone from a remote). | |
| 42 | +| --------------- | ------------------------------------------------------------------------ | |
| 43 | +| 📂 `remote.git` | A bare repository (The one where all changes are pushed). | |
| 44 | + |
| 45 | +> **Recommended**: It's a good practice to name your bare repository with a `.git` suffix, (like `remote.git`). |
| 46 | +
|
| 47 | +> When you see any GitHub repository clonable link (whether in HTTP or SSH URL) that always ends with `.git`, it indicates that **on the server, the bare repository is created in a folder named exactly as `repository_name.git`**. |
| 48 | +
|
| 49 | +**2. Set up the bare repository** |
| 50 | + |
| 51 | +Now, go into the remote folder and create the bare repository. |
| 52 | + |
| 53 | +```bash |
| 54 | +# Navigate to remote.git directory |
| 55 | +cd remote.git |
| 56 | + |
| 57 | +# Create a bare repository here |
| 58 | +git init --bare |
| 59 | +``` |
| 60 | + |
| 61 | +**3. Clone the bare repository** |
| 62 | + |
| 63 | +Go back to your project directory and navigate to the `local` folder. Clone the bare repository here. |
| 64 | + |
| 65 | +```bash |
| 66 | +# Navigate to your working directory where you want to clone |
| 67 | +cd local |
| 68 | + |
| 69 | +# clone the bare repository |
| 70 | +git clone <path-to-bare-repo> |
| 71 | +# Replace <path-to-bare-repo> with the actual file path or URL of your bare repository. |
| 72 | +# For an example: C:\User\DELL\Experiment\remote.git\ |
| 73 | +``` |
| 74 | + |
| 75 | +You should now see a folder named `remote` inside your `local` folder, as our bare repository was named `remote.git`. |
| 76 | + |
| 77 | +> **Note**: This is not the bare repository's directory. It’s now your local repository, created by cloning the bare repository. |
| 78 | +
|
| 79 | +**4. Make some changes in the local (cloned) Repository** |
| 80 | + |
| 81 | +Now, inside your local repository, navigate to the `remote` folder and do the following: |
| 82 | + |
| 83 | +```bash |
| 84 | +# Navigate to remote |
| 85 | +cd remote |
| 86 | + |
| 87 | +# Create README.md file |
| 88 | +echo "README" > README.md |
| 89 | + |
| 90 | +# Add to the staging area |
| 91 | +git add . |
| 92 | + |
| 93 | +# Commit the changes |
| 94 | +git commit -m "Add: README.md" |
| 95 | +``` |
| 96 | + |
| 97 | +**5. Check the remote origin** |
| 98 | + |
| 99 | +To see where your remote repository is set up, run: |
| 100 | + |
| 101 | +```bash |
| 102 | +git remote -vv |
| 103 | +``` |
| 104 | + |
| 105 | +You should see the remote URL listed under "`origin`" : |
| 106 | + |
| 107 | +For an example: |
| 108 | + |
| 109 | +```bash |
| 110 | +origin path\to\repo\Experiment\remote.git\ (fetch) |
| 111 | +origin path\to\repo\Experiment\remote.git\ (push) |
| 112 | +``` |
| 113 | + |
| 114 | +If **the remote isn't set up yet, you can add** it by running this command: |
| 115 | + |
| 116 | +```bash |
| 117 | +git remote add origin "<path-to-bare-repo>" |
| 118 | +# Replace <path-to-bare-repo> with the actual file path or URL of your bare repository. |
| 119 | +# For an example: C:\User\DELL\Experiment\remote.git\ |
| 120 | +``` |
| 121 | + |
| 122 | +**6. Push changes to the bare repository** |
| 123 | + |
| 124 | +Now that the remote repository URL is set up, push your changes to the bare repository: |
| 125 | + |
| 126 | +```bash |
| 127 | +git push |
| 128 | +``` |
| 129 | + |
| 130 | +**7. Verify the bare repository by cloning it elsewhere** |
| 131 | + |
| 132 | +Finally, to make sure everything works, let’s try cloning the bare repository in a different folder (e.g., bare_local). You should able to see changes. |
| 133 | + |
| 134 | +**This is how you can create a local Git repository, connect it to a bare repository, and push changes.** |
| 135 | + |
| 136 | +--- |
| 137 | + |
| 138 | +#### Experiment 2️⃣: Hosting a bare git repository on a remote server |
| 139 | + |
| 140 | +**1. Set up a remote server** |
| 141 | + |
| 142 | +Ensure you have a server with Ubuntu (or any OS) and SSH access, using any cloud provider or self-hosted server. |
| 143 | + |
| 144 | +**2. Connect to your server via SSH** |
| 145 | + |
| 146 | +To access your server, open a terminal (PowerShell or CMD) and access the server using ssh commands: |
| 147 | + |
| 148 | +```bash |
| 149 | +ssh -i "<path_to_private_key>" ubuntu@<server_ip_or_domain> |
| 150 | +``` |
| 151 | + |
| 152 | +Once executed, you’ll be logged into your server and will see the terminal. |
| 153 | + |
| 154 | +**3. Set up a bare git repository** |
| 155 | + |
| 156 | +```bash |
| 157 | +# Create a new directory for the bare repo and name it with .git suffix |
| 158 | +mkdir remotesync.git |
| 159 | + |
| 160 | +# Navigate to the Bare Repository Directory: |
| 161 | +cd remotesync.git |
| 162 | + |
| 163 | +# Create a bare repository |
| 164 | +git init --bare |
| 165 | +``` |
| 166 | + |
| 167 | +Now, your bare repository is ready for use. To allow cloning and pushing changes, you'll need to set up SSH key access. |
| 168 | + |
| 169 | +**4. Set up SSH key for git access** |
| 170 | + |
| 171 | +```bash |
| 172 | +# Navigate to the .ssh directory: |
| 173 | +cd .ssh |
| 174 | + |
| 175 | +# List the files: |
| 176 | +ls |
| 177 | +``` |
| 178 | + |
| 179 | +You should see a file named `authorized_keys`. If you’ve just set up your server, it will contain the default public key used for server access from anywhere. |
| 180 | + |
| 181 | +To enable Git access, add your public SSH key (from GitHub, GitLab, or another Git service) to the `authorized_keys` file: |
| 182 | + |
| 183 | +```bash |
| 184 | +# Edit the authorized_keys file: |
| 185 | +nano authorized_keys |
| 186 | +``` |
| 187 | + |
| 188 | +This will open the file in an editor. **Paste your GitHub SSH Public Key** into the file and Save it. |
| 189 | + |
| 190 | +> **Note**: If you want to allow multiple users to access this repository, add each user’s public key to this file. |
| 191 | +
|
| 192 | +**5. Clone the remote (bare) repository** |
| 193 | + |
| 194 | +```bash |
| 195 | +git clone ubuntu@<server_ip_or_domain>:<path_to_bare_repo> |
| 196 | + |
| 197 | +# For an Example: |
| 198 | +git clone [email protected]:/home/ubuntu/remotesync.git |
| 199 | +``` |
| 200 | + |
| 201 | +Now you're all set to collaborate with your team! 🚀 Happy coding! 👨💻👩💻 |
0 commit comments