Lineserve

How to Move Recent Git Commits to a New Branch: Step-by-Step Tutorial for Developers

Lineserve TeamLineserve Team
·
5 min read

Ever found yourself in a situation where you’ve made several commits on your master branch that really belong on a separate feature branch? Maybe you started developing a new feature directly on master, and now you want to keep your main branch clean for releases. Don’t worry, Git makes it straightforward to move those recent commits to a new branch and reset master back to an earlier state. In this tutorial, we’ll walk through the exact steps to achieve this, using the scenario from your example where commits C, D, and E need to move to a new branch, leaving master at commit B. By the end, you’ll have a solid understanding of branching and resetting in Git, ensuring your repository history stays organized and manageable.

Understanding the Scenario

Let’s clarify what’s happening here. You have a master branch with commits A, B, C, D, and E, where A and B are your stable code, and C, D, E are recent changes (perhaps a new feature). The goal is to create a new branch called ‘newbranch’ that contains C, D, and E, while resetting master to just A and B. This keeps master clean and allows you to merge the feature branch later when it’s ready.

This is a common use case for feature development gone astray on the wrong branch. By moving commits, you preserve history without losing work, and it aligns with Git best practices for branching.

Prerequisites

  • Basic familiarity with Git commands like git log, git status, and git checkout.
  • A Git repository with the described commit history.
  • Ensure you’re not in the middle of uncommitted changes—run git status to check.

Step-by-Step Guide

We’ll use Git commands to create the new branch, reset master, and verify everything. Let’s dive in.

Step 1: Identify the Commits to Move

First, inspect your commit history to pinpoint where master should be reset. Use git log --oneline to see a concise history.

$ git log --oneline
E (HEAD -> master) Latest commit message
D Second commit
C Third commit
B Fourth commit
A Initial commit

In this output, A is the initial commit, B is where you want master to be, and C, D, E are the recent ones to move. Note the commit hashes (e.g., the short SHA for B).

Step 2: Create a New Branch from Current HEAD

With master at E, create a new branch pointing to the same commit. This captures C, D, and E on the new branch.

$ git checkout -b newbranch
Switched to a new branch 'newbranch'

Now, newbranch is created and checked out, mirroring master’s state with commits A-B-C-D-E.

Step 3: Switch Back to Master and Reset It

Switch to master.

$ git checkout master
Switched to branch 'master'

Reset master to commit B (replace with actual hash).

$ git reset --hard <commit-hash-of-B>
HEAD is now at <commit-hash-of-B> Fourth commit

The --hard flag removes the commits from the working directory and index, effectively reverting master to B.

Step 4: Verify the Changes

Check master’s log to confirm it’s at B.

$ git log --oneline
B (HEAD -> master) Fourth commit
A Initial commit

Check newbranch’s log.

$ git checkout newbranch
$ git log --oneline
E (HEAD -> newbranch) Latest commit message
D Second commit
C Third commit
B Fourth commit
A Initial commit

Use git branch -v to see branches and their latest commits.

$ git branch -v
* newbranch E Latest commit message
  master     B Fourth commit

Practical Examples and Use Cases

Imagine you’re working on a web app: A is the initial setup, B adds user authentication. Then you add C (new dashboard), D (API integration), E (bug fixes). By moving C-D-E to a ‘dashboard-feature’ branch, master stays stable for hotfixes.

Another case: In a team, someone committed experimental code on master. Move it to a branch for review, preventing deployment issues.

Tips, Best Practices, and Common Pitfalls

  • Always backup or push first: If working remotely, push master before resetting to avoid losing shared history.
  • Use descriptive branch names: Instead of ‘newbranch’, use ‘feature-dashboard’ for clarity.
  • Soft vs. hard reset: --hard discards changes; use --soft if you want to keep them staged.
  • Check for uncommitted changes: Resetting with uncommitted work will lose it—stash with git stash if needed.
  • Push the new branch: After creating, push it to share: git push -u origin newbranch.
  • Avoid force-pushing master: If master is shared, inform collaborators; they may need to rebase.

Pitfall to avoid: Forgetting the commit hash—double-check with git log. If you reset to the wrong point, you can undo with git reflog.

Summary and Next Steps

In summary, moving recent commits to a new branch involves creating the branch from HEAD, switching back to master, and resetting it to an earlier commit. This keeps your history clean and follows Git’s branching model. You’ve now learned to create branches to capture commits, use git reset safely, and verify with git log and git status.

Next, practice this in a test repo. Explore advanced Git features like rebase for history rewriting or interactive rebase to squash commits. If you’re collaborating, read up on merge strategies to integrate your new branch back into master seamlessly. Happy coding!

Share:
Lineserve Team

Written by Lineserve Team

Related Posts

Lineserve

AI autonomous coding Limitation Gaps

Let me show you what people in the industry are actually saying about the gaps. The research paints a fascinating and sometimes contradictory picture: The Major Gaps People Are Identifying 1. The Productivity Paradox This is the most striking finding: experienced developers actually took 19% longer to complete tasks when using AI tools, despite expecting [&hellip;]

Stephen Ndegwa
·

How to Disable Email Sending in WordPress

WordPress sends emails for various events—user registrations, password resets, comment notifications, and more. While these emails are useful in production environments, there are scenarios where you might want to disable email sending entirely, such as during development, testing, or when migrating sites. This comprehensive guide covers multiple methods to disable WordPress email functionality, ranging from [&hellip;]

Stephen Ndegwa
·

How to Convert Windows Server Evaluation to Standard or Datacenter (2019, 2022, 2025)

This guide explains the correct and Microsoft-supported way to convert Windows Server Evaluation editions to Standard or Datacenter for Windows Server 2019, 2022, and 2025. It is written for: No retail or MAK keys are required for the conversion step. 1. Why Evaluation Conversion Fails for Many Users Common mistakes: Important rule: Evaluation → Full [&hellip;]

Stephen Ndegwa
·