Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to discard git local branch changes?

Tags:

git

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?

like image 468
Jane_Meng Avatar asked Oct 21 '11 08:10

Jane_Meng


People also ask

Does git reset remove local changes?

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.

How do I discard local changes and pull latest from GitHub repository?

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.

How do I remove unstaged changes?

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.

How do I delete uncommitted changes in git?

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.


1 Answers

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.

like image 166
ayoy Avatar answered Oct 02 '22 17:10

ayoy