Git is a popular distributed version control system that is used by developers all over the world. It provides a queue called index where all changes made by the developer are staged before they are committed. But sometimes you may need to remove one or more files & folders from this index before committing. This is called unstaging files & folders from git index. In this article, we will learn how to unstage files in git.
How to Unstage Files in Git
We will look at some use cases to unstage files in git.
1. Unstage All Files
If you want to unstage all files that have been added or modified in your present working branch, since its last commit, use git reset command.
$ git reset
The above command will remove all changes from staging area. It will not delete any of these files but not include it for commits, unless you add them back using git add command.
The git index file is located at .git/index it contains a list of all changes to be committed. The git reset command will wipe this file clean.
2. Unstage Single File or Folder
Often you may want to unstage only a single file or folder from commit. In such cases, simply add the location of the file or folder after git reset command.
$ git reset /path/to/file OR $ git reset /path/to/folder
3. Using .gitignore file
.gitignore is a simple text file that you can add in your git repository’s main folder or any of its subfolders. Any file, folder path added to this file will not be included in staging. So if you want to unstage a file or folder, as a one-off task, use any of the above commands. If you want to repeatedly avoid including any file or folder from staging area, use .gitignore file.
4. Unstage Committed Files
What if you want to unstage files that you have already committed? In such cases, you use git reset as shown below.
$ git reset [option] [commit]
In the above command, you can use –soft, –hard or –mixed option, along with the commit to which you want to reset your index head.
Here is the command to reset last commit.
$ git reset --mixed HEAD~1
You can replace 1 in the above command with the number of commits, you want to go back.
Alternatively, you can also find out the commit id from git log file and mention it after the option argument. Here is a command to reset your branch to commit id abdeaed3896387.
$ git reset --mixed abdeaed3896387
Let us look at it in detail what –soft, –hard and –mixed options do in the above command.
–soft
- Updates ref pointers
- Staging Index remains untouched
- Working Directory remains untouched
–mixed
- Updates ref pointers
- Resets staging index to specified commit
- Changes reverted from the staging index are moved to the working directory
–hard
- Updates ref pointers to specified commit
- Staging index is reset to match the specified commit
- Working Directory is reset to match specified commit
- All pending changes in the Working Directory and Staging Index are lost
So please be careful before using –hard option in git reset.
Please note, if you have already pushed your commits to remote repository, then you will have to run the following command after you run git reset locally, in order to revert your remote branch. Otherwise, your remote and local branches will be out of sync.
$ git push origin HEAD --force
In this article, we have learnt how to unstage changes in git repository. We have seen how to unstage files in different use cases.
Also read:
How to Enable Bluetooth from Command Line
How to Add Repository in Ubuntu
How to Delete Folders Older Than 7 Days in Ubuntu
NGINX Prevent Host Header Attack
How to Enable IP Forwarding in Linux
Related posts:
Sreeram has more than 10 years of experience in web development, Python, Linux, SQL and database programming.