How to reset a branch to another branch with git?

前端 未结 5 1558
走了就别回头了
走了就别回头了 2020-12-12 12:14

let\'s say that we have an hotfixes branch which was created from master. we added commits to hotfixes, but those commits were not use

相关标签:
5条回答
  • 2020-12-12 12:31

    Based on a few of the answers in this thread, I did the following script with a few prompts to reduce risk of messing stuff up:

    #!/bin/bash
    # Questions for loop:
    for value in {1..3}
    do
      # Asking if user wants to reset hotfix:
      if [ "$value" == "1" ] ;then
        echo -n "Are you sure you want to hard reset the hotfix branch (y/n)? "
        read answer
        if [ "$answer" == "${answer#[Yy]}" ] ;then
            echo 'Okay, maybe next time.'
            exit
        fi
      fi
      # Asking if user is in void:
      if [ "$value" == "2" ] ;then
        echo -n "Are you in the void branch (y/n)? "
        read answer
        if [ "$answer" == "${answer#[Yy]}" ] ;then
            echo 'You should checkout to the void branch.'
            exit
        fi
      fi
      # Asking if user has any uncommited changes:
      if [ "$value" == "3" ] ;then
        echo -n "Do you have any uncommited changes (y/n)? "
        read answer
        if [ "$answer" == "${answer#[Nn]}" ] ;then
            echo 'You should commit your changes to avoid losing them.'
            exit
        fi
      fi
    done
    
    echo 'Resetting...'
    git checkout void
    git branch -f hotfix origin/master
    git push -f origin hotfix
    

    100% open to any feedback to improve this script.

    0 讨论(0)
  • 2020-12-12 12:38

    this is how i did it with basic Git commands:

    git checkout hotfixes
    git reset --hard master
    git push --force origin hotfixes
    

    of course it's important to notify everyone working on hotfixes. most likely they will have to delete their local copy and start from a fresh one. an alternative, less invasive idea is to create a new branch:

    git checkout master
    git branch -tb hotfixes-2 # this creates branch `hotfixes-2` from a copy of `master`
    git push origin HEAD # this creates `hotfixes-2` on the remote server
    
    0 讨论(0)
  • 2020-12-12 12:40

    The answers here are solid. I have needed this exact change when resetting my staging branch to master. In that case I want to both reset the origin to match master and also reset my local to match that. So here is a git alias that allows you to pass in the branch name and do both commands in one move. (It's a little dangerous)

    reorient = "!f() { git push origin +master:$1 && git reset --hard origin/$1 ; }; f"
    

    Then use it like:

    git reorient hotfixes
    

    The answers above were totally correct. But this will simply allow for fewer keystrokes and a quicker turnaround! Hope it helps.

    0 讨论(0)
  • 2020-12-12 12:56

    You mean you want to push your local master to the remote hotfixes branch? Like this:

    git push origin +master:hotfixes
    

    However, this requires that you are allowed to re-write the history on the remote side.

    0 讨论(0)
  • 2020-12-12 12:57

    If I understood your question correctly, what you're looking for is a way to move the branch pointer of origin/hotfixes to point to the current revision of origin/master.

    If that be the case, these set of command should work (assuming you already have checked out hotfixes in your local git repo any time in the past):

    # git branch -f does not allow modifying the currently checked out
    # branch, so checkout any other branch than hotfixes
    git checkout <SOME_OTHER_BRANCH_THAN_HOTFIXES>
    
    # Move the branch pointer of hotfixes to the commit currently
    # pointed by origin/master
    git branch -f hotfixes origin/master
    
    # Force push the history rewrite in the hotfixes branch
    # into origin
    git push -f origin hotfixes
    
    0 讨论(0)
提交回复
热议问题