Introduction
When working on a project, sometimes you might need to merge a file or several files from one branch to another. In this post I will show you how you can do that.
How to merge select files
First, type git branch
in the command prompt to find out what branch you are currently on and list what branches are available for the project you wish to carry out the merging in. In the example below, featureA
is the branch that is currently selected:
$ git branch
master
* featureA
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 typegit branch
once again we should see the following:$ git branch * master featureA
-
Now, we merge the files that we want from the
featureA
branch into themaster
branch as follows:git checkout featureA src/app/components/subscription/subscription.component.ts src/app/components/subscription/subscription.component.html src/app/components/subscription/subscription.component.css
-
We then type
git status
, which will show us the files that can be committed to themaster
branch:$ git status On branch master Your branch is up to date with 'origin/master'. Changes to be committed: (use "git restore --staged <file>..." to unstage) new file: src/app/components/subscription/subscription.component.ts new file: src/app/components/subscription/subscription.component.html new file: src/app/components/subscription/subscription.component.css
-
Now, type
git commit -m "Subscription component"
orgit commit
if you want to type your commit message in a text editor. -
Once you are done with your commit message, you can then type
git push
to push your commit to themaster
branch.
Conclusion
The steps above are the simplest way that I have found in order to merge select files from one branch into another. I hope this blog post helps many developers who have tried to find a simple and easy way to merge select files from one branch into another in git.