Git

How to Copy Commits From One Branch to Another?

Git commits are the changes that developers make in projects’ local repositories and then push to remote repositories. On a typical project, several developers might work on various Git branches. In such a scenario, Git users occasionally want to access the commits of another branch. Another possible situation is that the user wants to delete or reset the branch and move all changes to another branch. Not sure how to create a backup of a branch? Do not worry!

This blog will explain:

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:

$ cd "C:\Git\Test"

Step 3: Make a New File
Make a new file to commit new changes in the Git repository:

$ touch text.txt

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:

$ git add text.txt

After that, check the repository status to verify if the file is added to the tracking index or not:

$ git status

Step 5: Commit Tracked Changes
In the next step, commit the tracked changes into the local repository using the provided command:

$ git commit -a -m "Text file is committed"

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:

$ git branch copy-commit

Step 7: Move to New Branch
After that, move to the newly created branch by utilizing the Git “git checkout” command:

$ git checkout copy-commit

Step 8: Check Git Logs
To determine whether the commits are copied into the new branch or not, check the Git logs:

$ git log

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:

$ git branch

Step 2: Go to Git Branch
Next, use the “git checkout” command to switch to the selected branch:

$ git checkout features

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:

$ git rebase master

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:

$ git log

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.

About the author

Rafia Zafar

I am graduated in computer science. I am a junior technical author here and passionate about Programming and learning new technologies. I have worked in JAVA, HTML 5, CSS3, Bootstrap, and PHP.