Home > Mobile >  Setup Branches with GIT
Setup Branches with GIT

Time:04-14

I have various things I've written, and rewritten a couple times, without keeping track, and git seems the perfect solution to merge my disjointed code. However, the tutorials had me running around in circles for 3 hours now, and it shouldn't be this difficult.

Once I get to conflicts, I think I know how to merge them, but I can't seem to get branches setup.

This will create the situation I want to resolve:

mkdir tmp
cd tmp
mkdir a b c d merge
echo "foo" >> a/foo
echo "foo" >> b/foo
echo "foo" >> c/foo
echo "foo" >> d/foo
echo "bar" >> b/foo
echo "bar" >> c/foo
echo "bar" >> d/foo
echo "totally" >> c/foo
echo "totally" >> d/foo
echo "beyond" >> d/foo
cd a 
git init ; git branch -m a ; git add . ; git commit -m initial
cd ../b
git init ; git branch -m b ; git add . ; git commit -m initial
cd ../c
git init ; git branch -m c ; git add . ; git commit -m initial
cd ../d
git init ; git branch -m d ; git add . ; git commit -m initial
cd ../merge/
git init ; git branch -m merge ; git add . ; git commit -m initial

Now, what I want to do is pull a into merge, check it; then pull b into merge, resolve conflict (diff); then pull c... Repeating until "merge" contains everything, corrected.

But everything I try does something weird, like, it puts branches into sub-folders, not actually merging them. And every tutorial I have read anywhere just talks about how to checkout from github... I'm not using git hub, it works fine locally, I want to just use it locally.

Help?

CodePudding user response:

The problem here is that you are running git init on each individual folder. This creates a new repository. At the end of your scripts above, you have five different repositories, rather than one repository with multiple branches.

This essentially looks like:

|      |      |      |      |
A      B      C      D      E

When you want:

 main ----------------------------------------------
       \ -- A -- /  \          /   \             /
                     \-- B -- /     \           /
                                     \ -- C -- /

In the above, the main branch contains all of the code from the various feature branches after a merge.

To achieve this, you only need to run git init once, in the main project folder. .git applies to all subfolders by default, so when you attempt to git add any files from subfolders, they will automatically become part of the singular repository, and get picked up by git diff.

After merging (with git merge), your main branch will contain the commits from A, B, and C.

CodePudding user response:

This might be slightly crazy, but I think this is what you're going for?

mkdir tmp
cd tmp
mkdir a b c d merge
echo "foo" >> a/foo
echo "foo" >> b/foo
echo "foo" >> c/foo
echo "foo" >> d/foo
echo "bar" >> b/foo
echo "bar" >> c/foo
echo "bar" >> d/foo
echo "totally" >> c/foo
echo "totally" >> d/foo
echo "beyond" >> d/foo
cd a 
git init ; git branch -m a ; git add . ; git commit -m initial
cd ../b
git init ; git branch -m b ; git add . ; git commit -m initial
cd ../c
git init ; git branch -m c ; git add . ; git commit -m initial
cd ../d
git init ; git branch -m d ; git add . ; git commit -m initial
cd ../merge/
git init ; git branch -m merge ; git commit -m initial --allow-empty

git remote add a ../a
git remote add b ../b
git remote add c ../c
git remote add d ../d

git fetch a
git fetch b
git fetch c
git fetch d

git merge a/a --allow-unrelated-histories --no-edit
git merge b/b --allow-unrelated-histories
git merge c/c --allow-unrelated-histories
git merge d/d --allow-unrelated-histories

Note that the first change I made to your sample commands was adding --allow-empty to the initial commit for the merge/merge branch since there were no files on that branch.

Image of resulting commit-graph for the merge/merge branch.

enter image description here

  • Related