Different Ways to Unstage Files in Git – Junos Notes

As developers, using and working with Git is mandatory. Git users are increasingly dealing with many files in the local repository. If any user accidentally added any file to their Git, they can remove it from their index by performing the lag files in Git. The staging file is very beneficial, it can be used to separate files into different commits or to work on some other modifications.

If you are a beginner, then take help from the Git Command Tutorial provided by Junos Notes. However, this tutorial is entirely on How to despise files in Git and addresses various methods to unzip all files or particular files or elaborately committed files in the following modules.

  • Prerequisites
  • Despise a file in Git
  • 1. Unstage Files using the command git ‘rm’ Case 1: rm -cached on
    • a new file that is not committed. Case 2: rm -cached in
    • the existing file.
  • 2. Unstage Files using git reset
    • What does the git reset command do?
  • Disorganize all
  • files in Git

  • Delete unconfigured changes
  • in Git Disorganize committed files in Git Unstage confirms

    • Soft Unstage confirms strict
    • prerequisites

  • First, the Git installation is as required A Git
  • project
  • Create a local and remote repository
  • A
    • Linux terminal/command line window : Search activities > > Windows Terminal
    • : Right-click Start > command prompt (or Windows PowerShell

)

Override a file’s settings

in Git

In Git, detasking a file can be done in two ways

. 1) git

rm -cached <file name> 2) git reset Head <file name> 1

. Unstage Files using

the git ‘rm’ command

One of the methods to disorganize git files is to use the ‘rm’ command. It can be used in two ways:

1) In the new file that is not on Github. 2) In the existing file that exists on Github.

Let’s check the process of using the git rm command in both scenarios.

Case 1: rm -cached on a new file that is not committed.

rm -cached <brand-new-file-name> is useful for deleting only staging area files where this file is not available on GitHub. After running this command, the file remains on the local machine, it is simply despised from the staging area.

Example

: $ git add filetwo.txt $ git status On the branch master Changes to commit: (use “git reset HEAD <file>…” to disorganize) New file: filetwo.txt $ git rm -cached filetwo.txt $ git status On the branch master Untracked files: (use “git add <file>…” to include in what will be committed) Filetwo.txt Nothing added to commit but untraced files present (use “git add” to crawl)

Case 2: rm -cached in the existing file.

  • How to delete Git Commit files | Git Remove File from Commit Stage
  • How to clear Git cache | Learn Git Clear Cache in Different Ways
  • How to Reset Git to HEAD |

  • What is Git HEAD? | How to reset Git Head to last commit

if ‘rm -cached <filename-existing>’. The command is used in the existing file in Git, then this file will be considered for deletion and endures as untracked on the machine. If we make a confirmation after this command, then the file on Github will be deleted forever. We must be very careful when using this command. Therefore, this case is not recommended for unpacking a file.

Reference: How to clean up Git 2 branches

. Disorganize

files using git reset

The easiest way to despise files in Git is to use the “git reset” command and specify the file you want to deconfigure

. git reset <commit> – <path> By default, the commit

parameter is optional: if you don’t specify it, it refers to HEAD.

What does the git reset command do?

This command will reset the index entries (the ones you added to your staging area) to their state on the specified commit (or HEAD if you didn’t specify any commits).

In addition, we use double hyphens as argument disambiguation, which means that the argument you are specifying can be related to two different objects: branches and directories, for example.

As a quick example, suppose you added a file named “README” to your staging area, but now you want to despise this file.

$ git status On the branch master Your branch is updated with ‘origin/master’. Changes to be confirmed: (use “git reset HEAD <file>…” to despise) new file: README

To unconfigure the README file, you need to run the following command

$git reset – README

Now you can check the status of your working directory again with “git status” $ git status On the

branch master Your branch is updated with ‘origin/master’. Untracked files: (use “git add <file>…” to include in what will be committed) README nothing was added to commit, but untraced files are present (use “git add” to crawl)

As you probably already understood, the “git reset” command is doing the exact opposite of the “git add” command: it deletes files from the index.

Despise all files in Git

Previously, we have seen how you can decompose a file by specifying a path or a file to reset.

In some cases, you may want to decompose all the files in the index.

To decompose all files you can also use the “git reset” command without specifying any file or path.

$git reset

Again, suppose you have created two files and a directory and added them to your staging area

. $ git status In the branch master Your branch is updated with ‘origin/master’. Changes to be confirmed: (use “git reset HEAD <file>…” to despreve) new file: README New file: directory/archive

To decompose all files and directories, run “git reset” and they will be removed from the staging area to your working directory. $ git reset $ git

status On the branch master Your branch office is updated with ‘origin/master’. Untracked files: (use “git add <file>…” to include in what will be committed) README directory/ nothing added to commit but untracked files present (use “git add” to crawl) Delete unconfigured changes

in Git

In some cases, after you retrieve staging area staging files, you may want to delete them entirely.

To delete unconfigured changes, Use the “Git Checkout” command and specify the paths to be deleted.

$ git checkout – <path>

Again, let’s say you have a file that is not currently preconfigured in your working directory

. $ git status In the branch master Your branch is updated with ‘origin/master’. Untentative changes to commit: (use “git add <file>…” to update what will be committed) (use “git checkout – <file>…” to discard changes to the working directory) modified: README No changes were added to commit (use “git add” and/or “git commit -a”)

To discard changes made to this unpreconfigured file, Run the “git checkout” command and specify the name of the file.

$ git checkout – README $ git status On the branch master Your branch is updated with ‘origin/master’. Nothing to confirm, clean worktree Alternatively,

if you want to discard your entire working directory, go back to the root of your project and run the following command

. $ git checkout – . $ git status On the branch master Your branch is updated with ‘origin/master’. Nothing to confirm, clean worktree

Despise

committed files in Git

In some cases, you actually committed files to your git directory (or repository), but you want to unzip them to make some modifications to your commit.

Luckily for you, there’s a command for that too.

Unstage Commits Soft

To override commits in Git, use the “git reset” command with the “-soft” option and specify the commit hash. $ git reset -soft <commit

>

Alternatively, if you want to unzip your last commit, you can the “HEAD” notation to easily reverse it

. $ git reset -soft HEAD~1

Using the “-soft” argument, changes are saved in your working directory and index

.

As a consequence, your edits are kept, they are simply no longer in the Git repository.

Inspecting your repository after a soft reset would give you the following result, since you deconfigured the last commit.

$ git status On the branch master Your branch is updated with ‘origin/master’. Changes to commit: (use “git reset HEAD <file>…” to disorganize) modified: README

What happens if you reset your confirmation?

In this case, all changes would

be discarded

and you would lose your changes.

Unstage Commits Hard

To override commits in Git and discard all changes, use the “git reset” command with the “-hard

” argument. $ git reset -hard <commit

>

Note: Be careful when using the hard reset command, you will lose all your changes when it is completely reset.

Conclusion

In this tutorial, learned how you can easily disorganize files in Git using the “git reset” command.

You learned that you can specify a path or a single file to disorganize files and keep working on your files until you commit them to your repository.

If you’re curious about Git, we have an entire section dedicated to it on the website, be sure to take a look!