Home Interests Git

Git: Move Your Latest Commits to Another Branch

howchoo   (435)
August 22, 2023
7 minutes


Posted in these interests:


git • 5 guides

Sometimes we start writing code without managing our branches in advance. Then we make commits in master (or some other base branch) when we intended to carve off a new feature branch. In this guide, we’ll learn how to move our latest commits to another branch, leaving our original branch unchanged.

If you’ve never used these tools before, visit our guide on how to commit and push in Git. You may also find our guide on how to check out a remote branch useful.

1 – Determine how many commits to move

Before we do anything, we need to assess the situation. Assuming you have checked out the branch that we’re going to modify, we need to see the history.

Use git log to deteremine how many commits to move.

git log

You’ll see output similar to this:

commit 5576dbf62182ac1d263e9777e31ff7f35ac6eee3 (HEAD -> master)
Author: Tyler <[email protected]>
Date:   Fri Nov 8 12:04:42 2019 -0800

    Another commit to move

commit edec499e8c85adf8c6fd79bc1b6993bfb233a5a0
Author: Tyler <[email protected]>
Date:   Fri Nov 8 12:04:29 2019 -0800

    One commit to move

commit 896cfcd0ae55d95fa81915a60460948b40fa55fa (origin/master, origin/HEAD)
Author: Zach Levine <[email protected]>
Date:   Thu Nov 7 10:40:13 2019 -0500

    Awesome code added to the repository.

We can see that that HEAD is two commits ahead of origin/HEAD, and these are the two commits we want to move to another branch.

In the remaining steps, we’ll cover to how move these commits to a new branch or an existing branch.

2 – Move commits to a new branch

The following steps will show you how to move your latest commits to a new branch.

Create a new branch

git branch feature/newbranch

This will create a new branch including all of the commits of the current branch.

Move the current branch back two commits

git reset --keep HEAD~2

Checkout the new branch

git checkout feature/newbranch

That’s it! Your two latest commits have been removed from master (or your current branch), and they’ve been included in a new branch called feature/newbranch.

3 – Move commits to an existing branch

The following steps will show you how to move your latest commits to an existing branch. This is useful if you’ve been working out of a feature branch, but accidentally started making commits in the wrong branch.

We’ll assume that the “current” branch, with the commits that need to be removed, is master.

Check out the existing branch

git checkout feature/existingbranch

Merge master

git merge master

This will add the additional commits to the existing branch.

Checkout master

git checkout master

Move the current branch back two commits

git reset --keep HEAD~2

This is it! The latest two commits have been removed from master and added to an existing branch.


How to Set up Git Tab Completion

howchoo   (435)
November 26, 2023

Tab completion, or auto-completion, is essential if you’re using Git on the command line. Tab completion is a nice feature of many shells that allows you to complete a word by hitting tab. In this case, we want to be able to use tab completion for things like branches and tags in git. Fortunately, setting

Continue Reading


 435 guides

Introducing Howchoo, an enigmatic author whose unique pen name reflects their boundless curiosity and limitless creativity. Mysterious and multifaceted, Howchoo has emerged as a captivating storyteller, leaving readers mesmerized by the uncharted realms they craft with their words. With an insatiable appetite for knowledge and a love for exploration, Howchoo's writing transcends conventional genres, blurring the lines between fantasy, science fiction, and the surreal. Their narratives are a kaleidoscope of ideas, weaving together intricate plots, unforgettable characters, and thought-provoking themes that challenge the boundaries of imagination.

Discover interesting things!

Explore Howchoo's most popular interests.