Git merge single file another branch




















Our goal is to merge files from the branch featureA into the master branch. We first switch to the master branch by typing git checkout master at the command prompt. If we type git branch once again we should see the following:. Now, we merge the files that we want from the featureA branch into the master branch as follows:.

We then type git status , which will show us the files that can be committed to the master branch:. Related protips. Creating patch from GitHub pull request Choose local or remote version in a merge conflict Hello I have tried that and result is not expected.

What I try to do is: I have a very old file 1 in branch A that contains a couple of functions that I need I have the latest version of file 1 in branch B I need to merge i do not know if this is the correct concept, maybe rebase? With this solution you propoused, there is a substitution but not a merge. Have a fresh tip? I just wanted to share another approach which seems the cleanest and less convoluted of them all: jasonrudolph.

Confused by the debate over which approach is 'correct'? The higher-voted answer is specific to files; the accepted answer uses cherry-pick, which is specific to commits. Cherry-pick may be key for merging commits selectively, but it can be very painful for moving files from one branch to another. Though commits are the heart of git's strength, don't forget files still have a role! Show 7 more comments. To selectively merge files from one branch into another branch, run git merge --no-ff --no-commit branchX where branchX is the branch you want to merge from into the current branch.

Depending on how you want to merge files, there are four cases: 1 You want a true merge. To select the version in the current branch, run: git checkout HEAD file1 This will retrieve the version of file1 in the current branch and overwrite the file1 automerged by Git. Run: git checkout branchX file1 This will retrieve the version of file1 in branchX and overwrite file1 auto-merged by Git. To explain further with an example, let's say you want to merge branchX into the current branch: git merge --no-ff --no-commit branchX You then run the git status command to view the status of modified files.

You can inspect how the merge was done by running the git diff --cached ; git diff --cached file1 git diff --cached file2 git diff --cached file3 If you find some merge undesirable then you can edit the file directly save git commit If you don't want to merge file1 and want to retain the version in the current branch Run git checkout HEAD file1 If you don't want to merge file2 and only want the version in branchX Run git checkout branchX file2 If you want file3 to be merged automatically, don't do anything.

Git has already merged it at this point. Finally, don't forget to git commit. JamesThomasMoon 4, 5 5 gold badges 32 32 silver badges 41 41 bronze badges. Careful though: If the git merge --no-commit branchX is just a fast-forward, the pointer will be updated, and the --no-commit is thus silently ignored — cfi.

This solution gives the best results and flexibility. Unlike the answer with the most votes, this solution preserved my merge history, which is important to me as I weave partial commits back and forth across branches. I didn't try all of the other suggested solutions, so perhaps some of them do this as well. This merge works in one direction, but the files that you did not include in the merge are seen as DELETED if you decide to merge your upstream master back into the source branch.

I use a git-flow like process using a master branch production main-line , a staging branch staging server main-line , and topic branches based off of the staging branch. Using this strategy has resulted in my "reverse merge" from master back into staging to competely fail thinking everything I didn't merge from staging to master is deleted. This includes whole files and hunks. Show 6 more comments. There is no --interactive argument you can pass to git merge.

Here is the alternative: You have some changes in branch 'feature' and you want to bring some but not all of them over to 'master' in a not sloppy way i. However, if you remove everything, the rebase will be aborted. I'm using this technique now and it seems to have worked really well. Unfortunately, it appears the the link in the answer is dead. Not only is the link dead, but it has a WOT poor reputation warning.

Therefore I removed it. This means that you can use git checkout -p to selectively discard edits from your current working tree. Chronial Chronial This is by far the easiest, simplest method, as long as you only have a manageable number of changes to merge in. I hope more people will notice this answer and upvote it. Similar answer posted on this question: stackoverflow. Oh, I didn't know checkout had a patch!

Truly the most simplest method. I tried with e and I could even edit the patch before applying How cool is it. A true one liner for merging selective files from other branches. Eric Hu Eric Hu This is beautiful. This works ok, but added two extra commit objects. Not a huge deal but a bit messy — MightyPork.

MightyPork you're right. Unfortunately, since I wrote this so long ago, I'm no longer sure why the "git stash" and "git merge stash" steps are in there instead of a "git commit". Oh that's clear, I think. This way it merges the one file, not necessarily overwriting previous changes on the target branch. Show 3 more comments. Create if not created a local branch with the changes you want to bring in. Cory Cory Tip: first use the git cherry command see manual first to identify commits you haven't yet merged.

This works.. Run git cherry-pick -x hash-of-commit and resolve merge conflicts are you are good to go. Your link is not working anymore. Can you update it please? The link is effectively broken: "Authentication Required The site says: 'Restricted Zone'". This won't merge. It'll just overwrite the changes on master with the changes from the feature1 branch.

Some times all you want to do is replace the whole file, so this is what I wanted, but you need to make sure you want to lose all the changes you made to this file. Cleanest solution, given that the OP specifically wanted to replace the entire file with the equivalent on another branch: 2.

Manual copying of common files into a temp directory followed by Slashes don't need to be escaped. Check out the man page for git-apply for more options. Step three: there is no step three Obviously you'd want to commit your changes, but who's to say you don't have some other related tweaks you want to do before making your commit.

For me, all changes are rejected. Any idea why? First, you'll take the unusual step of declaring in advance that what you're about to commit is a merge, without Git doing anything at all to the files in your working directory: git merge --no-ff --no-commit -s ours branchname If you were merging from more than one other branch, repeat as needed.

Now the files from the other branch are in the index, ready to be committed, with history. Your first command git merge --no-ff --no-commit -s outs branchname1 is exactly what I was looking for! With multiple branches, required history, need to merge single file s and have to change the content of the file before pushing, this seems to be a decent alternative. This worked perfectly, The only change I made was to do a git merge --continue instead of the final git commit. This is my workflow for merging selective files.

Make a new branch this will be temporary git checkout -b newbranch Grab the changes git merge --no-commit featurebranch Unstage those changes git reset HEAD You can now see the files from the merge are unstaged Now you can chose which files are to be merged. Felix Felix 1 1 gold badge 8 8 silver badges 7 7 bronze badges. I used a slight variation on this. Instead of merging I cherry-picked.

It does the job. The only downside to this approach is you lose the reference to the original commit hash. The easiest way is to set your repository to the branch you want to merge with, and then run git checkout [branch with file] [path to file you would like to merge] If you run git status you will see the file already staged Then run git commit -m "Merge changes on '[branch]' to [file]" Simple. This almost the best answer I found. I was just about to answer like this, I thought I invent new things that haven't been answered yet!

But this is the the most simple way to do it. This should be on top! You can do it easily: Just add this line to [alias] section in your global. JimStar JimStar 1 1 silver badge 6 6 bronze badges. Stunner Stunner This doesn't really merge, it overwrites the file on current branch.

One just has to be very careful. It is a mix of some answers. Felipe Felipe This is indeed usefull and could be added to what is for me the best answer, the alvinabad's answer. When doing: git checkout HEAD file1 to keep the current version and unmerge the file file1 , one can use -p option to select part of the file to be merged. What does this do?



0コメント

  • 1000 / 1000