I know this is a very common question but I still haven't got the answer I'm looking for. Let's say I have a local branch which I have made changes in. But while I'm making those changes and want to merge this branch into main (remote). Another person merged their branch. So now I have to pull those changes into my branch, but don't want to resolve all the conflicts manually.
Is there a way to safely pull the changes made in remote to my local without losing my changes?
(note: stashing my changes don't really work cause when I do want to 1. stash 2. pull 3. stash pop i still have to manually resolve conflicts in all files while pulling)
CodePudding user response:
Is there a way to safely pull the changes made in remote to my local without losing my changes?
git pull
is a git fetch
and a git merge
.
You can use git fetch
to update your remote branches without merging them with your local branch. This lets you examine origin/main locally and do whatever comparisons and fixes to your branch you like. Then merge.
You can also choose to rebase your branch on top of main, rewriting each commit. This won't avoid conflicts, it might produce more, but it is often easier to resolve conflicts one commit at a time.
but don't want to resolve all the conflicts manually.
There's no way around that. Merge conflicts are when two branches changed the same lines; Git cannot determine which one to use and needs a human to figure it out.
In some cases using a different merge strategy, such as ignoring whitespace, can let Git avoid the conflict.
You can also tell Git to simply throw out the changes from one branch and only use the other's (ours and theirs merge strategies).
But ultimately if both branches edited the same lines you have to decide how to resolve the conflict yourself.
CodePudding user response:
Copy your changes and put them in a text file using notepad. git restore . in your terminal. this restores back your project to what it previously was before you made your changes. I like to call this turning back time. After that pull in the branch you want to pull in , then add your changes.