Git how to remove file from index
Git how to remove file from index
Git how to remove file from index
Setup and Config
Getting and Creating Projects
Basic Snapshotting
Branching and Merging
Sharing and Updating Projects
Inspection and Comparison
Patching
Debugging
External Systems
Server Admin
Guides
Administration
Plumbing Commands
Check your version of git by running
SYNOPSIS
DESCRIPTION
OPTIONS
The command removes only the paths that are known to Git.
For more details, see the pathspec entry in gitglossary[7].
Override the up-to-date check.
Don’t actually remove any file(s). Instead, just show if they exist in the index and would otherwise be removed by the command.
Allow recursive removal when a leading directory name is given.
This option can be used to separate command-line options from the list of files, (useful when filenames might be mistaken for command-line options).
Use this option to unstage and remove paths only from the index. Working tree files, whether modified or not, will be left alone.
Exit with a zero status even if no files matched.
Allow updating index entries outside of the sparse-checkout cone. Normally, git rm refuses to update index entries whose paths do not fit within the sparse-checkout cone. See git-sparse-checkout[1] for more.
git rm normally outputs one line (in the form of an rm command) for each file removed. This option suppresses that output.
REMOVING FILES THAT HAVE DISAPPEARED FROM THE FILESYSTEM
There is no option for git rm to remove from the index only the paths that have disappeared from the filesystem. However, depending on the use case, there are several ways that can be done.
When accepting a new code drop for a vendor branch, you probably want to record both the removal of paths and additions of new paths as well as modifications of existing paths.
Typically you would first remove all tracked files from the working tree using this command:
and then untar the new code in the working tree. Alternately you could rsync the changes into the working tree.
After that, the easiest way to record all removals, additions, and modifications in the working tree is:
Other ways
SUBMODULES
A submodule is considered up to date when the HEAD is the same as recorded in the index, no tracked files are modified and no untracked files that aren’t ignored are present in the submodules work tree. Ignored files are deemed expendable and won’t stop a submodule’s work tree from being removed.
If you only want to remove the local checkout of a submodule from your work tree without committing the removal, use git-submodule[1] deinit instead. Also see gitsubmodules[7] for details on submodule removal.
EXAMPLES
Removes all *.txt files from the index that are under the Documentation directory and any of its subdirectories.
Note that the asterisk * is quoted from the shell in this example; this lets Git, and not the shell, expand the pathnames of files and subdirectories under the Documentation/ directory.
Git RM
A common question when getting started with Git is «How do I tell Git not to track a file (or files) any more?» The git rm command is used to remove files from a Git repository. It can be thought of as the inverse of the git add command.
Git rm Overview
Note that git rm does not remove branches. Learn more about using git branches
Usage
The «dry run» option is a safeguard that will execute the git rm command but not actually delete the files. Instead it will output which files it would have removed.
The cached option specifies that the removal should happen only on the staging index. Working directory files will be left alone.
The quiet option hides the output of the git rm command. The command normally outputs one line for each file removed.
How to undo git rm
Executing git rm is not a permanent update. The command will update the staging index and the working directory. These changes will not be persisted until a new commit is created and the changes are added to the commit history. This means that the changes here can be «undone» using common Git commands.
In the event that git rm was executed and a new commit was created which persist the removal, git reflog can be used to find a ref that is before the git rm execution. Learn more about using git reflog.
Discussion
The file> argument given to the command can be exact paths, wildcard file glob patterns, or exact directory names. The command removes only paths currently commited to the Git repository.
The scope of git rm
The git rm command operates on the current branch only. The removal event is only applied to the working directory and staging index trees. The file removal is not persisted to the repository history until a new commit is created.
Why use git rm instead of rm
A Git repository will recognize when a regular shell rm command has been executed on a file it is tracking. It will update the working directory to reflect the removal. It will not update the staging index with the removal. An additional git add command will have to be executed on the removed file paths to add the changes to the staging index. The git rm command acts a shortcut in that it will update the working directory and the staging index with the removal.
Examples
This example uses a wildcard file glob to remove all *.txt files that are children of the Documentation directory and any of its subdirectories.
Note that the asterisk * is escaped with slashes in this example; this is a guard that prevents the shell from expanding the wildcard. The wildcard then expands the pathnames of files and subdirectories under the Documentation/ directory.
This example uses the force option and targets all wildcard git-*.sh files. The force option explicitly removes the target files from both the working directory and staging index.
How to remove files no longer in the filesystem
Git rm summary
Git: How to remove file from index without deleting files from any repository
it doesn’t delete from the local filesystem, which is the goal. But if you’ve already versioned and committed the file, pushed it to a central repository, and pulled it into yet another repository before using the command, it will delete the file from that system.
Is there a way to just remove the file from versioning without deleting it from any filesystem?
Edit: Clarified, I hope.
7 Answers 7
Trending sort
Trending sort is based off of the default sorting method — by highest score — but it boosts votes that have happened recently, helping to surface more up-to-date answers.
It falls back to sorting by highest score if no posts are trending.
Switch to Trending sort
I do not think a Git commit can record an intention like “stop tracking this file, but do not delete it”.
Enacting such an intention will require intervention outside Git in any repositories that merge (or rebase onto) a commit that deletes the file.
Save a Copy, Apply Deletion, Restore
Restore File as Untracked After Pulling a Commit That Deletes It
If they have already pulled your deletion commit, they can still recover the previous version of the file with git show:
Or with git checkout (per comment by William Pursell; but remember to re-remove it from the index!):
In a comment, you mention that the file you want to “untrack, but keep” is some kind of configuration file that is required for running the software (directly out of a repository).
Keep File as a ‘Default’ and Manually/Automatically Activate It
If it is not completely unacceptable to continue to maintain the configuration file’s content in the repository, you might be able to rename the tracked file from (e.g.) foo.conf to foo.conf.default and then instruct your users to cp foo.conf.default foo.conf after applying the rename commit. Or, if the users already use some existing part of the repository (e.g. a script or some other program configured by content in the repository (e.g. Makefile or similar)) to launch/deploy your software, you could incorporate a defaulting mechanism into the launch/deploy process:
With such a defaulting mechanism in place, users should be able to pull a commit that renames foo.conf to foo.conf.default without having to do any extra work. Also, you avoid having to manually copy a configuration file if you make additional installations/repositories in the future.
Rewriting History Requires Manual Intervention Anyway…
How To Remove Or Unstage Files From Your Git Staging Index
3 Minutes, 48 Seconds to Read
In this article, we’re going to look at two easy ways to remove files from the staging index in Git. In Git, there are many different commands that can be employed to accomplish the same goal. Below you’ll learn two commands that you can use to remove files and modifications from the staging index and what the differences are between them.
No matter what kind of work you’re doing, resetting, removing, or otherwise erasing files is dangerous. There’s always a possibility of losing work.
This command will not remove files from the working directory, but only remove modifications and new files from the staging index.
The git rm command can easily be confused with the rm command available in most UNIX-like operating systems—including GNU/Linux operating systems and Mac OS.
But, the rm (or, /bin/rm ) command and git rm command function very differently. While rm should be employed when removing files from your working directory, effectively erasing a file from existence, git rm will remove files or file modifications from the Git staging index.
(Refresher: before files and file modifications are committed to your Git repository, they stop off in the staging index. Files, and any modifications to files, being tracked by your Git repository must be “staged” before they are committed. Only what is staged ends up in a commit.)
Thus, this is how are two commands are used separately:
To remove a file from the staging index, run the command like this:
No changes are made to the working directory.
Using the git reset HEAD method
The git reset command is incredibly power and can wipe out your work entirely. So be careful to use this command with great caution.
The git reset command is used to reset your project, or aspects of your project, to a certain state.
As you can imagine, the process can be very helpful or massively destructive.
But in this instance, we’re safely going to use this command to remove items from the staging index, which can include:
Here’s how the command is used. (In order to see the difference, run git status before and after running the following command.)
If you run git status after this command, you’ll notice that modifications and new files added to the staging index have been removed yet no changes are made to the working directory.
Background Information on The Staging Index
The staging index is one of three important concepts about the architecture of Git and how it works.
So that is listed as follows:
Files in your project have four different statuses:
Well done! Now you know how you can remove files from your Git staging index. Be sure to leave a comment below or ask a question if you get stuck.
Christopher Maiorana joined the InMotion community team in 2015 and regularly dispenses tips and tricks in the Support Center, Community Q&A, and the InMotion Hosting Blog.
Comments
Was this article helpful? Let us know! Cancel reply
Need help? Ask a question, share a helpful tip, or help others in our community forum.
Remove a file from a Git repository without deleting it from the local filesystem
I want to remove a file from my repository.
will remove the file from the repository, but it will also remove the file from the local file system. How do I remove this file from the repo without deleting my local copy of the file?
13 Answers 13
Trending sort
Trending sort is based off of the default sorting method — by highest score — but it boosts votes that have happened recently, helping to surface more up-to-date answers.
It falls back to sorting by highest score if no posts are trending.
Switch to Trending sort
The git rm documentation states:
So, for a single file:
and for a single directory:
To remove an entire folder from the repo (like Resharper files), do this:
I had committed some resharper files, and did not want those to persist for other project users.
For Windows Powershell:
To remove folder/directory or file only from git repository and not from the local try 3 simple steps.
Steps to remove directory
Steps to ignore that folder in next commits
To ignore that folder from next commits make one file in root named .gitignore and put that folders name into it. You can put as many as you want
.gitignore file will be look like this
A more generic solution:
Remove all items from index.
Make new commit
Also, if you have commited sensitive data (e.g. a file containing passwords), you should completely delete it from the history of the repository. Here’s a guide explaining how to do that: http://help.github.com/remove-sensitive-data/
Hence, not deleting it, but ignoring changes to it forever. I think this only works locally, so co-workers can still see changes to it unless they run the same command as above. (Still need to verify this though.)
Note: This isn’t answering the question directly, but is based on follow up questions in the comments of the other answers.
Источники информации:
- http://www.atlassian.com/git/tutorials/undoing-changes/git-rm
- http://stackoverflow.com/questions/2604625/git-how-to-remove-file-from-index-without-deleting-files-from-any-repository
- http://www.inmotionhosting.com/support/website/git/how-to-remove-or-unstage-files-from-your-git-staging-index/
- http://stackoverflow.com/questions/1143796/remove-a-file-from-a-git-repository-without-deleting-it-from-the-local-filesyste