Git

How to Merge Specific Files From Git Branches

While working on a large codebase project or codebase with multiple developers working on new modules/features, it can be possible users may need to merge a particular file from a branch instead of an entire commit or all added changes on the current working branch.

This blog will briefly explain the method of merging particular files from the Git branches.

How to Merge Specific Files From Git Branches?

In order to merge the particular files from Git branches, try the provided steps:

    • Redirect to the desired repository.
    • List the repository content.
    • Generate and modify a new file.
    • View the existing branches and switch to the target branch.
    • Check its current status.
    • Execute the “git add <file-name>” command and verify it.

Step 1: Move to Git Repository

Use the “cd” command along with its path and switch to it:

$ cd "C:\Users\nazma\Git\repo_16"

 
Step 2: List Repository Content

Then, display the list of the repository by running the “ls” command:

$ ls

 

Step 3: Generate and Modify New File

Next, execute the below-given command to generate and update the new file:

$ echo "one python file" >> "file2.py"

 

Step 4: Display Git Repository Status

After that, check the status of the repository by running the following command:

$ git status .

 
As you can see, the working directory contains the “file2.py” file:


Step 5: Check Git Branches List

Execute the “git branch” to view the list of all existing local branches:

$ git branch

 
The below-given output displays all branches and we have selected the highlighted branch for further process:


Step 6: Navigate to Selected Branch

Next, switch to the previously selected branch by utilizing the “git checkout” command:

$ git checkout beta

 

Step 7: View Current State of Working Repository

Now, execute the “git status” command to check the status of the current working repository:

$ git status .

 
It can be observed that the unstaged changes of the “master” branch move to the “beta” branch:


Step 8: Track Changes

Next, push the working area content to the staging index using the “git add” command:

$ git add file2.py

 

Step 9: Check Status

Lastly, verify the newly added unstaged changes of another branch into the current working branch through the provided command:

$ git status .

 
According to the below-given output, we have merged the particular file from the branch successfully:


Here, you have learned the efficient way of merging particular files from the Git branches.

Conclusion

To merge the particular files from Git branches, first, move to the desired repository and list its content. Then, generate and modify a new file simultaneously. After that, view the existing branches and switch to the target branch. Check its current status and execute the “git add <file-name>” command and verify it. This blog illustrated the process of merging particular files from the Git branches.

About the author

Maria Naz

I hold a master's degree in computer science. I am passionate about my work, exploring new technologies, learning programming languages, and I love to share my knowledge with the world.