How to merge specific files in git

Suppose I have a project on a MASTER branch with 100 php files. To perform error correction in the project, I create a separate branch

git checkout -b bugfix 

Then, after fixing the error in 3 files (for example, index.php, register.php and login.php), I will merge it into the main branch

 git checkout master git merge bugfix 

The above code will merge all 3 files contributed by me, but still, can I get GIT to merge only 2 files, say only login.php and register.php?

+6
source share
4 answers

The following decision was made from a blog post

It turned out that checking can also be useful in this matter. You can simply checkout those specific files from another branch:

 # switch to the branch you want to be your merge destination git checkout master # checkout specific files from specific branch git checkout bugfix login.php register.php # check the status git status # merge them in git commit -m "your merge comment" 
+3
source

It is not possible to write partial merges in Git, all merges must combine all the root trees of all the joins to be merged.

You can, of course, decide to allow this merge by selecting an unaltered copy of one of the files from one of the parents, but this is still a “complete” merge, and the changes in this file will be considered merged when it comes to subsequent permutations or merges between the involved branches .

0
source

No, there is no direct path. Though; You can select files from the branch you want to merge and overwrite them on top of existing ones in the main branch. The teams are in order:

 git checkout master git show bugfix:login.php > login.php git show bugfix:register.php > register.php git add . git commit -m "Select login and register from branch/bugfix" 

Well, the above method will “destroy” the file history. There is an old discussion about the same in this mailing list .

From a technical point of view, the thing that makes history important (and why a “set of commits in date order” is useless) because it gives us a GENERAL ANCHOR! And that is the only thing that matters.

Now, what is fundamentally wrong to do in file history?

Now, if you followed this argument, you should go "Aaahh! Obvious!".

Ignoring many paragraphs; you will find a workaround:

 git diff commit..othercommit filename | git-apply --index && git commit 

In your case, commit = hash from your HEAD / master branch; and othercommit = hash of the patch branch.

0
source

You can simulate the behavior you want, but what happens to the changes you make to the third file?

Changes in the third file remain in the commit on the branch, but they will not be present in the combined commit, since the file was not changed at all in the branch.

Here's how you can do it:

 git checkout master git merge bugfix --no-commit 

The --no-commit tells git merge the files, but stops before they are executed. Now you can check files, modify them, do whatever you want. To undo the changes you made to index.php , you can run:

 git checkout master -- index.php 

By running git status , you can see that index.php no longer appears as changed (neither in the index, nor in the working tree). Now you can git commit and that’s it.

0
source

Source: https://habr.com/ru/post/946753/


All Articles