You can use git checkout — command to discard tracked changes and git clean -fd command to discard untracked changes.
Example:
Create a new git local repository:
1 2 |
## Create a new git local repository git init myrepo && cd myrepo |
Create first commit:
1 2 3 4 |
## Create first commit echo "cat" > file1 git add file1 git commit -m "commit1" |
Check git status:
1 2 |
## Check git status git status ## nothing to commit, working tree clean |
Do some changes in your working tree:
1 2 3 4 |
## Update the file in working tree echo "dog" >> file1 ## Add a new file in working tree echo "cow" > file2 |
Check git status:
1 2 3 4 5 6 7 |
## Check git status git status ## Changes not staged for commit: ## modified: file1 ## Untracked files: ## file2 |
Discard working tree tracked changes (file1):
1 2 |
## git checkout -- git checkout -- file1 |
Check git status:
1 2 3 4 |
## Check git status git status ## Untracked files: ## file2 |
Discard working tree untracked changes (file2):
1 2 3 |
git clean -fd ## -f is to force, -d is for untracked directory. ## You can also use -n option to just report what gets deleted. |
Check git status:
1 2 |
## Check git status git status ## nothing to commit, working tree clean |
Remove your git local repository:
1 2 |
## Remove the git local repository cd .. && rm -rf myrepo |