How do I git revert to a point at which my fork is both behind and ahead of the upstream?
I was working on a fork of a github project and wanted to incorporate the changes on it into my fork, so i could open a pull request. However I made some mistakes during the merge and ended up reverting to the previous merge. Git now says my fork is ahead of the original project, which it is not entirely. I have made another branch, which is also still at my latest commit before the merge, github says it is some commits behind my master branch. Is there any way to get back to a stage where my master branch is both behind and ahead of the original project, so I can attempt merging them again?
I have tried multiple things, like manually replacing my repository's contents with older commits, different combinations of merges between my branches, but to no avail.
Answers
It sounds like you've encountered a situation where your local master branch is ahead of the original project, but you want to revert it to a previous state where it's both behind and ahead. Here's a step-by-step guide on how you can achieve this:
-
Create a Backup Branch: Before making any further changes, it's always a good idea to create a backup branch to preserve your current state in case something goes wrong. You can do this by running the following command:
git checkout -b backup-master master
-
Revert to a Previous Commit: Identify the commit in your master branch that you want to revert to. You can use
git log
to view the commit history and find the commit hash of the desired state.git log
Once you've identified the commit, use
git reset --hard
to revert your master branch to that commit. Replace<commit-hash>
with the hash of the desired commit:git reset --hard <commit-hash>
-
Force Push to Remote: Since you've rewritten the history of your master branch, you'll need to force push the changes to your remote repository. Use the following command to force push the changes:
git push origin master --force
This command will overwrite the history of your master branch on the remote repository with your local changes. Be cautious when using the
--force
option, as it can potentially overwrite changes on the remote repository. -
Merge Changes from Original Project: Once you've reverted your master branch to the desired state, you can merge changes from the original project again. Make sure you're on the master branch and then pull changes from the original project:
git checkout master git pull <original-project-remote> master
Resolve any merge conflicts that may occur during the merge process.
-
Push Merged Changes: After resolving any merge conflicts and finalizing the merge, push the changes to your remote repository:
git push origin master
Now your master branch should be both behind and ahead of the original project, allowing you to attempt merging them again. Make sure to carefully review the changes and resolve any conflicts during the merge process. If you encounter any issues, you can always refer back to the backup branch created in step 1.