ℹ️ Skipped - page is already crawled
| Filter | Status | Condition | Details |
|---|---|---|---|
| HTTP status | PASS | download_http_code = 200 | HTTP 200 |
| Age cutoff | PASS | download_stamp > now() - 6 MONTH | 0.1 months ago |
| History drop | PASS | isNull(history_drop_reason) | No drop reason |
| Spam/ban | PASS | fh_dont_index != 1 AND ml_spam_score = 0 | ml_spam_score=0 |
| Canonical | PASS | meta_canonical IS NULL OR = '' OR = src_unparsed | Not set |
| Property | Value |
|---|---|
| URL | https://www.squash.io/how-to-move-recent-commits-to-a-new-branch-with-git/ |
| Last Crawled | 2026-04-13 06:24:39 (3 days ago) |
| First Indexed | 2023-10-29 03:11:35 (2 years ago) |
| HTTP Status Code | 200 |
| Meta Title | How to Move Recent Commits to a New Branch with Git |
| Meta Description | Guide to relocate your latest commits to a new branch using Git commands. |
| Meta Canonical | null |
| Boilerpipe Text | Method 1: Using the git branch and git cherry-pick commands
Method 2: Using the git rebase command
Best Practices and Considerations
Moving recent commits to a new branch in Git can be done in a few simple steps. This process is particularly useful when you realize that certain commits should have been made on a separate branch instead of the current branch. It allows you to maintain a clean commit history and organize your code more effectively. Here are two possible ways to achieve this:
Method 1: Using the git branch and git cherry-pick commands
1. First, create a new branch at the current commit using the following command:
git branch <new-branch-name>
For example, if you want to create a new branch named "feature-branch" at the current commit, you would run:
git branch feature-branch
2. Switch to the new branch using the git checkout command:
git checkout <new-branch-name>
For example, to switch to the "feature-branch", you would run:
git checkout feature-branch
3. Identify the commit(s) you want to move to the new branch. You can use the git log command to view the commit history and find the commit hashes. Take note of the commit hash(es) of the recent commits you want to move.
4. Cherry-pick the commit(s) from the previous branch to the new branch using the git cherry-pick command:
git cherry-pick <commit-hash>
Replace
<commit-hash>
with the actual commit hash of the commit you want to move. You can repeat this command for each commit you want to move to the new branch.
For example, to cherry-pick a commit with the hash "abc123" to the current branch, you would run:
git cherry-pick abc123
If you have multiple commits to move, you can specify multiple commit hashes in a single command, separated by spaces.
5. After cherry-picking all the necessary commits, you can push the changes to the remote repository if needed using the git push command:
git push origin <new-branch-name>
This will create the new branch on the remote repository and push the commits to it.
Related Article:
How to Revert a Pushed Merge Commit in Git
Method 2: Using the git rebase command
1. Start by creating a new branch at the current commit, similar to Method 1.
2. Switch to the new branch using the git checkout command.
3. Run the following command to initiate an interactive rebase session:
git rebase -i <commit-hash>
Replace
<commit-hash>
with the commit hash of the commit just before the first commit you want to move. This will open a text editor with a list of commits.
4. In the text editor, find the line(s) corresponding to the commits you want to move and change the word "pick" to "edit" or "e" for each of those lines.
5. Save and close the text editor. Git will automatically stop at each commit you marked for editing.
6. For each commit you want to move, run the following command to amend the commit to the new branch:
git commit --amend
This will open a text editor where you can modify the commit message if needed. Save and close the text editor.
7. After amending the commit, you can continue the rebase process by running:
git rebase --continue
Git will apply the remaining commits and stop again if there are more commits to move.
8. Once the rebase is complete, you can push the changes to the remote repository if needed.
Best Practices and Considerations
Related Article:
How To Fix 'Updates Were Rejected' Error In Git
- Before moving commits to a new branch, it's important to ensure that you have a clean working directory and that there are no uncommitted changes. Use the git status command to check the status of your repository before starting the process.
- It's generally recommended to create a new branch at the current commit before attempting to move commits. This allows you to keep the original branch intact in case you need to revert any changes.
- When cherry-picking commits, be aware that the new branch will have different commit hashes compared to the original branch. This can cause conflicts if the original branch has already been merged into other branches. It's important to communicate and coordinate with other team members to avoid potential conflicts.
- If you're unsure about which method to use or want to experiment with moving commits without modifying your repository, you can create a new local repository and test the process there. This can help you gain confidence before applying the changes to your main repository.
- Remember to communicate any changes made to the commit history, especially if you're working in a team. Clear and concise commit messages can help others understand the changes and make collaboration smoother.
For more information on Git and its various commands, you can refer to the official Git documentation:
https://git-scm.com/doc
. |
| Markdown | [](https://www.squash.io/)
- [Product](https://www.squash.io/how-to-move-recent-commits-to-a-new-branch-with-git/ "Product")
- [How it works](https://www.squash.io/overview/ "How it works")
- [Core Features](https://www.squash.io/core-features/ "Core Features")
- [Build Environment](https://www.squash.io/build-environment/ "Build Environment")
- [Assets](https://www.squash.io/assets-deployment-data/ "Assets")
- [Microservices friendly](https://www.squash.io/microservices-friendly/ "Microservices friendly")
- [Automatic PR Comments](https://www.squash.io/automatic-pr-comments/ "Automatic PR Comments")
- [Auto Shutdown policies](https://www.squash.io/auto-shutdown-policies/ "Auto Shutdown policies")
- [Deployment Cache](https://www.squash.io/deployment-cache/ "Deployment Cache")
- [Persistent Storage](https://www.squash.io/persistent-storage/ "Persistent Storage")
- [Pricing](https://www.squash.io/pricing/ "Pricing")
- [Docs](https://www.squash.io/docs "Docs")
- [Resources](https://www.squash.io/how-to-move-recent-commits-to-a-new-branch-with-git/ "Resources")
- [Blog](https://www.squash.io/blog "Blog")
- [Tutorials](https://www.squash.io/tutorials "Tutorials")
- [Tutorial Series](https://www.squash.io/tutorial-series/ "Tutorial Series")
- [Demo](https://calendly.com/squash-labs/squash-demo "Demo")
- [Login](https://app.squash.io/accounts/)
- [Try it Free](https://www.squash.io/sign-up/)
# How to Move Recent Commits to a New Branch with Git

By squashlabs, Last Updated: Oct. 28, 2023

- [Method 1: Using the git branch and git cherry-pick commands](https://www.squash.io/how-to-move-recent-commits-to-a-new-branch-with-git/#method-1-using-the-git-branch-and-git-cherry-pick-commands)
- [Method 2: Using the git rebase command](https://www.squash.io/how-to-move-recent-commits-to-a-new-branch-with-git/#method-2-using-the-git-rebase-command)
- [Best Practices and Considerations](https://www.squash.io/how-to-move-recent-commits-to-a-new-branch-with-git/#best-practices-and-considerations)
Table of Contents
- [Method 1: Using the git branch and git cherry-pick commands](https://www.squash.io/how-to-move-recent-commits-to-a-new-branch-with-git/#method-1-using-the-git-branch-and-git-cherry-pick-commands "Method 1: Using the git branch and git cherry-pick commands")
- [Method 2: Using the git rebase command](https://www.squash.io/how-to-move-recent-commits-to-a-new-branch-with-git/#method-2-using-the-git-rebase-command "Method 2: Using the git rebase command")
- [Best Practices and Considerations](https://www.squash.io/how-to-move-recent-commits-to-a-new-branch-with-git/#best-practices-and-considerations "Best Practices and Considerations")
Moving recent commits to a new branch in Git can be done in a few simple steps. This process is particularly useful when you realize that certain commits should have been made on a separate branch instead of the current branch. It allows you to maintain a clean commit history and organize your code more effectively. Here are two possible ways to achieve this:
### Method 1: Using the git branch and git cherry-pick commands
1\. First, create a new branch at the current commit using the following command:
```
git branch <new-branch-name>
```
For example, if you want to create a new branch named "feature-branch" at the current commit, you would run:
```
git branch feature-branch
```
2\. Switch to the new branch using the git checkout command:
```
git checkout <new-branch-name>
```
For example, to switch to the "feature-branch", you would run:
```
git checkout feature-branch
```
3\. Identify the commit(s) you want to move to the new branch. You can use the git log command to view the commit history and find the commit hashes. Take note of the commit hash(es) of the recent commits you want to move.
4\. Cherry-pick the commit(s) from the previous branch to the new branch using the git cherry-pick command:
```
git cherry-pick <commit-hash>
```
Replace `<commit-hash>` with the actual commit hash of the commit you want to move. You can repeat this command for each commit you want to move to the new branch.
For example, to cherry-pick a commit with the hash "abc123" to the current branch, you would run:
```
git cherry-pick abc123
```
If you have multiple commits to move, you can specify multiple commit hashes in a single command, separated by spaces.
5\. After cherry-picking all the necessary commits, you can push the changes to the remote repository if needed using the git push command:
```
git push origin <new-branch-name>
```
This will create the new branch on the remote repository and push the commits to it.
Related Article: [How to Revert a Pushed Merge Commit in Git](https://www.squash.io/how-to-revert-a-pushed-merge-commit-in-git/)
### Method 2: Using the git rebase command
1\. Start by creating a new branch at the current commit, similar to Method 1.
2\. Switch to the new branch using the git checkout command.
3\. Run the following command to initiate an interactive rebase session:
```
git rebase -i <commit-hash>
```
Replace `<commit-hash>` with the commit hash of the commit just before the first commit you want to move. This will open a text editor with a list of commits.
4\. In the text editor, find the line(s) corresponding to the commits you want to move and change the word "pick" to "edit" or "e" for each of those lines.
5\. Save and close the text editor. Git will automatically stop at each commit you marked for editing.
6\. For each commit you want to move, run the following command to amend the commit to the new branch:
```
git commit --amend
```
This will open a text editor where you can modify the commit message if needed. Save and close the text editor.
7\. After amending the commit, you can continue the rebase process by running:
```
git rebase --continue
```
Git will apply the remaining commits and stop again if there are more commits to move.
8\. Once the rebase is complete, you can push the changes to the remote repository if needed.
### Best Practices and Considerations
Related Article: [How To Fix 'Updates Were Rejected' Error In Git](https://www.squash.io/how-to-fix-updates-were-rejected-error-in-git/)
\- Before moving commits to a new branch, it's important to ensure that you have a clean working directory and that there are no uncommitted changes. Use the git status command to check the status of your repository before starting the process.
\- It's generally recommended to create a new branch at the current commit before attempting to move commits. This allows you to keep the original branch intact in case you need to revert any changes.
\- When cherry-picking commits, be aware that the new branch will have different commit hashes compared to the original branch. This can cause conflicts if the original branch has already been merged into other branches. It's important to communicate and coordinate with other team members to avoid potential conflicts.
\- If you're unsure about which method to use or want to experiment with moving commits without modifying your repository, you can create a new local repository and test the process there. This can help you gain confidence before applying the changes to your main repository.
\- Remember to communicate any changes made to the commit history, especially if you're working in a team. Clear and concise commit messages can help others understand the changes and make collaboration smoother.
For more information on Git and its various commands, you can refer to the official Git documentation: <https://git-scm.com/doc>.
### More Articles from the [Git Tutorial: From Basics to Advanced Concepts](https://www.squash.io/git-tutorial-from-basics-to-advanced-concepts/) series:
#### [How to Remove a File From a Git Repository](https://www.squash.io/how-to-remove-a-file-from-a-git-repository/)
Deleting files from a Git repository is a common task for software developers. This article provides two methods for removing files: using the git rm… [read more](https://www.squash.io/how-to-remove-a-file-from-a-git-repository/)
#### [How To Delete A Commit From A Branch](https://www.squash.io/how-to-delete-a-commit-from-a-branch/)
Deleting a commit from a branch in Git can be done using simple steps and commands. There are two methods you can use: git revert and git reset. But … [read more](https://www.squash.io/how-to-delete-a-commit-from-a-branch/)
#### [How to Update Branches in Git using Git Force Pull and Git Pull](https://www.squash.io/how-to-update-branches-in-git-using-git-force-pull-and-git-pull/)
Updating branches in Git is essential for staying up-to-date with the latest changes in your codebase. This article will teach you how to update bran… [read more](https://www.squash.io/how-to-update-branches-in-git-using-git-force-pull-and-git-pull/)
#### [How To Fix 'Could Not Open A Connection To Your Authentication Agent' In Git](https://www.squash.io/how-to-fix-could-not-open-a-connection-to-your-authentication-agent-in-git/)
Learn how to troubleshoot and resolve the 'Could Not Open a Connection to Your Authentication Agent' error in Git with simple steps. Discover possibl… [read more](https://www.squash.io/how-to-fix-could-not-open-a-connection-to-your-authentication-agent-in-git/)
#### [How to Fix a Git Detached Head](https://www.squash.io/how-to-fix-a-git-detached-head/)
Solving the issue of a Git detached head in your repository can be a simple task. This article provides a guide with two methods to fix the problem. … [read more](https://www.squash.io/how-to-fix-a-git-detached-head/)
#### [How To Push And Track A Local Branch In Git](https://www.squash.io/how-to-push-and-track-a-local-branch-in-git/)
Learn how to push a new local branch to a remote Git repository and track it for seamless collaboration. This step-by-step guide will walk you throug… [read more](https://www.squash.io/how-to-push-and-track-a-local-branch-in-git/)
#### [How To Handle Git Refusing To Merge Unrelated Histories On Rebase](https://www.squash.io/how-to-handle-git-refusing-to-merge-unrelated-histories-on-rebase/)
Git refusing to merge unrelated histories on rebase can be a frustrating issue to encounter. This article provides possible answers and suggestions t… [read more](https://www.squash.io/how-to-handle-git-refusing-to-merge-unrelated-histories-on-rebase/)
#### [How to Delete a Remote Tag in Git](https://www.squash.io/how-to-delete-a-remote-tag-in-git/)
Git is a powerful version control system used by software engineers to manage code repositories. This article provides a guide on how to delete a rem… [read more](https://www.squash.io/how-to-delete-a-remote-tag-in-git/)
#### [How to Delete a Git Branch Locally and Remotely](https://www.squash.io/how-to-delete-a-git-branch-locally-and-remotely/)
Deleting a Git branch both locally and remotely is essential for maintaining a clean and organized Git repository. This article provides simple steps… [read more](https://www.squash.io/how-to-delete-a-git-branch-locally-and-remotely/)
#### [How to Rename Both Local and Remote Git Branch Names](https://www.squash.io/how-to-rename-both-local-and-remote-git-branch-names/)
Renaming Git branch names locally and remotely can be done with ease using a few simple methods. This guide provides step-by-step instructions on how… [read more](https://www.squash.io/how-to-rename-both-local-and-remote-git-branch-names/)
[JavaScript Tutorial & Cheat Sheet](https://www.squash.io/javascript-tutorial-cheat-sheet-the-ultimate-guide/)
[Mastering Microservices Guide](https://www.squash.io/mastering-microservices-a-comprehensive-guide-to-building-scalable-and-agile-applications/)
[Javascript Tutorials](https://www.squash.io/the-big-javascript-guide-from-basics-to-advanced-concepts/)
[Python Tutorials](https://www.squash.io/python-tutorial-from-basics-to-advanced-concepts/)
[How to Release Software Daily](https://www.squash.io/how-to-release-software-to-production-all-day-every-day/)
[Java Tutorials](https://www.squash.io/how-to-write-java-code-from-basics-to-advanced-concepts/)
[Shell Scripting Tutorials](https://www.squash.io/the-shell-scripting-guide-from-basics-to-advanced-concepts/)
[Git Tutorials](https://www.squash.io/git-tutorial-from-basics-to-advanced-concepts/)
[](https://www.squash.io/)
**Quick Links**
- [How it works](https://www.squash.io/#service)
- [Pricing](https://www.squash.io/pricing)
- [Blog](https://www.squash.io/blog)
- [Tutorials](https://www.squash.io/tutorials)
- [Tutorial Series](https://www.squash.io/tutorial-series/)
**Help**
- [FAQ](https://www.squash.io/docs-category/faq/)
- [Docs](https://www.squash.io/docs)
- [Support](https://www.squash.io/support/)
- [Chat]()
**Contact**
Mailto:
[support@squash.io](mailto:support@squash.io)

Twitter:
<https://twitter.com/LabsSquash>
Location:
Squash Labs Inc. 1600 - 925 West Georgia Street, Vancouver, BC V6C 3L2, Canada.
Copyright © 2024 Squash Labs Inc. All rights reserved.
- [Security](https://www.squash.io/security)
- [Terms](https://www.squash.io/terms)
- [Privacy](https://www.squash.io/privacy) |
| Readable Markdown | null |
| Shard | 122 (laksa) |
| Root Hash | 15969246546098136722 |
| Unparsed URL | io,squash!www,/how-to-move-recent-commits-to-a-new-branch-with-git/ s443 |