0

I've had git behaviour that I quite didnt understand:

I have production branch B1.

git checkout B1
git checkout -b B2

I work on a solution, merge it with B1 localy with

git checkout B2
git rebase B1
git checkout B1
git merge B2
git branch -d B2

Then I do the same routine - create branch B3 from B1, work on B3, and merge with B1. Now multiple merge conflicts occured (literaly between every single commit of B2). So I rebased and merged them same as I did with B2. Code pulled and pushed gratefully, git status showed no commits behind on B1 or B3.

Later, when my colleague pulled B1, he faced all those merge conflicts all over again. A total nightmare, that I though shouldn't happen.

Does anybody know how those merge conflicts could have been moved to remote without the commits that resolved them? I'd like to recall what (possibly terrible) mistake I've done not to repeat it again ^^

___edit The story form console:

git checkout -b B2
git add file1
git commit -m "b2c1"
git rebase B1
git add file1
git commit -m "b2c2"
git push
git rebase B1
git checkout B1
git pull
git checkout B2
git rebase B1
git add file1
git commit -m "b2c3"
git rebase B1
git merge B1
git push B1
git checkout B1
git pull
git merge B2
git push
git branch --merged master
git branch -d B2
git checkout -b B3
git pull
git add file1
git commit -m "b3c1"
git push
git rebase -i HEAD~5
git push -f
git add file1
git commit -m "b3c2"
git add file2 file3 file4
git commit -m "b3c3"
git stash
git pull
git push 
git checkout B3
git stash push
git stash save
git stash apply
git checkout B3
git rebase B1
git checkout -- .
git checkout B1
git merge B3
git push

something along those lines, I had to sqeeze many more line cause it otherwise would be a 200/400 line log.

Yurkee
  • 795
  • 2
  • 9
  • 23
  • 1
    I think your colleague should have done git pull --rebase. – Siri Jun 27 '19 at 15:17
  • Thank you. I've read the man to git-pull --rebase and it really seems like a good solution. How may I check if that wasn't the default option on his repo? – Yurkee Jun 27 '19 at 15:22
  • 1
    In general, as the other comment suggests, I've seen this sort of problem if rebasing branches (which puts any changes in the currently checked out branch at the top of the commit tree) rather than merging. You could try again without the `git rebase B1` and just merging `B2` in to `B1`, which might help. – Matt Pitkin Jun 27 '19 at 15:25
  • If you rebase a *public* branch, anyone who might have had a copy of that branch needs to delete their local version and get it again from origin. If you do not do this, then git will merge the two versions! – crashmstr Jun 27 '19 at 19:59

1 Answers1

0
git add file1
git commit -m "b2c1"
git rebase B1

This rebase is not necessary as b2c1's direct parent is B1. Assuming that B2 was created from B1 in the intial step.

git commit -m "b2c2"
git push
git rebase B1

Why are you doing a rebase after the push, this is counter intuitive. I would suggest don't do a push until you have your local history setup the way you want to have it, then publish it with the push

git checkout B1
git pull

Ok, this will update your local B1 based on what the remote currently has.

git checkout B2
git rebase B1

This is the first rebase that I think is actually necessary, but only if B1 has changed.

git add file1
git commit -m "b2c3"
git rebase B1

Again you have a rebase that isn't needed

git merge B1

You shouldn't need to do a merge of B1 into B2 at this point since B1 hasn't changed.

It seems like you might have some fundamental misconceptions regarding git and how it operates. Which if you are fairly new to git isn't really that surprising. It took me some time to get my head wrapped around it. I would suggest you look at the Pro Git book: https://git-scm.com/book/en/v2 to help understand some of the basic concepts.

My best advise though is to try and keep things simple, atomic, and deliberate. Keeping those things in mind will help you greatly.

git push B1
git checkout B1
git pull
git merge B2
git push
git branch --merged master
git branch -d B2
git checkout -b B3
git pull
git add file1
git commit -m "b3c1"
git push
git rebase -i HEAD~5
git push -f
git add file1
git commit -m "b3c2"
git add file2 file3 file4
git commit -m "b3c3"
git stash
git pull
git push 
git checkout B3
git stash push
git stash save
git stash apply
git checkout B3
git rebase B1
git checkout -- .
git checkout B1
git merge B3
git push
Matt
  • 21
  • 4