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.