0

Suppose,

One Branch:

file1,
No-add-another-branch
file3

Another Branch:

file1, 
file2, 
....

I want to merge all files from One Branch into another branch except "No-add-another-branch" file.

I can do move "No-add-another-branch" file to somewhere place and merge One Branch into another branch but I am looking for a shortcut way. or git way.

Ishk
  • 21
  • 4
  • Does this answer your question? [How to prevent git merge to merge a specific file from trunk into a branch and vice versa](https://stackoverflow.com/questions/773220/how-to-prevent-git-merge-to-merge-a-specific-file-from-trunk-into-a-branch-and-v) – aalbagarcia Oct 12 '20 at 07:42

1 Answers1

0
  1. merge One Branch into Another Branch normally (including No-add-another-branch)
  2. stage deletion of No-add-another-branch (using git rm No-add-another-beanch)
  3. amend the merge commit (git commit --amend)

An alternative is to delete the file in a temporary branch before merging. This creates one extra commit but makes the merge cleaner and the history will be more obvious to read.

  1. git checkout -b tmp OneBranch
  2. git rm No-add-another-branch followed by git commit -m 'Remove the file before merging
  3. git checkout AnotherBranch and git merge tmp
  4. git branch -d tmp

The history should then look like this:

 o--o--o--o Another Branch
         /
        o Remove the file before merging
       /
o--o--o One Branch

If, instead of removing the file, you want to keep it unchanged, you can either replace step 2 of the first approach by git checkout AnotherBranch^ -- No-add-another-branch (i.e., restore its previous version before the merge), or replace step 2 of the second approach by git checkout AnotherBranch -- No-add-another-branch followed by git commit -m 'Make the file equal to Another Branch before merging'.

mkrieger1
  • 19,194
  • 5
  • 54
  • 65
  • I thought we can only adds file before "git commit -amend" – Ishk Oct 07 '20 at 07:30
  • You can also delete, rename, or change files, or change the commit message. – mkrieger1 Oct 07 '20 at 07:39
  • I just came out with additional question: What if both branches have one same file name but don't want to merge them. I mean merge everything except that same files name from both branches – Ishk Oct 07 '20 at 07:45