Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Viewing changes to deleted files in git

Let's say that when I branch off from master, there's a file colors.txt on the master branch of my repo with these contents:

red
green
blue
yellow

I then branch off into my-branch, in which I make the following changes:

  1. Delete colors.txt
  2. Add red.txt with these contents:

    red
    
  3. Add green.txt with these contents:

    green
    
  4. Add blue.txt with these contents:

    blue
    
  5. Add yellow.txt with these contents:

    yellow
    

Now, there have been some changes on master that I need, so I want to merge. However, someone has also changed colors.txt to:

red
green
blue
yellow
orange
purple

During my merge, the only information I get is that I deleted the file colors.txt, so how can I see the changes that have been to the file on master so I can appropriately resolve the conflict (in this case, by adding the files orange.txt and purple.txt)?

like image 552
ericmarkmartin Avatar asked Jul 05 '17 20:07

ericmarkmartin


People also ask

Can you see deleted files in git history?

Listing all the deleted files in all of git history can be done by combining git log with --diff-filter . The log gives you lots of options to show different bits of information about the commit that happened at that point.

Can you see deleted files on GitHub?

If you have committed the deletion and pushed it to GitHub, it is possible to recover a deleted file using the GitHub Web UI. GitHub lets you browse the commit history and explore the project at any point in history, which then allows you to view and download any file.

What happens to deleted files in git?

About file and directory deletionIf the file or directory you deleted contains sensitive data, the data will still be available in the repository's Git history. To completely remove the file from GitHub, you must remove the file from your repository's history.


1 Answers

You can show all changes done to that file on master using this command:

git diff HEAD...master -- colors.txt

This should lead to this output in your case:

red
green
blue
yellow
+orange
+purple

Using three dots for git diff will show the changes of all commits which are parents of the second referenced commit but not of the first referenced commit. Therefore, using this you will see all changes which would be merged if the file had not been deleted.

By using -- colors.txt the changes shown by git diff are limited to that file.

A more generic version of that command would be

git diff HEAD...MERGE_HEAD -- colors.txt

MERGE_HEAD is always set to the merged commit, thus it can replace the merged branch name. Using this you could even set up an alias to reuse this command:

git config --global alias.merge-diff-theirs "diff HEAD...MERGE_HEAD"

Afterwards you can just do

git merge-diff-theirs -- colors.txt
like image 167
lucash Avatar answered Oct 14 '22 06:10

lucash