简体   繁体   中英

How do I show git files which were deleted several commits ago

I have deleted a file several commits ago and now I need part of it.
How to show it so I can copy paste?

I've tried this:

git show HEAD~2 -- path/to/file

But I get no output at all. Autocompletion doesn't work with deleted files so I am not 100% sure about file name, but I am 99% sure.

List all changed files between two commits.

 git diff --name-only START_COMMIT..END_COMMIT

Using --name-status will also show the changes like added, deleted, Modified along with file

git diff --name-status START_COMMIT..END_COMMIT

You can checkout the commit where you deleted it ( git checkout "old-commit-hash" ), copy the files you need and and checkout your current data again ( git commit "current-commit-hash" ).

The hashes can be looked up with: git log

You can find out a name of your deleted file using command git whatchanged . It shows a list of files that was modified by previous commits. Deleted files are marked with letter D. After you recognize the filename, to restore it, type git checkout <hash_id>~ -- <filename> , where <hash_id> means a hash of a commit in which you deleted the file with specified <filename> .

If you want to look at the file only, type git show <hash_id> -- <filename> . This command prints changes (including deleting) of the file made by the specified commit.

I think your problem is :

  1. You removed a file
  2. Then committed
  3. Lot's of commits happened
  4. Now need information of that file.

If this is the case, what you have done is correct. But something more to achieve it.

  1. git log --pretty=format:"%h - %an, %ad --> %s" --date=iso
    This will give you commits with date in readable format. Let there are 100 commits and your commit date is around 30th from HEAD (then take some 'last 40' commits)

  2. git log --pretty=format:"%h - %an, %ad --> %s" --date=iso | head -40 | awk '{print $1}' git log --pretty=format:"%h - %an, %ad --> %s" --date=iso | head -40 | awk '{print $1}' If this gives 40 commits as output, do the following.

  3. while read commit ; do echo -e "\\n\\nFile committed in $commit are : " ; git show --pretty="format:" --name-only $commit ; done< <(git log --pretty=format:"%h - %an, %ad --> %s" --date=iso | head -40 | awk '{print $1}')

  4. Then take the commit ID with your file.

  5. And then you can go for your command.

git cat-file -p HEAD~2:path/to/file

Using gitk.

One simple approach, if you happen to use the gitk gui, just simple run gitk in your terminal and go through the git tree list where you can the deleted files.


Using CLI way:

git log --your_file_path 

or

git log --full-history -- your_file_path

If you just want to see the deleted file from the tree, Check this answer .

This will show you last change on particular file:

git log -1 -p -- <file_path>

However, this sometimes is not very handy, since you will see the changes, but copy-paste process might be tricky due to leading - in front of every change line.

If you want to restore the file so that you can easily copy-paste from it:

  • Get SHA of the commit where you have deleted the file:

     git rev-list -n 1 HEAD -- <file_path> 
  • Checkout the file at the commit before, using the caret ( ^ ) symbol:

     git checkout <SHA>^ -- <file_path> 

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM