This content originally appeared on DEV Community and was authored by Kumar Nitesh
Introduction
Working with multiple Git repositories can be challenging, especially when you need to sync changes between different remote repositories. In this blog post, we’ll walk through a real-world scenario where we needed to sync changes from an awesome-repo2
repository to a awesome-repo1
repository, and then reset the remote configuration back to the original setup.
The Challenge
Imagine you’re working on a project where:
- Your local repository is currently pointing to
awesome-repo1.git
- You need to sync the latest changes from
awesome-repo2.git
- After syncing, you want to reset the remote configuration back to the original setup
This is a common scenario in enterprise environments where different teams maintain separate repositories but need to share code changes.
Step-by-Step Solution
Step 1: Assess Current Configuration
First, let’s check what remotes are currently configured:
git remote -v
This command shows us:
-
origin
pointing tossh://****/awesome-repo1
- No upstream remote configured yet
Step 2: Add Upstream Remote
To sync from the account-manager repository, we need to add it as an upstream remote:
git remote add upstream ssh://***/awesome-repo2.git
This creates a new remote called upstream
that points to the source repository we want to sync from.
Step 3: Fetch Latest Changes
Now we fetch all branches and commits from the upstream repository:
git fetch upstream
This command downloads all the latest changes from the account-manager repository without merging them yet. You’ll see output showing all the branches that were fetched.
Step 4: Switch to Target Branch
Ensure we’re on the branch we want to sync (in this case, develop
):
git checkout develop
Step 5: Pull Changes from Upstream
This is the crucial step where we actually sync the changes:
git pull upstream develop
This command merges the latest changes from upstream/develop
into your local develop
branch. In our example, this resulted in a fast-forward merge with 782 commits being added.
Step 6: Verify the Sync
Check the status to confirm the sync was successful:
git status
You should see that your branch is now ahead of origin/develop
by the number of commits that were synced.
Resetting Remote Configuration
After syncing, you might want to reset your remote configuration back to the original setup. Here’s how:
Step 7: Remove Upstream Remote
git remote remove upstream
This removes the temporary upstream remote we added for syncing.
Step 8: Verify Final Configuration
git remote -v
Confirm that only the original origin
remote remains, pointing to your intended repository.
Step 9: Check Final Status
git status
Verify that your branch is back to its original state relative to the origin remote.
Key Takeaways
Use upstream remotes for temporary syncing: Adding an upstream remote is a clean way to temporarily sync from another repository without permanently changing your remote configuration.
Fetch before pulling: Always use
git fetch
first to see what changes are available before pulling them into your branch.Clean up after syncing: Remove temporary remotes after completing the sync to keep your repository configuration clean.
Verify at each step: Use
git status
andgit remote -v
to verify your configuration at each step of the process.
Common Use Cases
This workflow is particularly useful for:
- Code migration projects: Moving code between different repositories
- Feature sharing: Syncing specific features between related projects
- Fork management: Keeping forks in sync with upstream repositories
- Enterprise environments: Managing code across different team repositories
Best Practices
- Always backup your work before syncing large changes
- Review the changes before pulling to ensure they’re what you expect
- Consider creating a backup branch before syncing if you’re unsure
- Document your sync process for team members
- Use descriptive commit messages when pushing synced changes
Conclusion
Syncing between different Git repositories doesn’t have to be complicated. By using upstream remotes and following a systematic approach, you can safely sync changes between repositories while maintaining clean remote configurations. The key is to be methodical, verify each step, and clean up after yourself.
Remember: Git is a powerful tool, but with great power comes great responsibility. Always understand what changes you’re pulling before merging them into your working branch!
This content originally appeared on DEV Community and was authored by Kumar Nitesh