Remove from staging

Remove from staging#

This page describes how to remove files from the staging area and return them to the changed files.

It can be achieved with git reset command.

Basic example#

So here is an example with two files. There is an initial commit which only has file1 in it. But then file2 is created, and file1 is changed, and both are added to the staging area - which is shown on the first git status.

Then a git reset is performed - which returns file2 to untracked and file1 to not staged.

%%bash
mkdir rm_staging_example
cd rm_staging_example
git init &> /dev/null

# Creating the original comitatus so 
# that there is something to go back to
echo "test" >> file1
git add --all
git commit -m 'initial commit' &> /dev/null

# creating new file
echo "some changes" >> file1
echo "test" >> file2
git add --all

echo "========status refore reset========"
git status

git reset

echo "========status after reset========"
git status


echo "========file1 content========"
cat file1

cd ..
rm -r rm_staging_example
========status refore reset========
On branch master
Changes to be committed:
  (use "git restore --staged <file>..." to unstage)
	modified:   file1
	new file:   file2

Unstaged changes after reset:
M	file1
========status after reset========
On branch master
Changes not staged for commit:
  (use "git add <file>..." to update what will be committed)
  (use "git restore <file>..." to discard changes in working directory)
	modified:   file1

Untracked files:
  (use "git add <file>..." to include in what will be committed)
	file2

no changes added to commit (use "git add" and/or "git commit -a")
========file1 content========
test
some changes

Specify files#

You can perform git reset' only on specified files by simply counting them as arguments to the git reset` command.

The following example simply copies the basic example but specifies file1 in the git reset command. There is a corresponding result - only file1 is returned, removed from the staging area.

%%bash
mkdir rm_staging_example
cd rm_staging_example
git init &> /dev/null

# Creating the original comitatus so 
# that there is something to go back to
echo "test" >> file1
git add --all
git commit -m 'initial commit' &> /dev/null

# creating new file
echo "some changes" >> file1
echo "test" >> file2
git add --all

echo "========status refore reset========"
git status

# the same but file specified
git reset file1

echo "========status after reset========"
git status


echo "========file1 content========"
cat file1

cd ..
rm -r rm_staging_example
========status refore reset========
On branch master
Changes to be committed:
  (use "git restore --staged <file>..." to unstage)
	modified:   file1
	new file:   file2

Unstaged changes after reset:
M	file1
========status after reset========
On branch master
Changes to be committed:
  (use "git restore --staged <file>..." to unstage)
	new file:   file2

Changes not staged for commit:
  (use "git add <file>..." to update what will be committed)
  (use "git restore <file>..." to discard changes in working directory)
	modified:   file1

========file1 content========
test
some changes