Introduction
When forking and maintaining a repository, it’s essential to prevent accidental pushes to the original (upstream) repository. This guide provides steps to ensure that your fork remains independent while still allowing updates from the upstream repository.
Steps to Prevent Accidental Pushes
1. Verify Your Git Remotes
Run the following command to check your configured remotes:
git remote -v
Expected output:
origin https://github.com/yourusername/your-repo.git (fetch) origin https://github.com/yourusername/your-repo.git (push) upstream https://github.com/original-repo/original-repo.git (fetch) upstream https://github.com/original-repo/original-repo.git (push)
If you see upstream in the push list, proceed to remove push access.
2. Disable Pushing to Upstream
To prevent accidental pushes to the upstream repository, modify the push URL:
git remote set-url --push upstream no_push
Verify that push access is disabled by running:
git remote -v
Expected output:
origin https://github.com/yourusername/your-repo.git (fetch) origin https://github.com/yourusername/your-repo.git (push) upstream https://github.com/original-repo/original-repo.git (fetch) upstream no_push (push)
Now, Git will no longer attempt to push to upstream.
3. Fetch Updates Without Pushing
To keep your fork updated without pushing changes to upstream:
git fetch upstream
Then merge changes:
git merge upstream/main # Or upstream/2.2 based on your branch
4. Handling Pull Requests Carefully
If you accidentally click Compare & Pull Request on GitHub, you can simply close the pull request before it gets merged.
Summary
✅ Use git remote -v to verify remotes. ✅ Set upstream push URL to no_push to prevent accidental pushes. ✅ Fetch and merge updates safely. ✅ Be cautious when working with pull requests on GitHub.
By following these steps, you can confidently manage your fork without affecting the original repository. 🚀