Excellent question! This is a very common situation in Git, and there's a straightforward way to fix it.
The solution depends on one crucial question: Have you pushed these changes to the remote repository (like GitHub, GitLab, etc.) yet?
I'll cover both scenarios.
TL;DR (The Quick Commands)
Scenario 1: You have NOT pushed to the remote. (This is the easy, clean fix)
# 1. Create the new 'dev' branch from master's current state
git branch dev
# 2. Reset the master branch back to where it was before your changes
# (This assumes 'origin/master' is the clean state you want)
git reset --hard origin/master
# 3. Switch to your new dev branch to continue your work
git checkout dev
Scenario 2: You HAVE pushed to the remote. (This is more delicate. Option A is safer.)
- Option A (Safer - Revert):
# 1. On master, create "revert" commits to undo your changes
git revert <hash-of-commit-to-revert>
# 2. Push the revert commit to fix the remote master
git push origin master
# 3. Create your dev branch and cherry-pick the original commits onto it
git checkout -b dev
git cherry-pick <hash-of-original-commit>
- Option B (Dangerous - Force Push): Use this ONLY if you are the only person working on this repo or have coordinated with your team.
# Do the same steps as the "NOT pushed" scenario...
git branch dev
git reset --hard origin/master
# ...then force push to overwrite the remote master.
# --force-with-lease is safer than a plain --force.
git push --force-with-lease origin master
# Finally, switch to your new branch
git checkout dev
Detailed Explanation
Let's break down exactly what's happening.
Imagine your commit history looks like this, where C and D are the commits you mistakenly made on master.
A---B---C---D <- master, HEAD, origin/master
Your goal is to have this:
A---B <- master, origin/master
\
C---D <- dev
Scenario 1: You Have NOT Pushed the Changes
This is the ideal situation because you're only rewriting your own local history.
Step 1: Create a new branch to "save" your work.
Your current master branch has the commits you want to keep. We can simply create a new branch pointer right where you are.
# This creates a new branch called 'dev' pointing to commit 'D'
git branch dev
Now your history looks like this. Both master and dev point to the same commit.
A---B---C---D <- master, dev
Step 2: Reset your local master branch.
Now that your work is safely stored on the dev branch, you can reset master back to its correct state. The easiest way to find that state is the origin/master branch, which reflects the remote repository you haven't touched yet.
# First, make sure you are on the master branch
git checkout master
# Now, reset it. The --hard flag discards the changes from your working directory.
git reset --hard origin/master
- What if I don't have a remote? You can find the commit hash of
B by running git log and then use git reset --hard <commit-hash-of-B>. You could also do git reset --hard HEAD~2 if you made 2 unwanted commits.
Now your history is exactly what you wanted!
A---B <- master
\
C---D <- dev
Step 3: Switch to your new branch.
Your master branch is clean, and your work is on dev. Just check it out to continue.
git checkout dev
Scenario 2: You HAVE Pushed the Changes
This is more sensitive because you've published the "mistake" to a shared history. Simply resetting and force-pushing (the "dangerous" option) can cause major problems for any collaborators who have already pulled your changes.
Option A: The Safe Way (Using git revert)
This is the recommended approach for shared branches. Instead of pretending the commits never happened, you create new commits that explicitly undo them.
- Stay on
master. Find the commit hashes of the changes you want to undo (git log).
- Revert the commits. You should revert them in the reverse order they were added.
# This creates a new commit 'D-revert' that undoes the changes from 'D'
git revert D
# This creates a new commit 'C-revert' that undoes the changes from 'C'
git revert C
Your history on master now looks like this:
A---B---C---D---D'---C' <- master
- Push the revert commits.
git push origin master
The master branch is now fixed and the history is clean and logical for everyone.
- Create your
dev branch and get your work back. Now you need to get commits C and D onto a new dev branch. You can do this with cherry-pick.
# Create dev branch from the correct starting point (B)
git checkout -b dev <commit-hash-of-B>
# Now, apply the original commits to it
git cherry-pick <commit-hash-of-C>
git cherry-pick <commit-hash-of-D>
Option B: The Dangerous Way (Using git push --force-with-lease)
Warning: Only do this if you are absolutely sure nobody else has pulled from master or you've told your team what you're doing. This rewrites the public history.
If you're in a solo project or have confirmed with your team, you can essentially perform the same steps as Scenario 1 and then force the remote to accept your rewritten history.
- Follow all the steps from Scenario 1 (
git branch dev, git reset --hard origin/master).
- Now, instead of a regular
push (which will fail), you must force push. Using --force-with-lease is safer than --force because it checks if someone else has pushed to the branch in the meantime.
git push --force-with-lease origin master
This will overwrite master on the remote, effectively erasing C and D from the public master branch history.