问题
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 git checkout design would do it, but it just tells me I'm already in branch design and that I have 3 modified files.
How would I discard those changes and get the branch as it stands now on the remote server?
回答1:
Note: You CANNOT UNDO this.
Try git checkout -f this will discard any local changes which are not committed in ALL branches and master.
回答2:
git reset --hard can help you if you want to throw away everything since your last commit
回答3:
If you don't want any changes in design and definitely want it to just match a remote's branch, you can also just delete the branch and recreate it:
# Switch to some branch other than design
$ git br -D design
$ git co -b design origin/design # Will set up design to track origin's design branch
回答4:
git diff master > branch.diff
git apply --reverse branch.diff
回答5:
@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
回答6:
In the source root:
git reset ./ HEAD <--un-stage any staged changes
git checkout ./ <--discard any unstaged changes
回答7:
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.
回答8:
git checkout -f
This is suffice for your question. Only thing is, once its done, its done. There is no undo.
来源:https://stackoverflow.com/questions/4830056/how-to-discard-all-changes-made-to-a-branch