-1

My colleague worked on same files as me, even though he knew I was also working on that files, and he pushed changes.

Can I pull his changes to my local branch without losing my changes?

Is there any way of dealing with this mess? Also, I refactored some files names.

Antonio Petricca
  • 8,891
  • 5
  • 36
  • 74
newhere
  • 39
  • 1
  • 8

2 Answers2

1

Working on the same files are a usual practice.

If you pull something which another user yet changed and pushed, may generate conflicts.

In such case you have to deal with the **conflict resolution" scenario, but you don't loose any of you changes.

At Atlassian you can get a good guide.

Antonio Petricca
  • 8,891
  • 5
  • 36
  • 74
1

Git is actually extremely good at handling these conflicts.

If you worked on two different areas of the same file, Git should be able to merge the two changes with no problem.
If your work actually conflicts, you'll get a merge conflict, with yours and you colleague's changes, and have to manually resolve which one (or a mix of the two) to take. GitLab has a neat guide on this subject for some details.

Note that "conflicts" in this context means only actual conflicts in the files edits, not logical conflicts. E.g., if you added a piece of code that calls some_function() and your colleague removed some_function's definition, there would be no textual conflict, but your code just won't work after merging. Therefore, it's important to test your code after all the conflicts have been resolved.

Mureinik
  • 297,002
  • 52
  • 306
  • 350
  • If I got it right, I'm able to pull his changes, but if there are conflicts I have to resolve it manually and I won't lose any of my modified code? Do I have commit it or stash it or something like that? – newhere Aug 12 '22 at 11:55
  • @newhere You can't pull as long as you have uncommitted local changes to files tracked by git (new files that were never `git add`ed are OK), regardless of whether or not there are conflicts, so indeed, you'd have to either commit or stash your changes. WRT losing your modified code - git won't lose your changes itself, but you can always choose to lose them (e.g., if when handling a conflict you decide your colleague's code is better and you'd rather use that and not your changes) – Mureinik Aug 12 '22 at 12:29