Mirroring Git Changes From One Server to Another Server
This article will help you to achieve the Git mirroring between one server to another server. Read on!
Join the DZone community and get the full member experience.Join For Free
Hello all, nowadays most of the development teams using GIT version control, some of you may have a requirement of mirroring your team's git changes from one server to another Git server. This article will help you to achieve the Git mirroring between one server to another server.
I got one assignment wherein there will be 2 Git Servers, development will happen in one Git server and the changes should be synchronized to another Git server at regular intervals. But in my case, the complexity is both the servers are in different restricted network. So I have done the small experiment and it worked. And I am sharing the steps to you all in this article.
The Experiment Performed Using Below 2 GIT Servers
Main GIT Server: Let's take our main git server is located in our office and can be accessed only in-office network.
Mirror GIT Server: The mirror server is located at the vendor/client-side, which can be accessible in a normal internet connection but not with our office network. Since the office proxy will block the outside URL's.
Now let's see how we can mirror the GIT projects between these 2 servers.
Step 1: Connected my laptop to my office LAN connection.
Step 2: Created a local folder and navigate to that using Git bash console
Step 3: Cloned the main Git server repository using clone command
git clone --mirror
Step 4: Cd to the cloned repository root path
Step 5: Check the available branches in the cloned repository using “git branch –a” command
Step 6: Disconnect from your office network and connect to wifi/dongle internet access
Step 7: Next updated the Push URL to the Mirror Git server
git remote set-url --push origin
Step 8: Validate the fetch and push origin URL’s, in our case, it looks like below
origin http://mainserver:8080/tfs/project1/_git/Experiment (fetch)
origin https://mirrorserver:8080/tfs/project1/git/Experiment (push)
Step 9: Now Git Push the locally cloned main Git server repository to Mirror git server repository
All the branches with history, tags are synchronized from your main Git server to mirror Git Server.
Step 10: So whenever changes happened in the main server just execute the fetch command as below in your local workspace by connecting to your office LAN connection
Git fetch –p origin
This will fetch all the changes in your main Git server repo to your local workspace
Step 11: Then switch to the secondary server network and use the push command to sync the changes to the mirror server as below
Git push –mirror
All the latest changes will be now synchronized with the mirror GIT server.
Note: If your primary server and mirror server can be accessible from the same network then there is no need for switching network connection as mentioned in above step 6 and step 11. In my case, I cannot access the mirror server from my office network, so I switched to a public wifi/dongle connection and perform the mirroring options.
Thus mirroring a GIT server can be easily achieved using the above method, if you want to mirror the GIT server changes with another GIT server in the same network then you can also put the fetch and push command in the shell file and automate this synchronization process.
Opinions expressed by DZone contributors are their own.