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

前端 未结 14 1212
梦毁少年i
梦毁少年i 2020-11-22 03:28

I\'d like to move the last several commits I\'ve committed to master to a new branch and take master back to before those commits were made. Unfortunately, my Git-fu is not

相关标签:
14条回答
  • 2020-11-22 03:53

    1) Create a new branch, which moves all your changes to new_branch.

    git checkout -b new_branch
    

    2) Then go back to old branch.

    git checkout master
    

    3) Do git rebase

    git rebase -i <short-hash-of-B-commit>
    

    4) Then the opened editor contains last 3 commit information.

    ...
    pick <C's hash> C
    pick <D's hash> D
    pick <E's hash> E
    ...
    

    5) Change pick to drop in all those 3 commits. Then save and close the editor.

    ...
    drop <C's hash> C
    drop <D's hash> D
    drop <E's hash> E
    ...
    

    6) Now last 3 commits are removed from current branch (master). Now push the branch forcefully, with + sign before branch name.

    git push origin +master
    
    0 讨论(0)
  • 2020-11-22 03:54

    If you just need to move all your unpushed commits to a new branch, then you just need to,

    1. create a new branch from the current one :git branch new-branch-name

    2. push your new branch: git push origin new-branch-name

    3. revert your old(current) branch to the last pushed/stable state: git reset --hard origin/old-branch-name

    Some people also have other upstreams rather than origin, they should use appropriate upstream

    0 讨论(0)
  • 2020-11-22 03:54

    You can do this is just 3 simple step that i used.

    1) make new branch where you want to commit you recent update.

    git branch <branch name>

    2) Find Recent Commit Id for commit on new branch.

    git log

    3) Copy that commit id note that Most Recent commit list take place on top. so you can find your commit. you also find this via message.

    git cherry-pick d34bcef232f6c...

    you can also provide some rang of commit id.

    git cherry-pick d34bcef...86d2aec

    Now your job done. If you picked correct id and correct branch then you will success. So before do this be careful. else another problem can occur.

    Now you can push your code

    git push

    0 讨论(0)
  • 2020-11-22 03:55

    How can I go from this

    A - B - C - D - E 
                    |
                    master
    

    to this?

    A - B - C - D - E 
        |           |
        master      newbranch
    

    With two commands

    • git branch -m master newbranch

    giving

    A - B - C - D - E 
                    |
                    newbranch
    

    and

    • git branch master B

    giving

    A - B - C - D - E
        |           |
        master      newbranch
    
    0 讨论(0)
  • 2020-11-22 03:57

    This doesn't "move" them in the technical sense but it has the same effect:

    A--B--C  (branch-foo)
     \    ^-- I wanted them here!
      \
       D--E--F--G  (branch-bar)
          ^--^--^-- Opps wrong branch!
    
    While on branch-bar:
    $ git reset --hard D # remember the SHAs for E, F, G (or E and G for a range)
    
    A--B--C  (branch-foo)
     \
      \
       D-(E--F--G) detached
       ^-- (branch-bar)
    
    Switch to branch-foo
    $ git cherry-pick E..G
    
    A--B--C--E'--F'--G' (branch-foo)
     \   E--F--G detached (This can be ignored)
      \ /
       D--H--I (branch-bar)
    
    Now you won't need to worry about the detached branch because it is basically
    like they are in the trash can waiting for the day it gets garbage collected.
    Eventually some time in the far future it will look like:
    
    A--B--C--E'--F'--G'--L--M--N--... (branch-foo)
     \
      \
       D--H--I--J--K--.... (branch-bar)
    
    0 讨论(0)
  • 2020-11-22 03:58

    Had just this situation:

    Branch one: A B C D E F     J   L M  
                           \ (Merge)
    Branch two:             G I   K     N
    

    I performed:

    git branch newbranch 
    git reset --hard HEAD~8 
    git checkout newbranch
    

    I expected that commit I would be the HEAD, but commit L is it now...

    To be sure to land on the right spot in the history its easier to work with the hash of the commit

    git branch newbranch 
    git reset --hard #########
    git checkout newbranch
    
    0 讨论(0)
提交回复
热议问题