2

What is merge conflict ?
Most questions out there address resolving merge conflict like this one.

But I have no clue to why its even happening to me...


In short, we were told to:

git clone url ~/lab

Now we had to edit these files and then commit the changes.


For second assignment we need to do the following:

 git commit -am 'changes to lab1 after handin'
 git pull
 git checkout -b lab2 origin/lab2
 git merge lab1

Now my partner sent me his files for lab1. So I can work on his code as it's cleaner and more organized.

git status
>>> nothing to commit, working directory clean

I then delete all files

git status
>>> list of deleted files

git commit -am 'delete my files'
git status
>>> nothing to commit, working directory clean

I then extracted his files to my directory

git status
>>> list of untracked files in red
git add .
git status
>>> Changes to be commit and the list of files in green
git commit -am 'teammate files'
git status
>>> nothing to commit, working directory clean

Now TA commands

git commit -am 'changed branch'
git pull 
>>> Already up-to-data. (As expected!!)
git checkout -b lab2 origin/lab2
>>> On branch lab2 
git merge lab1
>>> almost all files failed merge and have conflict

Any explanation to why is this happening ? and how could I prevent this ? I am afraid to do this on my own as I might remove code which shouldn't be removed... so I'd rather git merge to work on its own.

Tony Tannous
  • 14,154
  • 10
  • 50
  • 86
  • Why do you delete all files? And what's `git commit -am 'changed branch'` supposed to do? – tkausl May 05 '17 at 16:04
  • @tkausl I deleted my files as I got my teammate files. I want to work on his files. That commit doesn't do anything... an extra commit. – Tony Tannous May 05 '17 at 16:06
  • 1
    The solution to this sort of problem is almost always _figure out what your current state is_. Not what you think it is, but what it actually is. – Useless May 05 '17 at 16:51
  • 1
    For example, when I run `git checkout -b` for an already-existing branch, I get `fatal: A branch named 'X' already exists.`, but you show it succeeding. This means I don't trust your transcript. – Useless May 05 '17 at 16:51
  • 1
    Run `git log --all --decorate --graph` (or use a gui if you prefer) - but see what the actual state of your repo is. Trying to reason based on what you believe it ought to be is futile, and pointless since it's easy to examine. – Useless May 05 '17 at 16:52
  • What do the merge conflicts look like? Did you even want to merge, or do you just want to submit what you have? If you just want to submit what you have (i.e. merge but always take your change on conflict), then there's a good way to do that. – Matt Messersmith May 05 '17 at 17:35
  • @mwm314 no I don't want to submit. I am moving to a new branch with new files along side the older ones that don't have the implementation. So I need to merge. Trying to figure what the others wrote and I will update. – Tony Tannous May 05 '17 at 17:36
  • @Useless sorry ? do you think I just put commands and outputs randomly ? – Tony Tannous May 05 '17 at 17:37
  • No, I'm sure you pasted what you did, but it can't be both complete and as shown, because as I said that should give you a different result. So, something is wrong somewhere and _it is always better to compare your beliefs to reality when possible_. It's _easy_ to find out the real state of your repo in git, so _do that_. – Useless May 05 '17 at 20:23
  • Deleting all files means you are most likely using version control incorrectly. Instead you should create a new branch and copy files to your writing directory. I suggest that you read, or at least skim, the first three chapters of Git Pro which is available freely online. – Code-Apprentice May 06 '17 at 03:17

1 Answers1

2

Merge conflict occurs when your changes conflict(will need to overwrite what is on the remote repository, name it master or the branch where you pushing your changes onto) with another person of your team's changes(his changes since last time you pulled would overwrite your changes that you are trying to push, or you will overwrite his changes). Normally you would resolve this conflict by merging. You should be able to see the remote changes, your changes and the outcome. You can select what parts of the code to delete/add/keep.

HaroldSer
  • 2,025
  • 2
  • 12
  • 23