This blog will explain:
- Method 1: How to Copy Commits of One Branch to Another by Creating a New Branch?
- Method 2: How to Copy Commits of One Branch to Another Using the git rebase Command?
Method 1: How to Copy Commits of One Branch to Another by Creating a New Branch?
To copy the commits of one branch into a new branch, first, open the Git repository, open the branch to which you want to copy commits and then commit the tracked changes. After that, create a new branch that will automatically copy the commits of the current branch.
To copy the commits of one branch into another branch, look at the below-listed steps.
Step 1: Open Git Bash Terminal
First, open the Git Bash terminal from the Startup menu:
Step 2: Open Git Working Repository
Move to the Git working repository by utilizing the “cd” command:
Step 3: Make a New File
Make a new file to commit new changes in the Git repository:
Step 4: Add Untracked File to Tracking Index
Next, add the untracked file to the Git tracking index (staging area) using the “git add” command:
After that, check the repository status to verify if the file is added to the tracking index or not:
Step 5: Commit Tracked Changes
In the next step, commit the tracked changes into the local repository using the provided command:
Step 6: Create New Branch
Next, create a new branch that will copy the commits of the current branch automatically. For instance, we have created a “copy-commit” branch:
Step 7: Move to New Branch
After that, move to the newly created branch by utilizing the Git “git checkout” command:
Step 8: Check Git Logs
To determine whether the commits are copied into the new branch or not, check the Git logs:
It can be observed that we have successfully copied the commits from one branch to the newly created branch:
Method 2: How to Copy Commits From One Branch to Another Using the git rebase Command?
To copy the commits from one branch to another using the Git command “rebase”, first, choose the branch in which you want to copy the commits of the other branch using the “git branch” command. Next, move to that branch and utilize the “git rebase <branch-name>” command. In the “rebase” command, specify the branch name from which you want to copy the commits to another branch.
Follow the given instructions to copy one branch commits into another.
Step 1: Check Branches
Firstly, check all local branches through the “git branch” command and choose the branch where you want to copy the commits. For instance, we have selected the “features” branch:
Step 2: Go to Git Branch
Next, use the “git checkout” command to switch to the selected branch:
Step 3: Copy Commit of Branch
Use the “git rebase” command to copy the commit of one branch into the current branch and specify the name of the branch whose commits you want to copy:
For this purpose, we have copied the commits of the “master” branch into the “features” branch:
Check the Git logs of the current branch to verify whether the commits or copied or not:
The below output shows that we have successfully copied the commits through Git “rebase” command:
We have taught you the methods for copying the commits of one branch into another.
Conclusion
To copy the commits of one branch into another, Git users can create a new branch or utilize the Git “rebase” command. In the first method, you must first commit the branch changes. Then, create a new branch that will automatically copy the commits of the current branch. In the second approach, first, open the branch where you want to copy the commits. Then, utilize the “git rebase” command along with the branch name from which you want to copy the commits. This blog has demonstrated the methods for copying the Git commits of one branch into another.