Quick cheat sheet to unstage files in git
You are working on a project, staging, committing, and pushing changes. You just staged a file, and you want to remove it from the index, but not sure the best command to use. Worry no more because you are in the right place.
Here are the top three commands to use.
1. The cached command
git rm --cached <file>
helps to unstage new files, i.e., files not committed.
2. The restore command
git restore --staged <file>
is crucial in unstaging modified files, i.e., files already committed.
3. Git reset mixed
You can use the reset command in four ways when unstaging a file.
Use either
git reset <file>
or
git reset -- <file>
to git unstage files before committing them.
Reset either the HEAD
git reset HEAD <file>
or all
git reset .
to unstage modified files.
Learning how to apply the commands will enable you to control the staging area and easily collaborate with other developers.
It would be best to understand file staging before taking a practical look at the git unstage file. The tutorial then presents you with relatable examples of each of the three primary commands in action.
What do we mean by git unstage file?
Git unstage file is a way to undo changes at the index. An index is the second stage in the git workflow.
The first stage is the working directory. The working directory is the place you actively create or modify files. Next, it sends your files to temporary storage called the index or staging area. At the index, git takes a snapshot of the changes before finally storing them in the git database.
Three things happen in the git database. First, git takes a copy of the staging area changes and stores it. Secondly, each round of storage gets an id called commit hash. The commit hash forms part of the history.
Lastly, git makes one of the commits the HEAD. The HEAD is the commit you are viewing to act on, usually the last commit in history.
Unstaging a file returns the file's changes to the working directory without deleting it from the file system. Then, the file is untracked, as you are about to see in the examples section.
Lab setup to explore steps the unstage files in git
We will explore what happens when you git ustage file before and after pushing to a remote repo. For that reason, we will have two workstations: a local repo without a remote and another one with a remote.
Let us create the local repo without a remote. First, head over to your terminal and create a directory called unstage_before_push
.
mkdir unstage_before_push
Navigate to the unstage_before_push
directory.
cd unstage_before_push
Initialize the repo.
git init
Add two HTML
files.
touch index.html about.html
Move to GitHub and create a remote repo to practice git unstage file after push. I am creating a repo called unstage_after_push
.
Copy the repo URL, cd ..
and clone it on your terminal.
Like in the unstage_before_push
directory, let us create some files to ease manipulating the index.
Move into the cloned repo, unstage_after_push
.
cd unstage_after_push
Create some text files.
touch file1.txt file2.txt
We will not initialize a repo this time around because GitHub already did it for us. You have the first commit if you created the remote with a README.md
file. Let us confirm that.
git log
I have a commit.
Let us stage, commit and push the two text files we just created.
git add . git commit -m "track text files" git push
The remote repo's page on GitHub shows our new commit.
Now that we have a setup to explore the git unstage file, let us dive into more relatable examples.
git unstage file before push
If you have not modified the tracked files, use any of these commands to unstage them:
git rm --cached <file>
OR
git reset <file>
OR
git reset -- <file>
Example-1: Use git rm --cached <file>
to git unstage file
cd into the unstage_before_push
directory.
cd .. cd unstage_before_push
Running the command
git status
shows our files are untracked.
Stage the files.
git add .
Recheck the status.
git status
The files are staged, and git is ready to commit the changes.
Let us unstage each file using the --cached
flag.
git rm --cached index.html
git status
The index.html
file got unstaged!
The --cached
flag denotes changes temporarily held at the index, whereas rm
is the short form of remove. Let us restage the file before redoing the git unstage file on it.
Use any of these commands:
git add index.html git add . git add * git stage index.html
Example-2: Use git reset <file>
to unstage file before commit
Git reset is one of the most crucial commands when undoing changes. The three types of reset command are hard, soft, and mixed.
Git reset hard removes changes from the commit history and deletes the files from the working directory. Git reset soft resets the commit HEAD, while git reset mixed unstages files. It is the default git commit that is why you won't see us specifying any flag when resetting changes in this tutorial.
The ability of the mixed command to exist in various forms and take various flags enables us to apply it in several ways to git unstage files. Here is an example.
Running the command
git reset about.html
in the unstage_before_push
folder unstages the about.hmtl
file.
git status
Example-3: Use git reset -- <file>
to git unstage file
Another way to reset changes on new files is using the double dashes before the filename. Note: leave a space between the two dashes and the filename.
Let us use it to unstage the index.html file, as follows:
git reset -- index.html
Checking the status
git status
shows the index.html
file is back to the untracked stage.
Enough with new files. Let us see how to git unstage files after commit and push.
Git unstage file after push
Sometimes you need to git unstage files after push. That is, modify a file, restage and unstage file. Let me demonstrate the scenario.
We can unstage the files using the following three commands.
git restore --staged <file> git reset HEAD <file> git reset .
Navigate into the unstage_after_push
directory.
cd .. cd unstage_after_push
Modify the text files.
echo change1 >> file1.txt echo change2 >> file2.txt
Stage the changes.
git add .
Checking the status,
git status
git tells us we have unstaged the modified files.
Example-4: Unstage file using git restore --staged <file>
command
Run the command
git restore --staged file1.txt
to unstage the file1.tx
t file
Checking the status
git status
confirms the restore command unstaged file1.txt
Example-5: unstage file using the git reset HEAD <file>
command
We can undo the changes made on file2.txt
by playing the command
git reset HEAD file2.txt
Checking the status
git status
shows file2.txt
got untracked. Now, both of our files are unstaged.
Stage both files in readiness for the reset all command
git add *
Example-6: git unstage file using the reset all command
Finally, we can unstage all the modified files using the reset command using git reset all command.
git reset .
Recheck the status.
git status
And voila, all the modified files got unstaged!
Summary
Using the right command is the key to unlocking efficiency when undoing changes. It is more convenient to git unstage files using the cached, reset and restore commands by grouping the commands as follows.
How to unstage a file before push
git rm --cached <file>
OR
git reset <file>
OR
git reset -- <file>
How to unstage a file after commit
git restore --staged <file>
OR
git reset HEAD <file>
OR
git reset .
You are calling “–” slashes, but they are dashes! “//” are slashes.
Thanks for highlighting this typo. It has been corrected.