0

I've been searching a lot for similar questions, but I'm not able to find a solution to my issue.

I want to fully overwrite a local branch with the content of a remote (other) branch, keeping my HEAD reference to the local branch.

This question How to replace local branch with remote branch entirely in Git? answers the first part, my local files would be overridden with the other branch content, but in git diff panel I wouldn't see what I'm changing from my current branch to the other.

Basically I would like to see on the left part of the diff panel the current branch file version,and on the right the other branch version. I could achieve the same result by deleting everything (manually) from the original folder, except from the .git folder, and pasting everything (except from the other .git folder) from the other branch local folder.

I also tried some kind of merge command like (I'm working on main branch and I want to take everything from the dev branch)

git merge --no-commit --no-ff origin/dev

but of course this is not fully replacing files since I have to merge conflicts and there are some automatic merge operations on some files with additions on both sides.

I came up to this command

git reset --hard origin/dev

git reset --soft origin/main

which seems to be promising, since I can also see on the diff panel the main branch files (that are not on dev branch) like if I'm deleting them (which is what I want). But if I compare the src folders (local dev branch with "overriden" main branch) I can see there is some difference between total file sizes (but I already checked that encoding is different between the two). Can you tell me if this actually does what I want and if there is some downside on doing this?

Ansharja
  • 1,237
  • 1
  • 14
  • 37
  • 1
    Your hard and soft reset commands should work perfectly. Do you intend to actually commit it, or just "look" at it? If just looking, what tool are you using? Note there are multiple other ways to look at it besides this. – TTT Jul 28 '23 at 05:37
  • @TTT Thanks for your comment! I want to actually commit it but only after performing a manual merge without any sort of automatic operation. I'm using vscode as diff editor. If you think there is a better way to do this, I'd like to know, as I said i don't want to change my tree history, or detach anything or do any kind of operation on the branches, just want to replace my files locally and be able to see and manually merge the differences between the two branches. – Ansharja Jul 29 '23 at 08:25
  • OK. If I understand correctly, you don't necessarily want to completely replace the state of `main` with the state of `dev`, and you also don't necessarily want to take the merge of `dev` into `main`. Maybe it's one of those, or maybe it's something in between, so you want to look at it and possibly make tweaks before committing the final result? And, you're thinking you'll have less tweaks to make if you start with the state of `dev`, rather than the state of the merge of `dev` into `main`. Is that an accurate restatement of the goal? – TTT Jul 29 '23 at 16:02
  • @TTT Yes, that's the goal. I'm not sure what you mean with "you're thinking you'll have less tweaks ... with the state of dev, rather than ...". In this case I would say yes, I have a lot of work on dev which I want to bring to main, which also has some updates. So i would take more updates from dev to main. But I would go the same way even if I had more work on main, except from the case where I have a few files modified in dev, which would bring be to manually copy the updates. In my case the main branch is the final branch, so I want to check on main the result of the update. – Ansharja Jul 29 '23 at 18:57
  • 1
    If you wish to keep things from both branches, then I think you probably do want to merge. Other than the fact that you have some merge conflicts, is there another reason you think you shouldn't use the merge? Note you're likely going to have to resolve the merge conflicts either way, and the way Git presents them to you may actually make them easier to resolve compared to eyeing it manually. Also, VS Code has a nice merge conflict resolution view that shows you 4 views: the merge-base, each of the 2 branch tips, and the final result, with optional buttons to accept/undo either side or both. – TTT Jul 30 '23 at 19:25
  • @TTT Thanks for your opinion. I want to manually edit each file, deciding what to keep from both version, also I need to tag and comment the code for every updated file, so I can't just accept one version or the other, I want to manually write on them. I also noticed that some kind of automatic is still performed. For example, I had modified a file in both branches, adding some rows on top of the file. Instead of being forced to merge it I have, as a result, a single file with the added parts on top, which is wrong for me. I admit that I'm not skilled with merge, so there might be a better way – Ansharja Aug 04 '23 at 20:48
  • 1
    Releated: [How do I prevent an automerge using Git?](https://stackoverflow.com/q/5235010/184546). – TTT Aug 04 '23 at 21:29
  • @TTT Thanks for your help. I'm not sure I will go with merge, I would need some time trying to setup my environment and to force manual merge, since I wasn't able to achieve what I want with vscode. – Ansharja Aug 06 '23 at 20:27

0 Answers0