How to Move a Commit to Another Branch in Git

The admin panel that you'll actually want to use. Try for free.

February 15, 2023

Moving a commit to another branch in Git is pretty straightforward. You’ll usually want to do it to tidy up your project’s history, for e.g. if you accidentally committed to the wrong branch. This post covers the techniques you’d use to move Git commits so read on if you want to keep your projects more organized.

How to create and switch to the new branch?

If the target branch doesn’t exist, you first need to create it from the current state of the main branch. If the branch already exists and you simply want to add the commit to it, proceed to checking out the target branch.

git branch feature-x

How to check out a target branch in Git?

Switch to your intended branch, feature-x, with the following command:

git checkout feature-x

How to cherry-pick a commit in Git?

Find the commit you wish to move by using git log to get its hash. With the commit hash in hand, let's say abc1234, cherry-pick it onto your current branch (feature-x) using:

git cherry-pick abc1234

You could ship faster.

Imagine the time you'd save if you never had to build another internal tool, write a SQL report, or manage another admin panel again. Basedash is built by internal tool builders, for internal tool builders. Our mission is to change the way developers work, so you can focus on building your product.

How to move back to the original branch in Git?

Return to the main branch by running:

git checkout main

How to revert a commit on the original branch in Git?

You’ll want to be sure that removing this commit from the main branch doesn't disrupt your branch's history. If you've pushed the commit, consider using git revert to avoid issues with shared history.

To remove the commit locally without pushing, you can:

git reset --hard HEAD~1

This command discards the last commit on main, so use it with caution to avoid losing work.

For commits that have been shared or if you need to maintain a clean history for collaborators, revert the commit using:

git revert abc1234

This action creates a new commit on main that undoes the changes made by abc1234.

Push changes if necessary

If your commits have been shared remotely, and you've used git revert, then push your updates:

git push origin main git push origin feature-x

In cases where you've used git reset and are sure it won't affect others, you might consider a force push, though this is generally advised against:

git push origin main --force

TOC

How to create and switch to the new branch?
How to check out a target branch in Git?
How to cherry-pick a commit in Git?
How to move back to the original branch in Git?
How to revert a commit on the original branch in Git?
Push changes if necessary

February 15, 2023

Moving a commit to another branch in Git is pretty straightforward. You’ll usually want to do it to tidy up your project’s history, for e.g. if you accidentally committed to the wrong branch. This post covers the techniques you’d use to move Git commits so read on if you want to keep your projects more organized.

How to create and switch to the new branch?

If the target branch doesn’t exist, you first need to create it from the current state of the main branch. If the branch already exists and you simply want to add the commit to it, proceed to checking out the target branch.

git branch feature-x

How to check out a target branch in Git?

Switch to your intended branch, feature-x, with the following command:

git checkout feature-x

How to cherry-pick a commit in Git?

Find the commit you wish to move by using git log to get its hash. With the commit hash in hand, let's say abc1234, cherry-pick it onto your current branch (feature-x) using:

git cherry-pick abc1234

You could ship faster.

Imagine the time you'd save if you never had to build another internal tool, write a SQL report, or manage another admin panel again. Basedash is built by internal tool builders, for internal tool builders. Our mission is to change the way developers work, so you can focus on building your product.

How to move back to the original branch in Git?

Return to the main branch by running:

git checkout main

How to revert a commit on the original branch in Git?

You’ll want to be sure that removing this commit from the main branch doesn't disrupt your branch's history. If you've pushed the commit, consider using git revert to avoid issues with shared history.

To remove the commit locally without pushing, you can:

git reset --hard HEAD~1

This command discards the last commit on main, so use it with caution to avoid losing work.

For commits that have been shared or if you need to maintain a clean history for collaborators, revert the commit using:

git revert abc1234

This action creates a new commit on main that undoes the changes made by abc1234.

Push changes if necessary

If your commits have been shared remotely, and you've used git revert, then push your updates:

git push origin main git push origin feature-x

In cases where you've used git reset and are sure it won't affect others, you might consider a force push, though this is generally advised against:

git push origin main --force

February 15, 2023

Moving a commit to another branch in Git is pretty straightforward. You’ll usually want to do it to tidy up your project’s history, for e.g. if you accidentally committed to the wrong branch. This post covers the techniques you’d use to move Git commits so read on if you want to keep your projects more organized.

How to create and switch to the new branch?

If the target branch doesn’t exist, you first need to create it from the current state of the main branch. If the branch already exists and you simply want to add the commit to it, proceed to checking out the target branch.

git branch feature-x

How to check out a target branch in Git?

Switch to your intended branch, feature-x, with the following command:

git checkout feature-x

How to cherry-pick a commit in Git?

Find the commit you wish to move by using git log to get its hash. With the commit hash in hand, let's say abc1234, cherry-pick it onto your current branch (feature-x) using:

git cherry-pick abc1234

You could ship faster.

Imagine the time you'd save if you never had to build another internal tool, write a SQL report, or manage another admin panel again. Basedash is built by internal tool builders, for internal tool builders. Our mission is to change the way developers work, so you can focus on building your product.

How to move back to the original branch in Git?

Return to the main branch by running:

git checkout main

How to revert a commit on the original branch in Git?

You’ll want to be sure that removing this commit from the main branch doesn't disrupt your branch's history. If you've pushed the commit, consider using git revert to avoid issues with shared history.

To remove the commit locally without pushing, you can:

git reset --hard HEAD~1

This command discards the last commit on main, so use it with caution to avoid losing work.

For commits that have been shared or if you need to maintain a clean history for collaborators, revert the commit using:

git revert abc1234

This action creates a new commit on main that undoes the changes made by abc1234.

Push changes if necessary

If your commits have been shared remotely, and you've used git revert, then push your updates:

git push origin main git push origin feature-x

In cases where you've used git reset and are sure it won't affect others, you might consider a force push, though this is generally advised against:

git push origin main --force

What is Basedash?

What is Basedash?

What is Basedash?

Ship faster, worry less with Basedash

Ship faster, worry less with Basedash

Ship faster, worry less with Basedash

You're busy enough with product work to be weighed down building, maintaining, scoping and developing internal apps and admin panels. Forget all of that, and give your team the admin panel that you don't have to build. Launch in less time than it takes to run a standup.

You're busy enough with product work to be weighed down building, maintaining, scoping and developing internal apps and admin panels. Forget all of that, and give your team the admin panel that you don't have to build. Launch in less time than it takes to run a standup.

You're busy enough with product work to be weighed down building, maintaining, scoping and developing internal apps and admin panels. Forget all of that, and give your team the admin panel that you don't have to build. Launch in less time than it takes to run a standup.

Dashboards and charts

Edit data, create records, oversee how your product is running without the need to build or manage custom software.

USER CRM

ADMIN PANEL

SQL COMPOSER WITH AI

Screenshot of a users table in a database. The interface is very data-dense with information.