The Wiert Corner – irregular stream of stuff

Jeroen W. Pluimers on .NET, C#, Delphi, databases, and personal interests

  • My badges

  • Twitter Updates

  • My Flickr Stream

  • Pages

  • All categories

  • Enter your email address to subscribe to this blog and receive notifications of new posts by email.

    Join 1,509 other followers

Move the most recent commit(s) to a new branch with Git – Stack Overflow

Posted by jpluimers on 2019/03/05

Below are the git statements I used to solve this ASCII art problem from me (as I work in Git Flow feature branches):

old situation:

commit-1..4  -  commit-5  -  commit-6  -  commit-7  -  commit-8  -  commit-9
                   ^            ^            ^            ^           ^
                   |            |            |            |           |
                master       develop      feature/A                 feature/old

to:

commit-1..4  -  commit-5  -  commit-6  -  commit-7  -  commit-8  -  commit-9
                   ^            ^            ^            ^           ^
                   |            |            |            |           |
                master       develop      feature/A    feature/old  feature/new

 

git branch
git rev-parse HEAD
git log --pretty=format:'%H' -n 2
git checkout -b feature/new hash-of-commit-8
git branch --set-upstream-to=feature/old
git cherry-pick ..feature/old
git branch --force feature/old hash-of-commit-8

Step by step, this is what happens:

  1. branch lists the current branches
  2. rev-parse HEAD shows the hash of the current commit (commit-9)
  3. log --pretty=format:'%H' -n 1shows the hash of the previous two commits (from top to bottom: commit-9 and commit-8)
  4. checkout creates a new branch based on the past commit-8
  5. branch --set-upstream ensures the new branch tracks the old branch
  6. cherry pick ensures the new branch gets all the commits from the old branch
  7. branch --force ensure the old branch looses the extra commits you wanted to only be in newBranchName

Based on

–jeroen

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

 
%d bloggers like this: