You can use git restore –staged command to undo staged changes added by git add command.
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 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 ## no changes added to commit (use "git add" and/or "git commit -a") |
Stage the changes from working tree to the staging area:
1 2 |
## Stage the changes git add . |
Check git status:
1 2 3 4 5 |
## Check git status git status ## Changes to be committed: ## modified: file1 ## new file: file2 |
Undo staged changes added by git add command:
1 2 3 |
## Undo staged changes - git add ## git restore --staged git restore --staged . |
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 ## no changes added to commit (use "git add" and/or "git commit -a") |
Undo unstaged changes:
1 2 3 |
## Undo unstaged changes git clean -fd git checkout -- file1 |
Check git status:
1 2 3 |
## Check git status git status ## nothing to commit, working tree clean |
Remove the local git repository:
1 2 |
## Remove the git local repository cd .. && rm -rf myrepo |