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:
colors.txt
Add red.txt
with these contents:
red
Add green.txt
with these contents:
green
Add blue.txt
with these contents:
blue
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
)?
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.
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.
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.
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With