Reset local repository branch to be just like remote repository HEAD

后端 未结 21 1606
挽巷
挽巷 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:53

    If you had a problem as me, that you have already committed some changes, but now, for any reason you want to get rid of it, the quickest way is to use git reset like this:

    git reset --hard HEAD~2
    

    I had 2 not needed commits, hence the number 2. You can change it to your own number of commits to reset.

    So answering your question - if you're 5 commits ahead of remote repository HEAD, you should run this command:

    git reset --hard HEAD~5
    

    Notice that you will lose the changes you've made, so be careful!

    0 讨论(0)
  • 2020-11-22 02:56

    The question mixes two issues here:

    1. how to reset a local branch to the point where the remote is
    2. how to clear your staging area (and possibly the working directory), so that git status says nothing to commit, working directory clean.

    The one-stop-answer is:

    1. git fetch --prune (optional) Updates the local snapshot of the remote repo. Further commands are local only.
      git reset --hard @{upstream}Puts the local branch pointer to where the snapshot of the remote is, as well as set the index and the working directory to the files of that commit.
    2. git clean -d --force Removes untracked files and directories which hinder git to say “working directory clean”.
    0 讨论(0)
  • 2020-11-22 02:57

    Use the commands below. These commands will remove all untracked files from local git too

    git fetch origin
    git reset --hard origin/master
    git clean -d -f
    
    0 讨论(0)
  • 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
    
    0 讨论(0)
  • 2020-11-22 02:57

    Here is a script that automates what the most popular answer suggests ... See https://stackoverflow.com/a/13308579/1497139 for an improved version that supports branches

    #!/bin/bash
    # reset the current repository
    # WF 2012-10-15
    # see https://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"`
    git commit -a -m "auto commit at $timestamp"
    if [ $? -eq 0 ]
    then
      git branch "auto-save-at-$timestamp" 
    fi
    git fetch origin
    git reset --hard origin/master
    
    0 讨论(0)
  • 2020-11-22 03:01

    All of the above suggests are right, but often to really reset your project, you also need to remove even files that are in your .gitignore.

    To get the moral equivalent of erasing your project directory and re-cloning from the remote is:

    git fetch
    git reset --hard
    git clean -x -d -f
    

    Warning: git clean -x -d -f is irreversible and you may lose files and data (e.g. things you have ignored using .gitignore).

    0 讨论(0)
提交回复
热议问题