Table of Contents
Search for commits that added or deleted a specific file
Find when a file was deleted
Search across all commits for a file name
Restore a file from a past commit
Home Development Tools git How to find a file in git history

How to find a file in git history

Sep 16, 2025 am 05:18 AM

Use git log with --all, --full-history, and --diff-filter to find commits involving a deleted or missing file by name, then grep for the filename to identify relevant commits; once located, use git show or git checkout to inspect or restore the file from a specific commit.

How to find a file in git history

Searching for a file that's no longer in your current branch but existed in the past can be common when cleaning up or auditing a repository. Git doesn't have a direct "find file by name in history" command, but you can use a combination of commands to locate when and where a file was added or removed.

Search for commits that added or deleted a specific file

Use git log with path filtering and diff options to find commits that involved a file by name, even if it no longer exists.

To find all commits that touched a file named config.yml, run:

git log --all --full-history --diff-filter=ACDMR --summary --name-only | grep config.yml

  • --all: Search all branches
  • --full-history: Include all history, even merges
  • --diff-filter=ACDMR: Show commits where files were Added, Copied, Deleted, Modified, or Renamed
  • --summary --name-only: Show changed filenames
  • grep config.yml: Filter output for your target filename

This will list commits where config.yml appeared or changed.

Find when a file was deleted

If you know the file was deleted, you can search for deletion events specifically.

Run:

git log --all --diff-filter=D --summary --name-only | grep deleted-file.txt

This shows only commits that deleted the file. Once you find the commit hash, inspect it with:

git show

This reveals the full diff, including the contents of the file before deletion.

Search across all commits for a file name

To list every file that ever existed in the repo’s history and search within that list:

Run:

git log --all --pretty=format: --name-only | sort -u | grep filename

  • --pretty=format:: Suppress commit messages
  • --name-only: Output only filenames
  • sort -u: Remove duplicates
  • grep filename: Filter by your target name

This gives you a clean list of all unique files in history matching your search term.

Restore a file from a past commit

Once you find the commit where the file existed, you can restore it to your working directory.

From the commit hash found earlier:

git checkout -- path/to/file.txt

This checks out the file as it was at that commit. You can then move it into place or inspect its contents.

Basically, use git log with path and diff filtering to hunt down files in history. Combine with grep and checkout to locate and recover them. It's not a single command, but the approach is reliable and fast.

The above is the detailed content of How to find a file in git history. For more information, please follow other related articles on the PHP Chinese website!

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undress AI Tool

Undress AI Tool

Undress images for free

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

ArtGPT

ArtGPT

AI image generator for creative art from text prompts.

Stock Market GPT

Stock Market GPT

AI powered investment research for smarter decisions

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

What is Git, and why is it used for version control? What is Git, and why is it used for version control? Aug 30, 2025 am 03:47 AM

Git is a version control system that tracks the changes in code over time. It records the content, author and reason for each change by saving snapshots of projects at different points in time. Its core functions include: 1. Allowing selective temporary storage and submission to form a part of the project history; 2. Automatically track version differences, simplifying collaboration and merging multi-person modifications; 3. Providing a built-in backup mechanism and supporting parallel development through branches; 4. Combining with platforms such as GitHub, it realizes remote storage, code review and automated deployment processes. These features make Git an indispensable tool in modern software development.

How do I create a new branch at a specific commit in the past? How do I create a new branch at a specific commit in the past? Sep 16, 2025 am 02:52 AM

To create a new branch from the old commit, first find the hash value of the target commit, then use the gitcheckout-b or gitswitch-c command to create the branch, and finally verify that the branch is generated correctly. The specific steps are: 1. Use gitlog--oneline to find the commit hash, or use gitlog-S "keyword" to locate a specific commit; 2. Execute gitcheckout-b new branch name submission hash or gitswitch-c new branch name submission hash to create a branch, Git supports abbreviated hash; 3. Confirm branch information through gitlog and gitbranch, and check the file content to ensure correctness. The whole process is simple and reliable, and can be done after proficiency.

What is Git Cherry-Pick and When Should You Use It? What is Git Cherry-Pick and When Should You Use It? Aug 29, 2025 am 09:04 AM

Gitcherry-pickisusedtoapplyaspecificcommitfromonebranchtoanother.1.Applyahotfixfrommaintoafeaturebranchwithoutmergingallchanges.2.Shareasecuritypatchacrossmultiplereleasebrancheslikev1.2andv1.3.3.Recoveralostcommitafteraresetusingitshashfromgitreflog

How to find a file in git history How to find a file in git history Sep 16, 2025 am 05:18 AM

Usegitlogwith--all,--full-history,and--diff-filtertofindcommitsinvolvingadeletedormissingfilebyname,thengrepforthefilenametoidentifyrelevantcommits;oncelocated,usegitshoworgitcheckouttoinspectorrestorethefilefromaspecificcommit.

How do I contribute to an open-source project using Git? How do I contribute to an open-source project using Git? Sep 02, 2025 am 04:18 AM

Tocontributetoanopen-sourceprojectusingGit,followthesesteps:1.FindaprojectandissuetoworkonbyexploringGitHubforbeginner-friendlylabelslike“goodfirstissue,”ensuringtheprojectisactive,andreadingguidelinesinREADMEandCONTRIBUTING.mdfiles.2.Forkthereposito

Setting Up and Using a Global .gitignore File Setting Up and Using a Global .gitignore File Aug 31, 2025 am 07:50 AM

Create a global .gitignore file and add general ignorance rules (such as .DS_Store, .idea/, node_modules/, etc.), 2. Register the file through the gitconfig--globalcore.excludesfile~/.gitignore_global command. Git will automatically apply these ignorance rules in all repositories without repeated configuration, improving efficiency and maintaining the project.gitignore focuses on the project itself. After setting up, you can verify whether it takes effect and complete it by creating a new test repository.

Using Git for More Than Code: A Guide for Non-Developers Using Git for More Than Code: A Guide for Non-Developers Sep 07, 2025 am 07:13 AM

Non-developers can use Git for efficient versioning, collaboration and backup without programming. 1. Git supports viewing and modifying records, falling back to any version, processing multiple versions in parallel, and sharing files securely; 2. Applicable to collaborative writing, document management, academic research and design teams (managing text design specifications); 3. Use visual tools such as GitHubDesktop to avoid command-line operations; 4. Follow best practices such as clear submission of messages, branch development, frequent submission, and use of .gitignore to ignore unrelated files; 5. Avoid large-volume binary files, and give priority to using plain text format or GitLFS. Start with a simple folder, submit once a day, and gradually master this document time like

Integrating Git with VS Code for a Seamless Workflow Integrating Git with VS Code for a Seamless Workflow Sep 05, 2025 am 08:47 AM

InstallGitandinitializearepositoryviaVSCode’sCommandPalette.2.UsetheSourceControlpaneltostage,commit,andreviewchangeswithauto-stageandauto-fetchenabledforefficiency.3.ManagebranchesandsyncwithremoterepositoriesusingthestatusbarorCommandPalette.4.Reso

See all articles