0

I have a main branch. From main i create a branch A. I switch to that branch and clone a repo from github. Now there is a folder with repo on branch A.

The problem is that being on branch A i do not see commit history of cloned repo, unless i cd in cloned repo folder. After cd i can see history, but this is a completely different branch. I also need to create Pull Request, which I can't because GitHub says there is nothing to compare, branches have entirely different commit histories

Can I somehow merge/rebase branch from folder into main/other branch?

  • 2
    You can not create a branch first and then clone a repository!? and even if you can then you are standing on a completely different repo. Its the other way around. You clone the repo first then you create a branch from e.g. master/main. – Aram.B Jul 05 '22 at 07:15
  • @AramB But what if i need files and commit history from repo i want to clone in my branch? – Danyil Derkach Jul 05 '22 at 07:17
  • 1
    What is the relationship between your own repo and the cloned repo? Do you intend to add the cloned repo as a submodule of your own repo, or they are the same repo (or of the same level) and you want to manipulate different branches? – ElpieKay Jul 05 '22 at 07:18
  • @ElpieKay My repo is a private repo, that was created by school. The cloned repo was a school task, template – Danyil Derkach Jul 05 '22 at 07:22
  • Did you check this https://stackoverflow.com/a/10548919/3497293 ? – Krishnom Jul 05 '22 at 15:49
  • 1
    In Git, the phrase "branch that is in a folder" is nonsense. The word *branch* is abused (it has many meanings) but none of them go with "in a folder". As others have said, it sounds like you have *two separate repositories* here. A Git *repository* is a primarily collection of commits, and Git typically *stores* a repository *in* a hidden `.git` folder, so if you have two existing folders, each one can have its own hidden `.git`, in which the two separate repositories reside. – torek Jul 05 '22 at 19:51

1 Answers1

1

If I understand you correctly, you have two completely separate repositories, and you want to merge them, while keeping both commit histories. Is this correct? This is a bit tricky, but doable.

So you have your main repository (A) and want to include the whole history of another, completely separate repository (B) located in a subfolder of (A).
(Wherever I use (A) or (B), you should replace it with your actual repository names without those parentheses.)

Both repositories think, that their files live at the root of their working directory. They don't care, where those are on your computer. So you cannot simply push it in a directory and combine those two.

However, you can checkout (B) first, move everything inside a subfolder, and commit it.

Then, go to (A), add (B) as a remote (git remote add (B) path/to/(B)) and merge them like so:

git fetch (B)
git merge (B)/main --allow-unrelated-histories

The --allow-unrelated-histories argument is very important, so Git knows, that you intentionally wand to merge those separate repos.

If you have conflicts (Files, that were present in both repositories), you should be able to merge them manually and commit it afterwards, just like any other git conflicts.

Now you have the subfolder of (B) inside your (A) repository, and you may commit, modify and push like you want.

dallyger
  • 126
  • 1
  • 6