Reset local repository branch to be just like remote repository HEAD

后端 未结 21 1983
挽巷
挽巷 2020-11-22 02:19

How do I reset my local branch to be just like the branch on the remote repository?

I did:

git reset --hard HEAD

But when I run a <

21条回答
  •  我在风中等你
    2020-11-22 02:57

    This is something I face regularly, & I've generalised the script Wolfgang provided above to work with any branch

    I also added an "are you sure" prompt, & some feedback output

    #!/bin/bash
    # reset the current repository
    # WF 2012-10-15
    # AT 2012-11-09
    # see http://stackoverflow.com/questions/1628088/how-to-reset-my-local-repository-to-be-just-like-the-remote-repository-head
    timestamp=`date "+%Y-%m-%d-%H_%M_%S"`
    branchname=`git rev-parse --symbolic-full-name --abbrev-ref HEAD`
    read -p "Reset branch $branchname to origin (y/n)? "
    [ "$REPLY" != "y" ] || 
    echo "about to auto-commit any changes"
    git commit -a -m "auto commit at $timestamp"
    if [ $? -eq 0 ]
    then
      echo "Creating backup auto-save branch: auto-save-$branchname-at-$timestamp"
      git branch "auto-save-$branchname-at-$timestamp" 
    fi
    echo "now resetting to origin/$branchname"
    git fetch origin
    git reset --hard origin/$branchname
    

提交回复
热议问题