how to discard git local branch changes? eg, local branch with version: A->B->C Now I am on version A, and it has some changes conflict with latest version C. I want to discard local changes and pull the latest version C.
$ git pull
I will meet some error. and there are many files, so I don't need to do many times $ git co files
Is there any better way?
Git reset is essentially the opposite of the command git add . It will undo the git add to remove the changed file from version control, and then you can git checkout to undo the changes from the file.
git clean -df will discard any new files or directories that you may have added, in case you want to throw those away. If you haven't added any, you don't have to run this. git pull (or if you are using git shell with the GitHub client) git sync will get the new changes from GitHub.
For all unstaged files in current working directory use: git restore . That together with git switch replaces the overloaded git checkout (see here), and thus removes the argument disambiguation. If a file has both staged and unstaged changes, only the unstaged changes shown in git diff are reverted.
Try Git checkout --<file> to discard uncommitted changes to a file. Git reset --hard is for when you want to discard all uncommitted changes. Use Git reset --hard <commit id> to point the repo to a previous commit.
If you have uncommitted changes that you want to discard, use this:
$ git reset --hard
which is equivalent to
$ git reset --hard HEAD
This removes all the local uncommitted changes. If you want to remove some offending commits from your local branch, try rewinding it:
$ git reset --hard HEAD^ #moves HEAD back by one commit
or e.g.
$ git reset --hard HEAD~3 #moves HEAD back by 3 commits
Use these with caution, as you won't be able to undo these operations. Once you're done cleaning up your local branch, use git pull
to get the latest code.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With