How to Commit Some Files in a Branch and Make Them Available in Another
This tutorial shows how you can commit specific files in a branch and make the files available in another branch. Take a situation where you have a project, and you create a feature
branch to do some light modifications to the code.
You modify and add new files to the feature branch, and you only need to commit the common files between the master
and feature
branches.
How do you go about it?
Git Commit Some Files in a Branch and Make Them Available in Another
Let’s take a look at an example.
In the image above, our feature
branch has modified and newly added files. The modified files are also present in master
.
We only want to commit the modified files and make them available in the master
branch. Here is how we do it.
We will first add the modified file. Some may add one file at a time, which still works but takes time and energy.
We can run git add -u
to only stage the modified files and make a commit.
Since we only need to make the files available in the master
branch, we will use the git cherry-pick
command instead of merging the branches.
Run the git log --oneline
command to get a list of all the commits in your branch. Note the SHA-1
or hash of the latest commit and switch to the master
branch.
We can run the git cherry-pick
command, as illustrated below.
$ git cherry-pick be8ed67
The above command should apply the changes in the commit to our master
branch.
You can run the git push
command if you want to update the remote.
In a nutshell, committing specific files and availing them in another branch is possible. The git cherry-pick
command allows us to apply changes from a specific commit.
Always remember to stash changes when switching branches.
John is a Git and PowerShell geek. He uses his expertise in the version control system to help businesses manage their source code. According to him, Shell scripting is the number one choice for automating the management of systems.
LinkedIn