Rebase a single Git commit

后端 未结 5 1254
一个人的身影
一个人的身影 2020-12-22 18:00

Is there a way to rebase a single commit from a branch onto another branch?

I have this branch structure:

-- -- -- -- -- (Master)
            \\
           


        
相关标签:
5条回答
  • 2020-12-22 18:24

    Here's another option:

    1. Make sure you have a remote with a copy of the feature branch
    2. Delete the local feature branch
    3. Create and checkout a new branch with the same name as the old feature branch you just deleted off of master
    4. cherry-pick the one commit from the remote copy of the feature branch you want.

    Commands look like:

    git checkout Feature-branch
    git push -u origin HEAD
    git checkout master
    git branch -D Feature-branch
    git checkout -b Feature-branch
    git cherry-pick HASH-OF-XX
    

    It's not a rebase command no, but it's a rebase in spirit.

    0 讨论(0)
  • 2020-12-22 18:29
    git rebase --onto master branch~1 branch 
    

    This says "rebase the range of commits between last-before-branch and branch (that is, XX commit) on the tip of master branch"

    After this operation branch tip is moved on commit XX, so you want to set it back with

    git checkout branch
    git reset --hard branch@{1}^
    

    Which says "reset the branch tip to the commit before its previous state"

    So a cherry pick is a simpler solution...

    0 讨论(0)
  • 2020-12-22 18:35

    You can cherry-pick XX to master.

    git checkout master
    git cherry-pick <commit ID of XX>
    

    And remove the last commit from the feature branch with git reset.

    git checkout Feature-branch
    git reset --hard HEAD^
    
    0 讨论(0)
  • 2020-12-22 18:43

    It's pretty simple to do actually. The solution is to do an interactive rebase and "drop" all of the commits you don't want to include in the rebase.

    git rebase -i <target_branch> where target_branch is the branch you want to rebase on to

    Then you will edit the file that is opened and pick the commits you do want and drop (or d for short) all the commits you don't want to bring along.

    0 讨论(0)
  • 2020-12-22 18:45

    @Charles response is correct. Anyway I ended up using this so many times, most of all to rebase specific config on a project

      * a8f9182 (HEAD -> production) production configuration
      | * daa18b7 (pre) preproduction configuration
      |/  
      | * d365f5f (local) local configuration
      |/  
      * 27d2835 (dev) amazing new feature that will save the world
    * | 56d2467 (master) boring state of the art for project
    |/
    

    that I create a new command for it:

    $ cat ~/bin/git-rebaseshot 
    COMMIT=$1
    DEST=${2:-HEAD}
    git rebase ${COMMIT}^ ${COMMIT} --onto $DEST
    

    normally you want to autocomplete branch names for that command, so add it sourcing this function (adding to .bashrc or .profile):

    _git_rebaseshot () 
    { 
        __gitcomp_nl "$(__git_refs)"
    }
    

    git autocomplete will search for it

    you can use this command like this:

    # rebase config on prepro on actual HEAD
    $ git rebaseshot prepro 
    # rebase config on local onto dev
    $ git rebaseshot local dev
    # rebase production config on master
    $ git rebaseshot pro master
    

    When you divide features correctly, possibities are endless.

    * a8f9182 (HEAD -> postgres) BBDD config
    * a8f9182 (local) local config
    * a8f9182 (debug) log level config
    * a8f9182 (dev) new feature
    |
    

    I guess this is what quilt people like to do.

    this command will work anyway with whatever sha/ref you provide:

    $ git rebaseshot <Feature branch> master
    $ git rebaseshot <commit of XX> master
    
    0 讨论(0)
提交回复
热议问题