How to discard changes or unstage file when using git and github


The normal workflow of working with git and github are:

  • git add: Select specific files or changes to include in the commit.
  • git commit: Create a snapshot of the selected changes with a descriptive commit message.
  • git push: Push the committed changes to a remote repository for sharing and collaboration.

Example:

  • git add file1.txt: Select file1.txt to be included in the commit.
  • git commit -m "Updated file1.txt": Create a snapshot of the changes made to file1.txt with a descriptive commit message.
  • git push origin main: Push the committed changes to the remote repository named origin in the main branch.

However, how about if something doesn’t work out smoothly, and we need to back up a little bit.
For example

discard the changes to some file in the current directory

The anwer is:
use git restore <file-with-full-path> to discard changes in working directory

for example:
git restore dir1/file1.txt

I staged the file by using git add, but now I want to unstage

the answer is:
use git restore --staged <file-with-full-path> to unstage
for example:

git restore --staged dir1/file1.txt


Author: robot learner
Reprint policy: All articles in this blog are used except for special statements CC BY 4.0 reprint policy. If reproduced, please indicate source robot learner !
  TOC