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?
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.
git diff master > branch.diff
git apply --reverse branch.diff
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
In the source root:
git reset ./ HEAD <--un-stage any staged changes
git checkout ./ <--discard any unstaged changes
@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
git reset --hard can help you if you want to throw away everything since your last commit
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.
git checkout -f
This is suffice for your question. Only thing is, once its done, its done. There is no undo.
Source: Stackoverflow.com