devroom.io/content/posts/2012-08-14-move-your-latest-commits-to-a-separate-branch.md

36 lines
1.0 KiB
Markdown
Raw Normal View History

2017-03-20 15:35:19 +00:00
+++
date = "2012-08-14"
title = "Move your latest commits to a separate branch"
tags = ["git", "protip"]
slug = "move-your-latest-commits-to-a-separate-branch"
+++
2015-03-26 11:28:08 +00:00
The situation is pretty straightforward. You have been making commits for that new feature in your `master` branch. Naughty you!
Let's assume you want to have this:
2017-03-20 15:35:19 +00:00
``` text
A - B - (C) - D - E - F
```
2015-03-26 11:28:08 +00:00
`C` was the last commit you pulled from `origin` and D, E and F are commits you just made but should have been in their own branch. This is what you wanted:
2017-03-20 15:35:19 +00:00
``` text
A - B - (C)
\ D - E F
```
2015-03-26 11:28:08 +00:00
Step 1: Assuming you're at `F` on `master`, create a new branch with those commits:
2017-03-20 15:35:19 +00:00
``` shell
git branch my_feature_branch
```
2015-03-26 11:28:08 +00:00
Then, still on `master`, reset back to commit `C`. This is 3 commits back.
2017-03-20 15:35:19 +00:00
``` shell
git reset --hard HEAD~3
```
Okay, you're `master` is now back at `C`, which you lasted pulled, and the `my_feature_branch` includes D, E and F. Just checkout `my_feature_branch` and continue work as usual. I'm sure no one saw what you just did.
2015-03-26 11:28:08 +00:00