Move the most recent commits to a new branch with git

⋅ 2 min read ⋅ Git Workflow

Table of Contents

Sometimes I forget to create a new branch for a new feature and commit changes directly to the develop branch.

When I realize my mistake, I usually end up with some commits in the develop branch.

Result:

develop: A - B - C - Feature D.1 - Feature D.2

What I want:

feature/D:           Feature D.1 - Feature D.2
/
develop: A - B - C

I will share how I move my recent commits on develop to a new branchand remove those commits from the develop branch.

There are two steps to accomplish this.

  1. Create a new feature branch with those commits.
  2. Remove the commits from the develop branch.

Create a new feature branch

First, we move the new commits to a new branch using git branch <new_branch>.

This command will create a new branch from the current branch. So, our new branch will contain everything from the develop branch.

git branch feature/D

As a result, you will get a new feature branch with the commits you want.

develop:   A - B - C - Feature D.1 - Feature D.2

feature/D: A - B - C - Feature D.1 - Feature D.2

You can easily support sarunw.com by checking out this sponsor.

Sponsor sarunw.com and reach thousands of iOS developers.

Remove feature commits from the develop branch

To remove wrong commits from the develop branch, we use git reset. The git reset is a command for undoing changes.

First, make sure you are on the branch where you want to remove the commits.

In this case, it is the develop branch.

git checkout develop

Then you remove unwanted commits with git reset --hard HEAD~n, where n is the number of commits you want to remove.

In this example, we want to remove two commits (Feature D.1 and Feature D.2), so we use HEAD~2.

git reset --hard HEAD~2

After this command, your git tree would look like this.

develop:   A - B - C

feature/D: A - B - C - Feature D.1 - Feature D.2

That's all you need to do. Now all new commits are in their feature branch, ready for you to push for review (and merge back to develop 😅).

You can easily support sarunw.com by checking out this sponsor.

Sponsor sarunw.com and reach thousands of iOS developers.

Conclusion

There are many ways to do the same thing in git. The way I show you in this article is just one way to do it.

And since it contains git reset --hard, any changes not committed will be lost. Please ensure you understand what you are doing before running the command (This apply to all git command you find on the internet).


Read more article about Git, Workflow, or see all available topic

Enjoy the read?

If you enjoy this article, you can subscribe to the weekly newsletter.
Every Friday, you'll get a quick recap of all articles and tips posted on this site. No strings attached. Unsubscribe anytime.

Feel free to follow me on Twitter and ask your questions related to this post. Thanks for reading and see you next time.

If you enjoy my writing, please check out my Patreon https://www.patreon.com/sarunw and become my supporter. Sharing the article is also greatly appreciated.

Become a patron Buy me a coffee Tweet Share
Previous
Variable Color in SF Symbols 4

Variable Color is a new feature of SF Symbols that allows you to change the appearance of a symbol based on a percentage value. Let's learn what it is and how to use it.

Next
Improve numbers readability with underscores

A tip to prevent error and improve readability when dealing with numbers.

← Home