Overwriting someone else's changes is one of the most common fears on GitHub. The solution is simple: always pull the latest changes before you start working, use branches to isolate your edits, and review pull requests before merging. GitHub also offers branch protection rules that prevent anyone from pushing directly to main without a review.
Why Changes Get Overwritten and How to Stop It
Overwriting happens when two people edit the same file at the same time and one person's changes replace the other's. In the old days of emailing files back and forth, this was a constant problem. GitHub solves it with three mechanisms:
1. **Branches** — each person works on their own copy, then merges when ready. 2. **Pull requests** — a review step before changes reach the main branch. 3. **Conflict detection** — GitHub warns you if two people edited the same lines and asks you to choose which version to keep.
If you are using an AI tool like Lovable or Replit that syncs with GitHub, the same rules apply. The AI tool works on a branch, and you review the changes before they merge into main. As long as you follow the pull-before-edit habit and use branches, you will never accidentally overwrite someone's work.
Prerequisites
- A GitHub account (free tier is fine)
- A repository with at least one collaborator or an AI tool connected
- Basic familiarity with what a commit is
Step-by-step guide
Pull the latest changes before you start editing
Pull the latest changes before you start editing
If you are using GitHub Desktop, open your repository and click the 'Fetch origin' button in the top toolbar. If new changes are available, the button changes to 'Pull origin' — click it again to download them. If you are editing directly on GitHub.com, you are always seeing the latest version, so no pull is needed. The key habit is: never start editing until you have the freshest copy.
Expected result: Your local copy matches the latest version on GitHub, and you are ready to edit safely.
Create a branch for your changes
Create a branch for your changes
On GitHub.com, go to your repository and click the branch dropdown (top-left, showing 'main'). Type a new branch name in the text field, like 'update-homepage'. Click 'Create branch: update-homepage from main'. You now have a separate copy where your edits will not affect anyone else. On GitHub Desktop, click the 'Current Branch' dropdown at the top, then click 'New Branch', type the name, and click 'Create Branch'.
Expected result: A new branch is created and you are switched to it. The branch dropdown now shows your new branch name.
Make your edits on the branch
Make your edits on the branch
Now edit files on your branch. On GitHub.com, navigate to a file and click the pencil icon to edit it. Make your changes, scroll down to the 'Commit changes' section, add a short description of what you changed, and click the green 'Commit changes' button. On GitHub Desktop, edit files in your code editor, then return to GitHub Desktop where you will see the changed files listed. Write a summary and click 'Commit to update-homepage'.
Expected result: Your edits are saved on your branch only. The main branch is untouched.
Open a pull request to merge safely
Open a pull request to merge safely
On GitHub.com, click the 'Pull requests' tab at the top of your repository. Click the green 'New pull request' button. In the 'compare' dropdown, select your branch (update-homepage). GitHub shows you a diff — green lines are additions, red lines are removals. If GitHub says 'Able to merge' with a green checkmark, there are no conflicts. Click 'Create pull request', add a title and description, then click 'Create pull request' again.
Expected result: A pull request is open, showing all your changes for review before they touch main.
Turn on branch protection to prevent direct pushes
Turn on branch protection to prevent direct pushes
Go to your repository's 'Settings' tab (gear icon in the top navigation). In the left sidebar, click 'Branches' under the 'Code and automation' section. Click 'Add branch protection rule'. In the 'Branch name pattern' field, type 'main'. Check the box that says 'Require a pull request before merging'. Optionally check 'Require approvals' and set it to 1. Scroll down and click the green 'Create' button.
Expected result: No one — including you — can push changes directly to main. All changes must go through a pull request.
Resolve merge conflicts if they appear
Resolve merge conflicts if they appear
If GitHub shows a yellow warning saying 'This branch has conflicts that must be resolved', it means someone else changed the same lines you did. Click the 'Resolve conflicts' button on the pull request page. GitHub opens a special editor showing both versions side by side, separated by markers like <<<<<<< and >>>>>>>. Delete the version you do not want (and the marker lines), keep the version you do want, then click 'Mark as resolved' in the top-right. Finally, click the green 'Commit merge' button.
Expected result: The conflict is resolved and the pull request can be merged cleanly.
Complete working example
1# Branch Protection Checklist23## Settings to enable on `main`45- [x] Require a pull request before merging6- [x] Require at least 1 approval7- [x] Dismiss stale pull request approvals when new commits are pushed8- [ ] Require status checks to pass before merging9- [ ] Require branches to be up to date before merging10- [x] Do not allow bypassing the above settings1112## Daily habits to prevent overwrites13141. Pull latest changes before starting any work152. Create a new branch for every change163. Never edit directly on main174. Review the diff before approving a pull request185. Delete branches after mergingCommon mistakes when avoiding Overwriting Changes in GitHub
Why it's a problem: Editing files directly on the main branch
How to avoid: Always create a new branch first, even for small changes. This keeps main safe.
Why it's a problem: Ignoring the 'Pull origin' button in GitHub Desktop before starting work
How to avoid: Make it a habit to click 'Fetch origin' every time you open GitHub Desktop. If it changes to 'Pull origin', click it.
Why it's a problem: Merging a pull request without reviewing the diff
How to avoid: Click the 'Files changed' tab on every pull request and read through the green and red lines before merging.
Why it's a problem: Deleting conflict markers instead of resolving the conflict
How to avoid: Read both versions carefully, keep the correct one, and remove the <<<<<<< and >>>>>>> markers completely.
Why it's a problem: Assuming an AI tool like Lovable will never create conflicts
How to avoid: AI tools work on branches just like people do. If you edit the same file manually while Lovable is working, conflicts can happen. Coordinate your edits.
Best practices
- Pull before you push — always fetch the latest changes before starting new work
- One branch per feature or fix — never reuse branches for unrelated changes
- Enable branch protection on main for every repository, even personal ones
- Write clear commit messages so reviewers understand what changed and why
- Review pull request diffs line by line before clicking 'Merge'
- Delete merged branches to keep your branch list clean
- If using Lovable or Cursor with GitHub sync, avoid editing the same files manually at the same time
- Communicate with collaborators about who is working on which files
Still stuck?
Copy one of these prompts to get a personalized, step-by-step explanation.
I share a GitHub repository with a teammate and I'm worried about overwriting their changes. Explain the safest workflow using branches and pull requests, without any terminal commands.
Frequently asked questions
What happens if I accidentally overwrite someone's changes?
GitHub keeps a full history of every commit. Go to the 'Commits' list on your repository page, find the commit before the overwrite, click it, and you can restore the old version. Nothing is permanently lost on GitHub.
Do I need branch protection if I am the only person on the project?
Yes. Even solo developers make mistakes. Branch protection forces you to use pull requests, which gives you a chance to review your own changes before they go live.
Can Lovable or Cursor overwrite my changes on GitHub?
AI tools like Lovable and Cursor sync with GitHub via branches. They should not overwrite your main branch directly. However, if you merge their pull request without reviewing it, their changes will replace yours on those specific files. Always review the diff.
What is a merge conflict?
A merge conflict happens when two people change the same lines in the same file. GitHub cannot decide which version to keep, so it asks you to choose. You resolve it by picking the correct version in GitHub's conflict editor.
How do I know if someone else is editing the same file?
Check the 'Pull requests' tab for open pull requests that touch the same files. You can also click a file and then click 'Blame' to see who last edited each line.
Can RapidDev help set up a safe GitHub workflow for my team?
Yes. RapidDev can configure branch protection, set up pull request templates, and establish a branching strategy tailored to your team size and AI tools, so no one ever overwrites each other's work.
Talk to an Expert
Our team has built 600+ apps. Get personalized help with your project.
Book a free consultation