I recently encountered a situation where I pushed my changes to the main branch at 3 PM. Shortly after, at 3:15, a coworker made changes and pushed them to the same branch. While I was in the middle of my own updates between 3:00 and 3:20, I tried to merge my changes but ran into an error since I hadn't pulled my coworker's updates first. I attempted to abort the merge, but it didn't work as expected. What are the correct steps to resolve this? Should I just revert to my last commit, pull the changes, and then reapply my updates? Also, if I had created a remote branch during this process around 3:20 without incorporating my coworker's changes, how would that affect my edits if they were made to the same line?
5 Answers
From what you described, it sounds like you started a merge and made some changes, which is likely why the abort command isn’t working. You can undo your changes however you like and try the abort again later. Just be careful with how you stage your changes next time. There’s a helpful guide on advanced merging in Git you might want to check out.
You really shouldn’t push directly to main, though! That creates a big risk for conflicts. Ideally, work on a separate branch. But if you end up with a merge conflict anyway, you'll need to resolve that manually. Look into how to resolve merge conflicts in Git—there are plenty of tutorials that can guide you through it.
If you want to avoid these mess-ups in the future, consider using feature branches or a workflow where each developer works in isolation on their own feature. This way, every commit is safe and working before merging. It’ll significantly decrease the likelihood of conflicts and make the process smoother.
When you run into a situation with changes from another developer, Git will prompt you that there’s a merge conflict. If you attempted to solve it and failed, don’t worry! Start a new feature branch and push your previous commits there. You can then hard reset your local main branch to the last successful commit, fetch updates, and apply your colleague’s changes using pull with rebase. This way, you'll be in a clean state.
It’s actually best practice not to push directly to the main branch. Instead, you should push to your own feature branch and create a pull request (PR). That way, you reduce the chances of merge conflicts. If you find yourself in a situation where you’ve made a PR and it's behind the main branch, you’ll have to sync with main before merging it.
Yeah, I could see how that might complicate things. What if your PR is outdated because someone else pushed changes?

Can I revert to my exact state from earlier before everything went wrong?