0

I have no advanced experience in Git. I have the following scenario.

I did a branch, let's say Branch-A (it points to remote branch origin/Branch-A) and I start implementing things in it. Later my colleague created another branch, let's say Branch-B from remote branch origin/Branch-A.

He finished his implementation before me and he requested a merge request from his Branch-B into remote origin/Branch-A and I approved and merged it without problems.

Now all his changes are in origin/Branch-A.

Now I have finish my implementation in my Branch-A. My changes are in local and I want to push them to the remote origin/Branch-A so before doing this, I prefer to do a pull merge from origin/Branch-A into my local Branch-A but when I try to do it an error appears saying:

git pull --progress "origin"
warning: redirecting to <remote url here>
error: Your local changes to the following files would be overwritten by merge:
  <list of files conflicting here>
Please commit your changes or stash them before you merge.

So how can I merge the remote branch origin/Branch-A (which contains now all the changes from my colleage) into my local branch Branch-A? and then, if merge is ok, to commit and push my changes to remote branch origin/Branch-A.

Kiraski
  • 27
  • 5
  • 1
    Git is already doing you what to do: "Please commit your changes or stash them before you merge". You have uncommitted changes in files that would be changed by the merge. – knittl Jan 31 '23 at 21:06
  • https://stackoverflow.com/search?q=%5Bgit%5D+error%3A+Your+local+changes+to+the+following+files+would+be+overwritten+by+merge – phd Jan 31 '23 at 21:10
  • @knittl Yep, but how can I push my changes to the remote branch and not overwrite what my colleague pushed previously? If I commit my local changes to the remote branch, changes that made my colleague will be overwritten? for example, he deleted some files that are present in my local, so if i push my changes to local, will i make those deleted files by my colleague to be added again when i push my local changes? if i do a commit of my local changes to remote branch, will git find conflicts and let me resolve? – Kiraski Jan 31 '23 at 21:27
  • You cannot commit to a remote branch, you can only commit to your local branches. If you and your colleague modified the same files and you merge, Git will try its best to merge. It may fail in which case it stops and asks you to resolve the conflicts. https://git-scm.com/book/en/v2/Git-Branching-Basic-Branching-and-Merging – knittl Jan 31 '23 at 21:29
  • @knittl sorry i wanted to say push instead of commit to the remote branch. Also i think it is possible to do a checkout of origin/Branch-A with merge option. So I will get all the changes from remote to my local and Git automatically will detect conflicts and ask me to solve manually, right? i think it is a good solution. Then once i resolve the conflicts, I can commit and push. – Kiraski Jan 31 '23 at 21:54
  • You cannot push to a remote branch if it contains commits which you don't have locally (a "non fast-forward"). You have to fetch + merge locally first, resolve potential conflicts, then push. – knittl Jan 31 '23 at 21:59

0 Answers0