1

I'm currently experimenting with git and encountered the following problem. I created a python file with one function called f1 and saved it under main.py and committed it to the master branch. I then created a branch called b1 of that, went back to the master and changed the name of that function to f2 (no other changes). Back in the branch b1 I added a second function called new_function. After that I tried to rebase b1 onto master.

I was surprised to see that there was a conflict. Why isn't git seeing that I simply changed the name of f1 to f2 in the master?

Am I doing anything wrong? Any suggestions are appreciated.

Nickpick
  • 6,163
  • 16
  • 65
  • 116

2 Answers2

2

You are not doing anything wrong. Git is simply preventing any potential loss of your work. At this point, since function f1 has been changed to f2 in your master branch - your branch b2 still refers to it as f1. So, when you do git rebase, git will simply ask you "so what name do you want it to be f1,f2 or something else.

e.doroskevic
  • 2,129
  • 18
  • 25
  • But in that instance there is no conflict. There have been changes in the master that do not conflict with my branch. So why does t ask me anything? It should be able to see that I was only working on the new fiction. It would do that if I had created it in a new file. – Nickpick Aug 08 '16 at 13:48
  • When you `rebase` your `feature` branch, what `rebase` does is besically takes whatever commits you've got on your `feature` branch and replays them on top of your master or whichever branch you've specified in `git rebase `. If your branch doesn't contain any changes to any of the information preserved on index it originated from you will have no conflict. – e.doroskevic Aug 08 '16 at 13:52
  • "on index it originated from" - is that on a by file basis? – Nickpick Aug 08 '16 at 14:12
  • Yes, it replays the commits on top of whichever branch you've specified. Think of a commit as a snapshot of whatever work you've staged prior to commit. – e.doroskevic Aug 08 '16 at 14:15
  • I understand that, but why doesn't it split the files by function rather than just looking the files as a whole? – Nickpick Aug 08 '16 at 14:18
  • The simple answer to this would be, it has been programmed that way. Why? No idea. Git is used not only by software engineers and web developers, doing it by function simply doesn't make sense – e.doroskevic Aug 08 '16 at 14:22
  • I believe tortoiseSVN does make a split by function – Nickpick Aug 08 '16 at 14:24
  • I haven't used subversion before, therefore I am not in a position to discuss it :D it would only make sense to implement it in such a way (per function) if you are working with programming languages *only*. Say, you are a designer :s does it mean they cannot use it to track their work? Perhaps this could be taken to chat – e.doroskevic Aug 08 '16 at 14:27
  • @nickpick TortoiseSVN does not split by function. TortoiseSVN is just a frontend for SVN and does a normal diff. If you like the way TortoiseSVN did it, maybe you like the way TortoiseGit does it. Or you can just use the mergetool of Tortoise for doing Git merges. Or, as I said before use P4Merge as mergetool as it is even much better than the Tortoise one. – Vampire Aug 09 '16 at 07:37
2

Git - like almost any other VCS - does not know symanitcs of the tracked content, so it cannot guess what is conflicting and what not. If you merge or rebase two histories that both changed the same file, this is a conflict and you have to resolve it. Use a propert tool for this (like P4Merge, the best free diff / merge tool I know) and it is easy as pie to resolve the conflict or in your case see that there maybe is not even a real conflict.

Vampire
  • 35,631
  • 4
  • 76
  • 102
  • In that case would you agree that it is imperative to split the code in as many different files as possible rather than just functions in one file? – Nickpick Aug 08 '16 at 13:55
  • No, that's non-sense. You don't let the VCS dictate how you structure your code. You structure the code how it is suitable for your project / language. And if there are conflicts on merge, well you resolve them with a proper tool. In your case this will be as easy as pressing save in P4Merge after reviewing that there is really no real conflict. – Vampire Aug 08 '16 at 13:59
  • I'm currently using pycharm and there's a tool included for handling the diffs. But it doesn't seem to be able to see than one function changed the name while I added another, so I can only accept the server version or my version of the file as a whole. This does not seem satisfactory. – Nickpick Aug 08 '16 at 14:22