1

I have a branch named test and i want to merge it to master. i am using below git diff command in shell script to get the changed file name

->master=git diff release/1..master --name-only . ':(exclude)*.yml' ':(exclude)*.yml' ':(exclude)*.gitmodules'

master variable has all the file names such as test1.py test2.py and template/test3.py

I tried using below command but not able to merge it

git checkout master
git checkout release/1 $master

Could you let me know the command which merges specific file from specific folder to master?

kyb
  • 7,233
  • 5
  • 52
  • 105
sumesh
  • 55
  • 7
  • Found a better answer than mine: https://stackoverflow.com/questions/18115411/how-to-merge-specific-files-from-git-branches – Monsieur Merso Feb 02 '22 at 07:27
  • 1
    Can you clean up your question? It's a bit difficult reading it with git commands, files and file contents intermixed with text with no clear way to determining which is which. To be blunt, you can't merge single files from one branch to another, you merge whole branches. That's the only type of merge git can do. – Lasse V. Karlsen Feb 02 '22 at 09:55
  • cleaned the question. need a command which will merge a specific files from a branch to master. checked other links but not able to find an answer – sumesh Feb 02 '22 at 12:46

1 Answers1

2

If you need to take just one file from branchX to current one:

git checkout branchX -- path/to/file

then commit.

If need to create merge commit but drop any changes except one file (not recommented!)

git merge branchX --no-commit
git checkout -f  ## drop any changes
git checkout branchX -- path/to/file
git commit  ## create merge commit with the only file taken from source branch
kyb
  • 7,233
  • 5
  • 52
  • 105