Showing posts with label Git. Show all posts
Showing posts with label Git. Show all posts

Wednesday, July 23, 2025

Two ways to Unstage a File in Git git rm --cached and git reset Head)

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.


Unstage a File in Git

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.

If 'rm --cached <existing-file-name>.'command is used on the existing file on git then this file will be marked for delete and remains as untracked on machine. If we do commit after this command then the file on the github will be deleted permanently. We should be very careful while using this command. Hence, This is not recommend for upntaging a 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.txt  
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)

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

Tuesday, November 30, 2021

[Fixed] Another git process seems to be running in this repository git in 6 ways

1. Overview

In this article, We'll learn how to fix the git error "Another git process seems to be running in this repository" in windows and mac operating systems.

When you are a beginner or working a git project, you get this type of error.

This error is saying git commands are locked by another git process for the current git application. Somewhere you are running two git commands at the same time. Simultaneous execution of git commands lead to error - Another git process seems to be running in this repository.

Another git process seems to be running in this repository


[Fixed] Git error: failed to push some refs to in 5 ways

1. Overview

In this tutorial, We'll learn how to fix the git error failed to push some refs to in 5 ways.

Most of the time you get this error while pushing your change git remote repository.

This might be working a few days back but suddenly started showing a weird error message "failed to push some refs to".

You are stuck at this point and do not what to do now. And you might be thinking because of my new changes?


For this error, there are mainly two reasons.

a) Your branch is set to main and not master
b) Remote repo has new changes and you do not have those changes on your laptop or local branch

Now, you knew about the actual reasons to get this common error.

Let us jump into the fixes.

Git error: failed to push some refs to

[Fixed] fatal: not a git repository (or any of the parent directories): .git in 2 ways

 

1. Overview

In this tutorial, We'll learn how to fix git error "fatal: not a git repository (or any of the parent directories): .git" on windows or mac os or Heroku.

Fixing this error can be done in 2 ways. But first, let us understand the error what is the meaning of the error.


fatal not a git repository (or any of the parent directories) .git

Monday, November 29, 2021

[Fixed] Git - remote: Repository not found in 9 ways (Windows and Mac)

1. Overview

In this tutorial, we'll learn how to fix the "Git - remote: fatal Repository not found" issue in 9 different ways.

You might have seen this error while cloning the repository or while pushing the commits to the remote repository.

This error can be fixed in simple ways.

[Fixed] Git - remote: Repository not found



2. Fix 1 - Git - remote: Repository not found


If you are new to programming and developing then you might have not set remote repo URL on your local repo. And trying to push the changes to remote.

This is needed when you downloaded the project as a zip file.

In this case, you need to set the remote base url to the local repo using the "git remote" command.

More on Git Commands

Follow the below steps.

git remote set-url origin https://github.com/JavaProgramTo/Kotlin-Demo.git

git add *.java
git commit -m "commit title"
git push origin master


3. Fix 2 - Git - remote: Repository not found


If the above is not your case, then you need to make sure that the remote URL is the correct one is pointing out.

Use git remote command as below.

git remote -v
origin	https://github.com/JavaProgramTo/Kotlin-Demo.git (fetch)
origin	https://github.com/JavaProgramTo/Kotlin-Demo.git (push)

If it is showing the right URL then verify it on remote whether it is the correct one or not. If not the correct one then you need to follow fix 1 to set the correct remote repo.


4. Fix 3 - Git - remote: Repository not found


Verify if your password has been reset recently. If you have changed so please make sure the new password is fetched from the credential manager on windows or keychain access on mac.

Sometimes, an old password will be used and a new one is not cached yet.

In this case, Remove all github.com credential details from the system. 
you need to clear the git related info from the credential manager and remove it from keychain access.

On windows

Follow the below commands on windows. After this, you should be able to push and pull the changes.
$ git credential-manager uninstall

$ git credential-manager install


On Mac machine

Open Keychain Access and find your password account in the password category ( you can search it on the top right keychain access page)

Once you find it, delete all keys related to your git source control and then try it again from the clone.

Before this take the backup of your uncommitted changes.


5. Fix 4 - Git - remote: Repository not found


This is a temporary fix and not recommended for the long term.

On Windows

  • Go to the git folder and go inside the .git folder
  • Open 'config' file using notepad or any other editor
  • Change your URL from https://github.com/username/repo_name.git to https://username:[email protected]/username/repo_name.git
  • Save and Push the code, it will work.
Here, you need to save the password and security threat.

On Mac

If the GitHub entry is not present in keychain access then you can clone the app in a different way as below.
git clone https://[email protected]/org/repo.git

Follow the below steps.

  • username with your GitHub username
  • org with your organisation name
  • repo with the repository name

6. Fix 5 - Git - remote: Repository not found


Just run the git remote update command which updates the local repo with the remote repo and its credentials.

If this command is executed without any issues then your issue will be resolved.
git remote update


7. Fix 6, 7, 8, 9- Git - remote: Repository not found


There might be chances of any one of the following.

7.1 If you are not a collaborator, then you may successfully authenticate the GitHub but can not clone or push the changes to the repository as long you are not a collaborator.

7.2 In case of any spelling issues

7.3 If the git remove -v has set to HTTPS but your repo is pointing to the ssh. In this case, you need to remove the ssh and add HTTPS.

git remote remove origin
git remote add origin https://github.com/JavaProgramTo/Kotlin-Demo.git

7.4 Remote repository is not found and deleted.


8. Conclusion


In this article, we've seen all versions of fixes on git error "fatal: repository not found error".


Git Delete Local Branch and Remote Branch (With Force Delete)

1. Overview

In this tutorial, We'll learn how to delete the local branch and remote branch in git with normal and force delete options.

Git provides easy access and manages the git local branches. And also provides exceptional support to remove local branches without any exception.

Removing git local branches can be done in two ways and removing remote repo in one way with commands.

Git Delete Local Branch and Remote Branch



let us look one by one now.

It is very useful to know Git Commands.

Tuesday, December 1, 2020

Top Git Commands With Examples - Developer Uses Everyday

1. Introduction


In this tutorial, You'll learn what are the git commands that can be used in everyday life. You'll see the top git commands with examples. You can not imagine a developer's life without using version control tools such as git, bitbucket, or any tool.

Because this makes like simple and easy to maintain the programming files and collaborate with your teammates.

Most of you do not use git commands either you use GUI plugins in IDE eclipse or Intelleji tools

But, if you know all these handy command and you will get confidence in dealing with the conflicts resolving manually from the terminal.

Top Git Commands With Examples


Monday, April 6, 2020

3 Ways to Fix Git Clone "Filename too long" Error in Windows [Fixed]

1. Introduction


In this tutorial, We'll learn how to fix the git clone error "Filename too long" in windows operating systems Powershell and GitHub Application. This happens when doing a git clone from remote repositories.

Most used Git Commands

This error does not come for the UNIX or mac users. So they can push the long length file names to git but the issues occur only for the windows users. Because this capability is disabled by default in the Windows operating system.

Usually, Git has a limit of 4096 characters for a filename, except on Windows when Git is compiled with MSYS. It uses an older version of the Windows API and there's a limit of 260 characters for a filename.


3 Ways to Fix git "Filename too long" Error in Windows [Fixed]

Friday, March 29, 2019

Learn Top 20 Points in Git - Introduction, Features, Why all are moving to Git

Git Introduction

In this post, we will learn the following.
  • what is git?
  • why it became famous?
  • why all are moving to Git?
  • Git features?
We will see the top 20 important points of Git today.

git introduction

In 2005, Initially Git has evolved by Linus Torvalds.

Every project must be using the Version Control. We are seeing everyone now migrating to Git.
Before moving into Git, first we will go through what is Version Control.

Problems before SVN:

1.    If we do not have the Version Control then if any file is corrupted or lost then recovering the file is difficult and sometimes not possible.
2.    After reopening or recovering file if we want to move to previous state or compare with previous version, who last modified. These are not possible.
3.    We have many issues like and difficult in maintenance hence introduced Version Control.
Examples: CVS, SVN, GIT, Mercurial, Bazaar, Monotone.

For Step-By-Step explanation, Click on how to unstage a file from git.