Friday 19 July 2019

Git: Keep the file but untrack it


In my previous post, I explained how to remove a file from git repository. Some times you may want to keep the file locally but remove it (untrack the file) from the repository. You can do that by using 'git rm --cached {file_name}' command.

For example, my git repository has below files.
$tree
.
├── README.md
├── tempFile1.txt
├── tempFile2.txt
├── tempFile4.txt
└── welcome.txt

0 directories, 5 files

Untrack the file tempFile2.txt by executing below command.

git rm --cached tempFile2.txt
$git rm --cached tempFile2.txt
rm 'tempFile2.txt'


Execute the command ‘git status’.
$git status
On branch master
Your branch is up to date with 'origin/master'.

Changes to be committed:
  (use "git reset HEAD <file>..." to unstage)

 deleted:    tempFile2.txt

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

 tempFile2.txt

$

As you see the output, tempFile2.txt is untracked and not deleted from your local file system. You can confirm the same by printing directory structure.
$tree
.
├── README.md
├── tempFile1.txt
├── tempFile2.txt
├── tempFile4.txt
└── welcome.txt

0 directories, 5 files
$

Commit the changes by executing below command.

git commit -m "Ubtracking tempFile2.txt"
$git commit -m "Ubtracking tempFile2.txt"
[master 4f26ba8] Ubtracking tempFile2.txt
 1 file changed, 0 insertions(+), 0 deletions(-)
 delete mode 100644 tempFile2.txt


Before pushing the changes to remote repository, you can see below files.


Let’s push the changes to remote repository.
$git push origin master
Enumerating objects: 3, done.
Counting objects: 100% (3/3), done.
Delta compression using up to 12 threads
Compressing objects: 100% (2/2), done.
Writing objects: 100% (2/2), 248 bytes | 248.00 KiB/s, done.
Total 2 (delta 1), reused 0 (delta 0)
remote: Resolving deltas: 100% (1/1), completed with 1 local object.
To https://github.com/harikrishna553/gitHelloWorld.git
   3d2911a..4f26ba8  master -> master



Now you can see that tempFile2.txt is removed from remote repository.


Previous                                                    Next                                                    Home

No comments:

Post a Comment