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:
Step 2: List Repository Content
Then, display the list of the repository by running the βlsβ command:
Step 3: Generate and Modify New File
Next, execute the below-given command to generate and update the new file:
Step 4: Display Git Repository Status
After that, check the status of the repository by running the following command:
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:
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:
Step 7: View Current State of Working Repository
Now, execute the βgit statusβ command to check the status of the current working repository:
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:
Step 9: Check Status
Lastly, verify the newly added unstaged changes of another branch into the current working branch through the provided command:
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.