1

In master branch, I have the Initial Commit pushed gitlab

After that I create a new branch called "branchA"

I rename the initial commit with

git commit --amend -am Initial Commit v2

and do some modif and commit it

In this example, you can see something like that

master :
    - Initial Commit
branchA :
    - Initial Commit v2
    - Commit number 2
    - ....

I want to merge branchA into master how can I do that?

mega6382
  • 9,211
  • 17
  • 48
  • 69
qsgfesqk
  • 43
  • 2

1 Answers1

0

You didn't rename the initial commit. Instead, you made a new, different initial commit. You now have two initial commits:

$ mkdir tworoots
$ cd tworoots
$ git init
Initialized empty Git repository in ...
$ echo example of multiple roots > README
$ git add README && git commit -m 'first root'
[master (root-commit) b89218d] first root
 1 file changed, 1 insertion(+)
 create mode 100644 README
$ git branch branchA
$ git log --all --decorate --oneline --graph
* b89218d (HEAD -> master, branchA) first root

Note how both branch names master and branchA select commit b89218d, which is my initial commit.

Now if I make a new initial commit:

$ echo forgot something > afile
$ git add afile
$ git commit --amend -m "second root"
[master c982f45] second root
 Date: Thu Feb 4 02:27:37 2021 -0800
 2 files changed, 2 insertions(+)
 create mode 100644 README
 create mode 100644 afile

This new commit has a new hash ID, c982f45 (your commits won't have this hash ID since this depends on my name, my email address, the date-and-time on my computer, and so on). But the first root still exists, and branchA still finds it:

$ git log --all --decorate --oneline --graph
* c982f45 (HEAD -> master) second root
* b89218d (branchA) first root

It's hard to tell from this particular graph (due to the --oneline option) that these two commits are completely unrelated. Run the same git log command but without --oneline and note that there's no connecting line between the commits, to see that.

If we now git checkout branchA and make more commits, they have the root commit of branchA as their (single) root commit:

$ git checkout branchA
Switched to branch 'branchA'
$ echo data go in files and the files go in commits > datafile
$ git add datafile
$ git commit -m 'add some data'
[branchA 12fe86a] add some data
 1 file changed, 1 insertion(+)
 create mode 100644 datafile

A git log with our other usual options, but this time without --all, shows that master's commit is not related to these commits:

$ git log --decorate --oneline --graph
* 12fe86a (HEAD -> branchA) add some data
* b89218d first root

An attempt to merge these two disjoint subgraphs produces an error (since Git 2.9):

$ git checkout master
Switched to branch 'master'
$ git merge branchA
fatal: refusing to merge unrelated histories

You can force git merge to attempt the merge, as previous versions of Git would, by adding the --allow-unrelated-histories option. However, without a good understanding of git merge, this is rarely a good idea. In the example I built, it works. If it works for you and does what you want, that's fine, but be sure it's really what you want.

torek
  • 448,244
  • 59
  • 642
  • 775