Skip to content
Fix Code Error

How can I delete a file from a Git repository?

March 13, 2021 by Code Error
Posted By: Anonymous

I have added a file named "file1.txt" to a Git repository. After that, I committed it, added a couple of directories called dir1 and dir2, and committed them to the Git repository.

Now the current repository has "file1.txt", dir1, and dir2. How can I delete "file1.txt" without affecting others, like dir1 and dir2?

Solution

Use git rm.

If you want to remove the file from the Git repository and the filesystem, use:

git rm file1.txt
git commit -m "remove file1.txt"

But if you want to remove the file only from the Git repository and not remove it from the filesystem, use:

git rm --cached file1.txt
git commit -m "remove file1.txt"

And to push changes to remote repo

git push origin branch_name
Answered By: Anonymous

Related Articles

  • Find the files existing in one directory but not in…
  • What is the difference between __dirname and ./ in node.js?
  • git pull while not in a git directory
  • Retrieve specific commit from a remote Git repository
  • Calling a Python function from another file and…
  • Checkout another branch when there are uncommitted…
  • How do I make a Git commit in the past?
  • ExpressJS How to structure an application?
  • In plain English, what does "git reset" do?
  • How to solve Internal Server Error in Next.Js?
  • How do SO_REUSEADDR and SO_REUSEPORT differ?
  • Git submodule head 'reference is not a tree' error
  • Ukkonen's suffix tree algorithm in plain English
  • What is your most productive shortcut with Vim?
  • Git workflow and rebase vs merge questions
  • How to recover stashed uncommitted changes
  • diff to output only the file names
  • Git add all subdirectories
  • Why do I have to "git push --set-upstream origin "?
  • git lfs push to github failure on Ubuntu 18.04
  • How to track untracked content?
  • How is Perl's @INC constructed? (aka What are all…
  • Squash the first two commits in Git?
  • What's the difference between HEAD^ and HEAD~ in Git?
  • Is it safe to shallow clone with --depth 1, create…
  • How to parse this string in Java?
  • Git: Create a branch from unstaged/uncommitted…
  • Fast way of finding lines in one file that are not…
  • How can I move HEAD back to a previous location?…
  • How can I merge two commits into one if I already…
  • How To Edit Date and Time Picker Vuejs Vuetify From…
  • Why does Git say my master branch is "already up to…
  • Why do git fetch origin and git fetch : behave differently?
  • How to use Git Revert
  • git status shows modifications, git checkout --…
  • How can I reconcile detached HEAD with master/origin?
  • How to modify existing, unpushed commit messages?
  • How to retrieve a single file from a specific…
  • How to fix Git error: object file is empty?
  • Git Using Remote Branch
  • Does "git fetch --tags" include "git fetch"?
  • How to run functions in parallel?
  • What are the undocumented features and limitations…
  • Various ways to remove local Git changes
  • "git rm --cached x" vs "git reset head --​ x"?
  • Finding what branch a Git commit came from
  • How to simplify sequential logic design by…
  • Why does git perform fast-forward merges by default?
  • Given two directory trees, how can I find out which…
  • Git submodule update
  • How do I get the find command to print out the file…
  • How to exclude a directory in find . command
  • How does PHP 'foreach' actually work?
  • Why does git say "Pull is not possible because you…
  • git bring some "older" changes to a new branch after…
  • How do i update a javascript variable as its value changes?
  • How do I use 'git reset --hard HEAD' to revert to a…
  • How to un-commit last un-pushed git commit without…
  • Identifying and solving…
  • Skip Git commit hooks
  • Git undo changes in some files
  • Setting up and using Meld as your git difftool and mergetool
  • What's the best way of scraping data from a website?
  • The definitive guide to form-based website authentication
  • How do I revert a Git repository to a previous commit?
  • Windows git "warning: LF will be replaced by CRLF",…
  • In git, what is the difference between merge…
  • How do I 'overwrite', rather than 'merge', a branch…
  • Why does C++ code for testing the Collatz conjecture…
  • How to pull in changes from skeleton sub-repository…
  • VS error from std::filesystem::u8path(const char8_t*)
  • Smart way to truncate long strings
  • How to revert multiple git commits?
  • Git merge with force overwrite
  • Partly cherry-picking a commit with Git
  • Why is Git better than Subversion?
  • SVN- How to commit multiple files in a single shot
  • What is "git remote add ..." and "git push origin master"?
  • How to filter a RecyclerView with a SearchView
  • How to list branches that contain a given commit?
  • Why call git branch --unset-upstream to fixup?
  • How to find the nearest parent of a Git branch?
  • How to create the branch from specific commit in…
  • Usage of __slots__?
  • Logging best practices
  • Memcached vs. Redis?
  • How to undo "git commit --amend" done instead of…
  • “tag already exists in the remote" error after…
  • git - Your branch is ahead of 'origin/master' by 1 commit
  • Axios Interceptors retry original request and access…
  • SQL query return data from multiple tables
  • How do I rotate text in css?
  • How to commit my current changes to a different…
  • master branch and 'origin/master' have diverged, how…
  • How to recover a dropped stash in Git?
  • java.sql.SQLException: - ORA-01000: maximum open…
  • Git Cherry-pick vs Merge Workflow
  • Remove specific commit
  • Practical uses of git reset --soft?
  • Why are there two ways to unstage a file in Git?

Disclaimer: This content is shared under creative common license cc-by-sa 3.0. It is generated from StackExchange Website Network.

Post navigation

Previous Post:

How to make a flat list out of list of lists?

Next Post:

How do I efficiently iterate over each entry in a Java Map?

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

.net ajax android angular arrays aurelia backbone.js bash c++ css dataframe ember-data ember.js excel git html ios java javascript jquery json laravel linux list mysql next.js node.js pandas php polymer polymer-1.0 python python-3.x r reactjs regex sql sql-server string svelte typescript vue-component vue.js vuejs2 vuetify.js

  • you shouldn’t need to use z-index
  • No column in target database, but getting “The schema update is terminating because data loss might occur”
  • Angular – expected call-signature: ‘changePassword’ to have a typedeftslint(typedef)
  • trying to implement NativeAdFactory imports deprecated method by default in flutter java project
  • What should I use to get an attribute out of my foreign table in Laravel?
© 2022 Fix Code Error