How to discard all changes made to a branch?

后端 未结 9 1297
粉色の甜心
粉色の甜心 2021-01-29 18:03

I\'m working in a branch (i.e. design) and I\'ve made a number of changes, but I need to discard them all and reset it to match the repository version. I thought

相关标签:
9条回答
  • 2021-01-29 18:38

    When you want to discard changes in your local branch, you can stash these changes using git stash command.

    git stash save "some_name"

    Your changes will be saved and you can retrieve those later,if you want or you can delete it. After doing this, your branch will not have any uncommitted code and you can pull the latest code from your main branch using git pull.

    0 讨论(0)
  • 2021-01-29 18:48

    In the source root: git reset ./ HEAD <--un-stage any staged changes git checkout ./ <--discard any unstaged changes

    0 讨论(0)
  • 2021-01-29 18:51
    git diff master > branch.diff
    git apply --reverse branch.diff
    
    0 讨论(0)
  • 2021-01-29 18:51

    @Will, git immersion is a really nice and simple git tutorial. it will show you how to undo changes for the following cases: unstaged, staged and committed. labs 14-18

    0 讨论(0)
  • 2021-01-29 18:52

    REVERSIBLE Method to Discard All Changes:

    I found this question after after making a merge and forgetting to checkout develop immediately afterwards. You guessed it: I started modifying a few files directly on master. D'Oh! As my situation is hardly unique (we've all done it, haven't we ;->), I'll offer a reversible way I used to discard all changes to get master looking like develop again.

    After doing a git diff to see what files were modified and assess the scope of my error, I executed:

    git stash
    git stash clear
    

    After first stashing all the changes, they were next cleared. All the changes made to the files in error to master were gone and parity restored.

    Let's say I now wanted to restore those changes. I can do this. First step is to find the hash of the stash I just cleared/dropped:

    git fsck --no-reflog | awk '/dangling commit/ {print $3}'
    

    After learning the hash, I successfully restored the uncommitted changes with:

    git stash apply hash-of-cleared-stash
    

    I didn't really want to restore those changes, just wanted to validate I could get them back, so I cleared them again.

    Another option is to apply the stash to a different branch, rather than wipe the changes. So in terms of clearing changes made from working on the wrong branch, stash gives you a lot of flexibility to recover from your boo-boo.

    Anyhoo, if you want a reversible means of clearing changes to a branch, the foregoing is a less dangerous way in this use-case.

    0 讨论(0)
  • 2021-01-29 18:55

    Note: You CANNOT UNDO this.

    Try git checkout -f this will discard any local changes which are not committed in ALL branches and master.

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