Git Unstage file
Common question on git unstage :This post covers the following questions with in-depth answers along with step-by-step example.
git reset - Why are there 2 ways to unstage a file in git? - Stack Overflow
version control - How to undo 'git add' before commit? - Stack Overflow
git - How can I unstage my files again after making a local commit.
More article on Git Series
Unstage a File in Git:
In Git, unstaging a file can be done in two ways.1) git rm --cached <file-name>
2) git reset Head <file-name>
These commands are very useful when we add the files to git. But later or before commit, we realize that mistakenly added the files to git. we should remove the files from git. The process of removing a file from staging area is called "Unstaging file" from git.
We will be discussing indepth in this tutorial.
Way 1) git rm --cached <file-name>:
This can be used in two ways.1) On the brand new file which is not on github.
2) On existing file which exists on github.
We will see how this command behaves on above 2 scenarios.
Case 1: rm --cached on new file which is not committed.
rm --cached <brand-new-file-name>is useful when we want to remove only the file(s) from staging area where this file is not available on github ever. After executing this command, the file remains in the local machine, it just unstaged from staging area.Example:
The below example is on the new file.Venki@Venki-PC MINGW64 /d/Site/adeepdrive/git/practice/gitdemo (master) $ ls fileone.txt Venki@Venki-PC MINGW64 /d/Site/adeepdrive/git/practice/gitdemo (master) $ echo "this is second file" >> filetwo.txt Venki@Venki-PC MINGW64 /d/Site/adeepdrive/git/practice/gitdemo (master) $ ls fileone.txt filetwo.txt Venki@Venki-PC MINGW64 /d/Site/adeepdrive/git/practice/gitdemo (master) $ git add filetwo.txt Venki@Venki-PC MINGW64 /d/Site/adeepdrive/git/practice/gitdemo (master) $ git status On branch master Changes to be committed: (use "git reset HEAD <file>..." to unstage) new file: filetwo.txt Venki@Venki-PC MINGW64 /d/Site/adeepdrive/git/practice/gitdemo (master) $ git rm --cached filetwo.txt rm 'filetwo.txt'Venki@Venki-PC MINGW64 /d/Site/adeepdrive/git/practice/gitdemo (master) $ git status On branch master Untracked files: (use "git add <file>..." to include in what will be committed) filetwo.txt nothing added to commit but untracked files present (use "git add" to track)
Case 2: rm --cached on existing file.
Below example will demonstrate on existing file.
Example:
Venki@Venki-PC MINGW64 /d/Site/adeepdrive/git/practice/gitdemo (master)$ git status On branch master Untracked files: (use "git add <file>..." to include in what will be committed) filetwo.txt nothing added to commit but untracked files present (use "git add" to track) Venki@Venki-PC MINGW64 /d/Site/adeepdrive/git/practice/gitdemo (master) $ git add filetwo.txt Venki@Venki-PC MINGW64 /d/Site/adeepdrive/git/practice/gitdemo (master) $ git commit -m "second file commit" [master 2efa8d9] second file commit 1 file changed, 1 insertion(+) create mode 100644 filetwo.txt Venki@Venki-PC MINGW64 /d/Site/adeepdrive/git/practice/gitdemo (master) $ echo "file 1 is modified" >> fileone.txt Venki@Venki-PC MINGW64 /d/Site/adeepdrive/git/practice/gitdemo (master) $ echo "file 2 is modified" >> filetwo.txt Venki@Venki-PC MINGW64 /d/Site/adeepdrive/git/practice/gitdemo (master) $ git add fileone.txt filetwo.txt Venki@Venki-PC MINGW64 /d/Site/adeepdrive/git/practice/gitdemo (master) $ git status On branch master Changes to be committed: (use "git reset HEAD <file>..." to unstage) modified: fileone.txt modified: filetwo.txt Venki@Venki-PC MINGW64 /d/Site/adeepdrive/git/practice/gitdemo (master) $ git commit -m "second commit" [master d81f0ef] second commit 2 files changed, 2 insertions(+) Venki@Venki-PC MINGW64 /d/Site/adeepdrive/git/practice/gitdemo (master) $ ls fileone.txt filetwo.txt $ git rm --cached filetwo.txt rm 'filetwo.txt' Venki@Venki-PC MINGW64 /d/Site/adeepdrive/git/practice/gitdemo (master) $ ls fileone.txt filetwo.txt Venki@Venki-PC MINGW64 /d/Site/adeepdrive/git/practice/gitdemo (master) $ git status On branch master Changes to be committed: (use "git reset HEAD <file>..." to unstage) deleted: filetwo.txt Untracked files: (use "git add <file>..." to include in what will be committed) filetwo.txt Here we see that filetwo.txt is marked as deleted but original file is present on local machine. After git commit, file is completely not tracked by git. $ git commit -m "delete from git commit" [master bc72f1b] delete from git commit 1 file changed, 2 deletions(-) delete mode 100644 filetwo.txt Venki@Venki-PC MINGW64 /d/Site/adeepdrive/git/practice/gitdemo (master) $ ls fileone.txtfiletwo.txt
Venki@Venki-PC MINGW64 /d/Site/adeepdrive/git/practice/gitdemo (master)
$ git status
On branch master
Untracked files:
(use "git add <file>..." to include in what will be committed)
filetwo.txt
nothing added to commit but untracked files present (use "git add" to track)
Way 2) git reset Head <file name>:
There is a scenario, where the file is already exists on the github. We made some changes to the file and added the file to git staging area but not committed. After some time, this change is no longer required. We just want to remove from git index or unstage the file and our new changes should be available in the file. If we use git reset head <file-name> command then changes will not be lost.After unstage, this modified will be in the modified files list when we do git status command..
Example:
Venki@Venki-PC MINGW64 /d/Site/adeepdrive/git/practice/gitdemo (master)$ ls fileone.txt filetwo.txt Venki@Venki-PC MINGW64 /d/Site/adeepdrive/git/practice/gitdemo (master) $ git status On branch master nothing to commit, working tree clean Venki@Venki-PC MINGW64 /d/Site/adeepdrive/git/practice/gitdemo (master) $ echo "added new line" >> filetwo.txt Venki@Venki-PC MINGW64 /d/Site/adeepdrive/git/practice/gitdemo (master) $ git add filetwo.txt Venki@Venki-PC MINGW64 /d/Site/adeepdrive/git/practice/gitdemo (master) $ cat filetwo.txt this is second file file 2 is modified added new line Venki@Venki-PC MINGW64 /d/Site/adeepdrive/git/practice/gitdemo (master) $ git status On branch master Changes to be committed: (use "git reset HEAD <file>..." to unstage) modified: filetwo.txt Venki@Venki-PC MINGW64 /d/Site/adeepdrive/git/practice/gitdemo (master) $ git reset head filetwo.txt Unstaged changes after reset: M filetwo.txt Venki@Venki-PC MINGW64 /d/Site/adeepdrive/git/practice/gitdemo (master) $ cat filetwo.txt this is second file file 2 is modified added new line
No comments:
Post a Comment
Please do not add any spam links in the comments section.