8

Move your latest commits to a separate branch

 3 years ago
source link: https://www.devroom.io/2012/08/14/move-your-latest-commits-to-a-separate-branch/?utm_campaign=Feed%3A+ariejan+%28ariejan%7Cdevroom.io%29
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.

Move your latest commits to a separate branch

Posted: 2012-08-14 - Last updated: 2019-06-05

Tagged git protip

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:

A - B - (C) - D - E - F

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:

A - B - (C)
           \ D - E  F

Step 1: Assuming you’re at F on master, create a new branch with those commits:

git branch my_feature_branch

Then, still on master, reset back to commit C. This is 3 commits back.

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.

My site is free of ads and trackers. I record privacy-respecting usage statistics with Fathom.

Was this post helpful to you? Why not ☕ Buy me a coffee

Copyright © 1999-2020 Ariejan de Vroom <[email protected]>

Live now; make now always the most precious time. Now will never come again.
    – Jean-Luc Picard


About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK