Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Git Merge some folder

I have two git local branches named A and B (which points to the remote branches origin/A and origin/B.

Branch - A:

Folder-1
   File-11
   File-12
Folder-2
   File-21
   File-22

Branch - B:
Folder-2
   File-22
   File-23
Folder-3
   File-31
   File-32

I want to merge branch B with branch A.

git checkout A
git merge B

The final out come should be like this.

Branch - A:
Folder-1
   File-11
   File-12
Folder-2
   File-21
   File-22 (Branch -A file).
Folder-3
   File-31
   File-32

Merge Folder-3 alone, keep Folder-1 and Folder-2 from branch A.

The basic requirement is, I should not loss logs (and commits) history.

How to do it?

Thanks in advance.

like image 611
Nageswaran Avatar asked Dec 15 '22 08:12

Nageswaran


2 Answers

If you want to mark the commit as a merge commit, here is one way to do this :

# from branch A :
git checkout A

# say "start a merge commit, I initially want to only keep the content of A" :
git merge -s ours --no-commit B
  # -s ours : keep current branch content (current branch is A)
  # --no-commit : do not run the 'git commit' command yet, I will do it later

# say "get me the content of Folder-3 from branch B" :
git checkout B -- Folder-3/

# and commit this new content on top of branch A :
git commit

With this option : branch B will be marked as merged into A, and you will see the history of commits :

$ git log --oneline --graph A
* aa1234 (HEAD -> A) Merged branch 'B' into A
|\
| * bb1234 (B) update 3 on branch B
| * cc1234 update 2 on branch B
| * dd1234 update 1 on branch B
...

If you just want to add the content of Folder-3 from branch B to branch A, and do not care about making this commit a merge commit, here is a simpler way to do this :

# from branch A :
git checkout A

# say "get me the content of Folder-3 from branch B" :
git checkout B -- Folder-3/

# and commit this new content on top of branch A :
git commit
like image 144
LeGEC Avatar answered Dec 28 '22 06:12

LeGEC


Try this out:

$ git checkout branchB
$ git checkout -b branchC
$ rm -rf folderA and folderB //pseudocode. Remove folder A and B
$ git checkout branchA
$ git merge branchC
$ git add -A
$ git commit -m "Added folderC"
$ git push

$ git branch -d branchC
like image 28
Zeeshan Hyder Avatar answered Dec 28 '22 05:12

Zeeshan Hyder